Skip to content

Commit de6f2c8

Browse files
Updated test cases
1 parent 67a6a7c commit de6f2c8

File tree

9 files changed

+1334
-418
lines changed

9 files changed

+1334
-418
lines changed

resource/ip-v4-range.php

Lines changed: 704 additions & 134 deletions
Large diffs are not rendered by default.

resource/ip-v6-range.php

Lines changed: 290 additions & 270 deletions
Large diffs are not rendered by default.

src/Exceptions/IpVNException.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,27 @@
1616
class IpVNException extends ValidationException
1717
{
1818

19+
const STANDARD = 0;
20+
21+
const VERSION = 1;
22+
1923
public static $defaultTemplates = [
2024
self::MODE_DEFAULT => [
21-
self::STANDARD => '{{name}} must be an IP address of Viet Nam'
25+
self::STANDARD => '{{name}} must be an IP address of Viet Nam',
26+
self::VERSION => '{{name}} must be an IP version {{version}}'
2227
],
2328
self::MODE_NEGATIVE => [
24-
self::STANDARD => '{{name}} must be an IP address of Viet Nam'
29+
self::STANDARD => '{{name}} must be an IP address of Viet Nam',
30+
self::VERSION => '{{name}} must be an IP version {{version}}'
2531
],
2632
];
2733

34+
public function chooseTemplate()
35+
{
36+
if (null !== $this->getParam('version')) {
37+
return static::VERSION;
38+
} else {
39+
return static::STANDARD;
40+
}
41+
}
2842
}

src/Rules/IdVN.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public static function pregFormat(): string
2020
$oldId = self::idPregFormatPart(true);
2121
$cId = self::cIdPregFormatPart();
2222

23-
return '~^(' . implode(')|(', [$id, $oldId, $cId]) . ')$~';
23+
return '~^((' . implode(')|(', [$id, $oldId, $cId]) . '))$~';
2424
}
2525

2626
private static function idPregFormatPart(bool $old): string

src/Rules/IpVN.php

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@
1818
*/
1919
class IpVN extends AbstractRule
2020
{
21+
const IPV4 = IpType::T_IPv4;
22+
23+
const IPV6 = IpType::T_IPv6;
24+
25+
public $version;
26+
27+
public function __construct($version = null)
28+
{
29+
$this->version = $version;
30+
}
2131

2232
public function validate($input)
2333
{
@@ -26,17 +36,22 @@ public function validate($input)
2636
return false;
2737
}
2838

29-
if (!$ranges = $this->getIpRanges($input, $ip->getAddressType())) {
39+
if (($version = $ip->getAddressType()) !== $this->version && null !== $this->version) {
40+
41+
return false;
42+
}
43+
44+
if (!$ranges = $this->getIpRanges($input, $version)) {
3045

3146
return false;
3247
}
3348

3449
return $this->validateIpInRange($ip, $ranges);
3550
}
3651

37-
protected function getIpRanges(string $ip, int $type): ?array
52+
protected function getIpRanges(string $ip, int $version): ?array
3853
{
39-
if ($type === IpType::T_IPv4) {
54+
if (self::IPV4 === $version) {
4055
$keys = explode('.', $ip);
4156
$map = static::getIpV4Range();
4257
} else {
@@ -78,9 +93,9 @@ protected function validateIpInRange(IpInterface $ip, array $ranges)
7893

7994
protected static function getIpV4Range(): array
8095
{
81-
static $range = [];
96+
static $range = null;
8297

83-
if ($range === null) {
98+
if (null === $range) {
8499
$range = require(__DIR__ . '/../../resource/ip-v4-range.php');
85100
}
86101

@@ -89,9 +104,9 @@ protected static function getIpV4Range(): array
89104

90105
protected static function getIpV6Range(): array
91106
{
92-
static $range = [];
107+
static $range = null;
93108

94-
if ($range === null) {
109+
if (null === $range) {
95110
$range = require(__DIR__ . '/../../resource/ip-v6-range.php');
96111
}
97112

tests/Rules/IdVNTest.php

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
/**
3+
* @link https://github.com/phpviet/validation
4+
* @copyright (c) PHP Viet
5+
* @license [MIT](https://opensource.org/licenses/MIT)
6+
*/
7+
8+
namespace PHPViet\Validation\Tests\Rules;
9+
10+
use PHPUnit\Framework\TestCase;
11+
use PHPViet\Validation\Rules\IdVN;
12+
13+
/**
14+
* @author Vuong Minh <vuongxuongminh@gmail.com>
15+
* @since 1.0.0
16+
*/
17+
class IdVNTest extends TestCase
18+
{
19+
/**
20+
* @var IdVN
21+
*/
22+
protected $validator;
23+
24+
protected function setUp()
25+
{
26+
$this->validator = new IdVN;
27+
}
28+
29+
/**
30+
* @dataProvider providerValid
31+
*/
32+
public function testValidShouldReturnTrue($input)
33+
{
34+
$this->assertTrue($this->validator->__invoke($input));
35+
$this->assertTrue($this->validator->assert($input));
36+
$this->assertTrue($this->validator->check($input));
37+
}
38+
39+
/**
40+
* @dataProvider providerInvalid
41+
*/
42+
public function testInvalidShouldThrowException($input)
43+
{
44+
$this->expectException('\PHPViet\Validation\Exceptions\IdVNException');
45+
$this->assertFalse($this->validator->__invoke($input));
46+
$this->assertFalse($this->validator->assert($input));
47+
}
48+
49+
public function providerValid()
50+
{
51+
return [
52+
['006487975'],
53+
['116487952'],
54+
['290649784'],
55+
['356497220'],
56+
['090649752'],
57+
['231649700'],
58+
['245697911'],
59+
['285649733'],
60+
['006448799975'],
61+
['116448722952'],
62+
['290646975584'],
63+
['356499722047'],
64+
['090764975265'],
65+
['231864970011'],
66+
['245069791197'],
67+
['285264973388'],
68+
['086497542199'],
69+
['106497006444'],
70+
['223131316790'],
71+
['316498797104'],
72+
['499797976410'],
73+
['526499998877'],
74+
['649879870111'],
75+
['776464646777'],
76+
['866646777797'],
77+
['964646497971']
78+
];
79+
}
80+
81+
public function providerInvalid()
82+
{
83+
return [
84+
['0064 87975'],
85+
['11648!7952'],
86+
['2906a49784'],
87+
['35649f7220'],
88+
['a231649700'],
89+
[' 245697911'],
90+
['@285649733'],
91+
['0964487975'],
92+
['3964997220'],
93+
['0967649752'],
94+
['2470697911'],
95+
['096497542199'],
96+
['439797976410'],
97+
['596499998877'],
98+
['699879870111'],
99+
['856646777797'],
100+
['994646497971']
101+
];
102+
}
103+
}

0 commit comments

Comments
 (0)