Skip to content

Commit ae1c473

Browse files
thomasbisignaninorberttech
authored andcommitted
Added isIp() pattern expander (#131)
1 parent a9d0f0b commit ae1c473

File tree

5 files changed

+87
-0
lines changed

5 files changed

+87
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ $matcher->getError(); // returns null or error message
7777
* ``isDateTime()``
7878
* ``isEmail()``
7979
* ``isUrl()``
80+
* ``isIp()``
8081
* ``isEmpty()``
8182
* ``isNotEmpty()``
8283
* ``lowerThan($boundry)``

src/Matcher/Pattern/Expander/IsIp.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Coduo\PHPMatcher\Matcher\Pattern\Expander;
6+
7+
use Coduo\PHPMatcher\Matcher\Pattern\PatternExpander;
8+
use Coduo\ToString\StringConverter;
9+
10+
final class IsIp implements PatternExpander
11+
{
12+
const NAME = 'isIp';
13+
14+
private $error;
15+
16+
public static function is(string $name) : bool
17+
{
18+
return self::NAME === $name;
19+
}
20+
21+
public function match($value) : bool
22+
{
23+
if (false === \is_string($value)) {
24+
$this->error = \sprintf('IsIp expander require "string", got "%s".', new StringConverter($value));
25+
return false;
26+
}
27+
28+
if (false === $this->matchValue($value)) {
29+
$this->error = \sprintf('string "%s" is not a valid IP address.', $value);
30+
return false;
31+
}
32+
33+
return true;
34+
}
35+
36+
public function getError()
37+
{
38+
return $this->error;
39+
}
40+
41+
private function matchValue(string $value) : bool
42+
{
43+
try {
44+
return false !== \filter_var($value, FILTER_VALIDATE_IP);
45+
} catch (\Exception $e) {
46+
return false;
47+
}
48+
}
49+
}

src/Parser/ExpanderInitializer.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ final class ExpanderInitializer
2525
Expander\IsEmpty::NAME => Expander\IsEmpty::class,
2626
Expander\IsNotEmpty::NAME => Expander\IsNotEmpty::class,
2727
Expander\IsUrl::NAME => Expander\IsUrl::class,
28+
Expander\IsIp::NAME => Expander\IsIp::class,
2829
Expander\LowerThan::NAME => Expander\LowerThan::class,
2930
Expander\MatchRegex::NAME => Expander\MatchRegex::class,
3031
Expander\OneOf::NAME => Expander\OneOf::class,
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Coduo\PHPMatcher\Tests\Matcher\Pattern\Expander;
6+
7+
use Coduo\PHPMatcher\Matcher\Pattern\Expander\IsIp;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class IsIpTest extends TestCase
11+
{
12+
/**
13+
* @dataProvider examplesIpProvider
14+
*/
15+
public function test_ip($ip, $expected)
16+
{
17+
$expander = new IsIp();
18+
$this->assertEquals($expected, $expander->match($ip));
19+
}
20+
21+
public static function examplesIpProvider()
22+
{
23+
return [
24+
['127.0.0.1', true],
25+
['255.255.255.255', true],
26+
['2001:0db8:0000:42a1:0000:0000:ab1c:0001', true],
27+
['999.999.999.999', false],
28+
['127.127', false],
29+
['foo:bar:42:42', false]
30+
];
31+
}
32+
}

tests/MatcherTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,10 @@ public static function expanderExamples()
384384
[[], ['unexistent_key' => '@[email protected]()'], true],
385385
[[], ['unexistent_key' => '@[email protected]()'], true],
386386
[['Norbert', 'Michał'], '@[email protected]("@string@")', true],
387+
['127.0.0.1', '@[email protected]()', true],
388+
['2001:0db8:0000:42a1:0000:0000:ab1c:0001', '@[email protected]()', true],
389+
['127.255.999.999', '@[email protected]()', false],
390+
['foo:bar:42:42', '@[email protected]()', false],
387391
];
388392
}
389393

0 commit comments

Comments
 (0)