IdVN::cIdPregFormatPart()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
c 0
b 0
f 0
rs 9.7
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * @link https://github.com/phpviet/validation
4
 *
5
 * @copyright (c) PHP Viet
6
 * @license [MIT](https://opensource.org/licenses/MIT)
7
 */
8
9
namespace PHPViet\Validation\Rules;
10
11
/**
12
 * @author Vuong Minh <[email protected]>
13
 * @since 1.0.0
14
 */
15
class IdVN extends AbstractStaticRegexRule
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];
0 ignored issues
show
Bug introduced by
The variable $range1 does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $range2 does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
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}', [
55
            '::head::' => implode('|', [
56
                '0[012468]',
57
                '1[0124579]',
58
                '2[024-7]',
59
                '3[013-8]',
60
                '4[0245689]',
61
                '5[12468]',
62
                '6[024678]',
63
                '7[024579]',
64
                '8[0234679]',
65
                '9[1-6]',
66
            ]),
67
        ]);
68
    }
69
}
70