Skip to content

Commit d86e0b0

Browse files
Updated ip rule
1 parent 6f8b0e4 commit d86e0b0

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

src/Exceptions/IpVNException.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ class IpVNException extends ValidationException
2424
public static $defaultTemplates = [
2525
self::MODE_DEFAULT => [
2626
self::STANDARD => '{{name}} must be an IP address of Viet Nam',
27-
self::VERSION => '{{name}} must be an IP version {{version}}',
27+
self::VERSION => '{{name}} must be an IP address version {{version}} of Viet Nam',
2828
],
2929
self::MODE_NEGATIVE => [
3030
self::STANDARD => '{{name}} must be an IP address of Viet Nam',
31-
self::VERSION => '{{name}} must be an IP version {{version}}',
31+
self::VERSION => '{{name}} must be an IP address version {{version}} of Viet Nam',
3232
],
3333
];
3434

src/Rules/IpVN.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use IPLib\Address\AddressInterface as IpInterface;
1212
use IPLib\Address\Type as IpType;
1313
use IPLib\Factory as IpFactory;
14+
use Respect\Validation\Exceptions\ComponentException;
1415
use Respect\Validation\Rules\AbstractRule;
1516

1617
/**
@@ -26,8 +27,12 @@ class IpVN extends AbstractRule
2627

2728
public $version;
2829

29-
public function __construct($version = null)
30+
public function __construct(?int $version = null)
3031
{
32+
if(null !== $version && !$this->isSupportedVersion($version)) {
33+
throw new ComponentException(sprintf('Only versions %d, %d are supported: %d given', self::IPV4, self::IPV6, $version));
34+
}
35+
3136
$this->version = $version;
3237
}
3338

@@ -107,4 +112,10 @@ protected static function getIpV6Range(): array
107112

108113
return $range;
109114
}
115+
116+
protected function isSupportedVersion(int $version): bool
117+
{
118+
return self::IPV4 === $version || self::IPV6 === $version;
119+
}
120+
110121
}

tests/Rules/IpVNTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ public function testValidV6ShouldReturnTrue($input)
7070
$this->assertTrue($this->validatorV6->check($input));
7171
}
7272

73+
public function testInvalidVersionShouldThrowException()
74+
{
75+
$this->expectException('\Respect\Validation\Exceptions\ComponentException');
76+
new IpVN(100);
77+
}
78+
7379
/**
7480
* @dataProvider providerInvalid
7581
*/

0 commit comments

Comments
 (0)