Skip to content

Commit c8ad7cf

Browse files
[WIP] Prepare id & phone number validators.
1 parent 1dd7582 commit c8ad7cf

File tree

5 files changed

+186
-0
lines changed

5 files changed

+186
-0
lines changed

src/Validator.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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;
9+
10+
use Respect\Validation\Validator as BaseValidator;
11+
use Respect\Validation\Factory;
12+
13+
/**
14+
* @author Vuong Minh <vuongxuongminh@gmail.com>
15+
* @since 1.0.0
16+
*/
17+
class Validator extends BaseValidator
18+
{
19+
20+
/**
21+
* @return Factory
22+
*/
23+
protected static function getFactory()
24+
{
25+
if (!static::$factory instanceof Factory) {
26+
$factory = static::$factory = new Factory();
27+
$factory->prependRulePrefix('\\PHPViet\\Validation\\Rules\\');
28+
}
29+
30+
return static::$factory;
31+
}
32+
33+
}

src/rules/AbstractStaticRegexRule.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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\rules;
9+
10+
use Respect\Validation\Rules\AbstractRegexRule;
11+
12+
/**
13+
* @author Vuong Minh <vuongxuongminh@gmail.com>
14+
* @since 1.0.0
15+
*/
16+
abstract class AbstractStaticRegexRule extends AbstractRegexRule
17+
{
18+
19+
protected function getPregFormat()
20+
{
21+
return static::pregFormat();
22+
}
23+
24+
abstract public static function pregFormat(): string;
25+
26+
}

src/rules/IdVN.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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\rules;
9+
10+
/**
11+
* @author Vuong Minh <vuongxuongminh@gmail.com>
12+
* @since 1.0.0
13+
*/
14+
class IdVN extends AbstractStaticRegexRule
15+
{
16+
17+
public static function pregFormat(): string
18+
{
19+
$id = self::idPregFormatPart(false);
20+
$oldId = self::idPregFormatPart(true);
21+
$cId = self::cIdPregFormatPart();
22+
23+
return '~^(' . implode(')|(', [$id, $oldId, $cId]) . ')$~';
24+
}
25+
26+
private static function idPregFormatPart(bool $old): string
27+
{
28+
if ($old) {
29+
[$range1, $range2] = [7, 6];
30+
} else {
31+
[$range1, $range2] = [10, 9];
32+
}
33+
34+
return strtr('((::head1::)\d{::range1::})|((::head2::)\d{::range2::})', [
35+
'::head1::' => implode('|', [
36+
'0[0-8]',
37+
'1[0-9]',
38+
'2[0-9]',
39+
'3[0-8]'
40+
]),
41+
'::head2::' => implode('|', [
42+
'09[015]',
43+
'23[01]',
44+
'245',
45+
'28[015]'
46+
]),
47+
'range1' => $range1,
48+
'range2' => $range2
49+
]);
50+
}
51+
52+
private static function cIdPregFormatPart(): string
53+
{
54+
return strtr('(::head::)\d{10}', '::head::', implode('|', [
55+
'0[012468]',
56+
'1[0124579]',
57+
'2[024-7]',
58+
'3[013-8]',
59+
'4[0245689]',
60+
'5[12468]',
61+
'6[024678]',
62+
'7[024579]',
63+
'8[0234679]',
64+
'9[1-6]'
65+
]));
66+
}
67+
}

src/rules/LandLineVN.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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\rules;
9+
10+
/**
11+
* @author Vuong Minh <vuongxuongminh@gmail.com>
12+
* @since 1.0.0
13+
*/
14+
class LandLineVN extends AbstractStaticRegexRule
15+
{
16+
17+
public static function pregFormat(): string
18+
{
19+
return strtr('/^(\+?84|0)(::head::)\d{7}$/', '::head::', implode('|', [
20+
'20[3-9]',
21+
'21[0-689]',
22+
'22[0-25-9]',
23+
'23[2-9]',
24+
'24[0-9]',
25+
'25[124-9]',
26+
'26[0-39]',
27+
'27[0-7]',
28+
'28[0-9]',
29+
'29[0-4679]'
30+
]));
31+
}
32+
}

src/rules/MobileVN.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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\rules;
9+
10+
/**
11+
* @author Vuong Minh <vuongxuongminh@gmail.com>
12+
* @since 1.0.0
13+
*/
14+
class MobileVN extends AbstractStaticRegexRule
15+
{
16+
17+
public static function pregFormat(): string
18+
{
19+
return strtr('/^(\+?84|0)(::head::)\d{7}$/', '::head::', implode('|', [
20+
'3[2-9]',
21+
'5[2689]',
22+
'7(0|[6-9])',
23+
'8[1-9]',
24+
'9[0-9]',
25+
]));
26+
}
27+
28+
}

0 commit comments

Comments
 (0)