Skip to content

Commit 77c5d1d

Browse files
committed
Merge branch 'release/0.5'
2 parents ee1b4e2 + e9d6207 commit 77c5d1d

File tree

3 files changed

+97
-1
lines changed

3 files changed

+97
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@
99
Provide objects which follow value semantics rather than reference semantics. This means value objects' equality are not based on identity. Two value objects are equal when they have the same value, not necessarily being the same object.
1010

1111
## Related projects
12-
* https://github.com/cultuurnet/valueobjects
12+
- https://github.com/cultuurnet/valueobjects
13+
- https://github.com/nubeiro/value-objects

src/Network/IpAddress.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Talentify\ValueObject\Network;
6+
7+
use Talentify\ValueObject\ValueObject;
8+
9+
class IpAddress implements ValueObject
10+
{
11+
/** @var string */
12+
private $ipAddress;
13+
14+
/**
15+
* @throws \InvalidArgumentException if IP address is not valid
16+
*/
17+
public function __construct(string $ipAddress)
18+
{
19+
if (filter_var($ipAddress, FILTER_VALIDATE_IP) === false) {
20+
throw new \InvalidArgumentException(sprintf('The value %s is not a valid IP address.', $ipAddress));
21+
}
22+
23+
$this->ipAddress = $ipAddress;
24+
}
25+
26+
public function getIpAddress() : string
27+
{
28+
return $this->ipAddress;
29+
}
30+
31+
public function equals(?ValueObject $object) : bool
32+
{
33+
if (!$object instanceof self) {
34+
return false;
35+
}
36+
37+
return $object->getIpAddress() === $this->getIpAddress();
38+
}
39+
40+
public function __toString() : string
41+
{
42+
return $this->ipAddress;
43+
}
44+
}

tests/Network/IpAddressTest.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Talentify\ValueObject\Network;
6+
7+
use Talentify\ValueObject\ValueObjectTestCase;
8+
9+
class IpAddressTest extends ValueObjectTestCase
10+
{
11+
/**
12+
* @test
13+
*/
14+
public function willThrownExceptionIfValueIsInvalid() : void
15+
{
16+
$this->expectException(\InvalidArgumentException::class);
17+
new IpAddress('fooBar');
18+
}
19+
20+
public static function getClassName() : string
21+
{
22+
return IpAddress::class;
23+
}
24+
25+
public function sameValueDataProvider() : array
26+
{
27+
return [
28+
[new IpAddress('127.0.0.1'), new IpAddress('127.0.0.1')],
29+
[
30+
new IpAddress('2001:0db8:85a3:08d3:1319:8a2e:0370:7344'),
31+
new IpAddress('2001:0db8:85a3:08d3:1319:8a2e:0370:7344')
32+
],
33+
// #FIXME these as equal IPv6 addresses
34+
// [
35+
// new IpAddress('2001:0db8:85a3:0000:0000:0000:0000:7344'),
36+
// new IpAddress('2001:0db8:85a3::7344')
37+
// ]
38+
];
39+
}
40+
41+
public function differentValueDataProvider() : array
42+
{
43+
return [
44+
[new IpAddress('127.0.0.1'), new IpAddress('192.168.0.1')],
45+
[
46+
new IpAddress('2001:0db8:85a3:08d3:1319:8a2e:0370:7344'),
47+
new IpAddress('2001:0db8:85a3:08d3:1319:8a2e:0370:5555')
48+
]
49+
];
50+
}
51+
}

0 commit comments

Comments
 (0)