File tree Expand file tree Collapse file tree 3 files changed +97
-1
lines changed Expand file tree Collapse file tree 3 files changed +97
-1
lines changed Original file line number Diff line number Diff line change 9
9
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.
10
10
11
11
## Related projects
12
- * https://github.com/cultuurnet/valueobjects
12
+ - https://github.com/cultuurnet/valueobjects
13
+ - https://github.com/nubeiro/value-objects
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments