File tree 2 files changed +15
-1
lines changed
2 files changed +15
-1
lines changed Original file line number Diff line number Diff line change @@ -17,14 +17,18 @@ class IpAddress
17
17
/**
18
18
* @var string
19
19
*/
20
- private $ ipAddress = '' ;
20
+ private $ ipAddress ;
21
21
22
22
/**
23
23
* Country constructor.
24
24
* @param string $ipAddress
25
25
*/
26
26
public function __construct (string $ ipAddress )
27
27
{
28
+ // Clean up in case of comma-separated IPs
29
+ $ ipAddressList = explode (', ' , $ ipAddress );
30
+ $ ipAddress = trim (reset ($ ipAddressList ));
31
+
28
32
if (filter_var ($ ipAddress , FILTER_VALIDATE_IP ) === false ) {
29
33
throw new InvalidArgumentException ('Value " ' . $ ipAddress . '" is not a valid IP address ' );
30
34
}
Original file line number Diff line number Diff line change @@ -29,4 +29,14 @@ public function testWhetherWrongValueCanNotBeSetAndUsed()
29
29
$ this ->expectException (InvalidArgumentException::class);
30
30
new IpAddress ('foobar ' );
31
31
}
32
+
33
+ /**
34
+ * Test whether comma-separated values could be set and used
35
+ * @link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-For#examples
36
+ */
37
+ public function testWhetherCommaSeparatedValuesCanBeSetAndUsed ()
38
+ {
39
+ $ ipAddress = new IpAddress ('203.0.113.195, 70.41.3.18, 150.172.238.178 ' );
40
+ $ this ->assertEquals ('203.0.113.195 ' , $ ipAddress ->get ());
41
+ }
32
42
}
You can’t perform that action at this time.
0 commit comments