Skip to content

Commit aa280fe

Browse files
committed
Add IpAddressV6
1 parent 19a7709 commit aa280fe

3 files changed

Lines changed: 72 additions & 3 deletions

File tree

src/Web/IpAddressV6.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Tanigami\ValueObjects\Web;
4+
5+
use InvalidArgumentException;
6+
7+
class IpAddressV6
8+
{
9+
/**
10+
* @var string
11+
*/
12+
private $ipAddressV6;
13+
14+
/**
15+
* @param string $ipAddressV6
16+
*/
17+
public function __construct(string $ipAddressV6)
18+
{
19+
if (false === filter_var($ipAddressV6, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
20+
throw new InvalidArgumentException(sprintf('Invalid IP address v6: %s', $ipAddressV6));
21+
}
22+
$this->ipAddressV6 = $ipAddressV6;
23+
}
24+
25+
/**
26+
* @param self $other
27+
* @return bool
28+
*/
29+
public function equals(self $other): bool
30+
{
31+
return $this->ipAddressV6 === $other->ipAddressV6;
32+
}
33+
34+
/**
35+
* @return string
36+
*/
37+
public function ipAddressV6(): string
38+
{
39+
return $this->ipAddressV6;
40+
}
41+
}

tests/Web/IpAddressV4Test.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ class IpAddressV4Test extends TestCase
88
{
99
/**
1010
* @expectedException \InvalidArgumentException
11-
* @expectedMessage nvalid IP address v4: this_is_not_ipv4
11+
* @expectedMessage Invalid IP address v4: this_is_not_ipv4
1212
*/
13-
public function testConstructorReturnsExceptionIfUrlIsInvalid()
13+
public function testConstructorReturnsExceptionIfIpv4IsInvalid()
1414
{
1515
new IpAddressV4('this_is_not_ipv4');
1616
}
@@ -20,7 +20,7 @@ public function testGetterReturnsValueString()
2020
$this->assertSame('192.168.0.1', (new IpAddressV4('192.168.0.1'))->ipAddressV4());
2121
}
2222

23-
public function testEquelsReturnsTrueIfUrlsAreSame()
23+
public function testEqualsReturnsTrueIfIpv4sAreSame()
2424
{
2525
$this->assertTrue((new IpAddressV4('192.168.0.1'))->equals(new IpAddressV4('192.168.0.1')));
2626
$this->assertFalse((new IpAddressV4('192.168.0.1'))->equals(new IpAddressV4('10.0.0.1')));

tests/Web/IpAddressV6Test.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Tanigami\ValueObjects\Web;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
class IpAddressV6Test extends TestCase
8+
{
9+
/**
10+
* @expectedException \InvalidArgumentException
11+
* @expectedMessage Invalid IP address v6: this_is_not_ipv6
12+
*/
13+
public function testConstructorReturnsExceptionIfIpV6IsInvalid()
14+
{
15+
new IpAddressV6('this_is_not_ipv6');
16+
}
17+
18+
public function testGetterReturnsValueString()
19+
{
20+
$this->assertSame('2001:0db8:85a3:0000:0000:8a2e:0370:7334', (new IpAddressV6('2001:0db8:85a3:0000:0000:8a2e:0370:7334'))->ipAddressV6());
21+
}
22+
23+
public function testEqualsReturnsTrueIfIpv6sAreSame()
24+
{
25+
$this->assertTrue((new IpAddressV6('2001:0db8:85a3:0000:0000:8a2e:0370:7334'))->equals(new IpAddressV6('2001:0db8:85a3:0000:0000:8a2e:0370:7334')));
26+
$this->assertFalse((new IpAddressV6('2001:0db8:85a3:0000:0000:8a2e:0370:7334'))->equals(new IpAddressV6('0:0:0:0:0:0:0:1')));
27+
}
28+
}

0 commit comments

Comments
 (0)