Passed
Push — master ( d093e3...81fa39 )
by Dāvis
03:13 queued 25s
created

Helper::validatePersonCode()   C

Complexity

Conditions 9
Paths 8

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 13
nc 8
nop 1
dl 0
loc 24
rs 5.3563
c 0
b 0
f 0
1
<?php
2
3
namespace Sludio\HelperBundle\Script\Utils;
4
5
class Helper
6
{
7
    // @formatter:off
8
    /**
9
     * Cyrillic mapping.
10
     *
11
     * @var array
12
     */
13
    protected static $cyrMap = array(
14
        'е', 'ё', 'ж', 'х', 'ц', 'ч', 'ш', 'щ', 'ю', 'я',
15
        'Е', 'Ё', 'Ж', 'Х', 'Ц', 'Ч', 'Ш', 'Щ', 'Ю', 'Я',
16
        'а', 'б', 'в', 'г', 'д', 'з', 'и', 'й', 'к', 'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'ъ', 'ы', 'ь', 'э',
17
        'А', 'Б', 'В', 'Г', 'Д', 'З', 'И', 'Й', 'К', 'Л', 'М', 'Н', 'О', 'П', 'Р', 'С', 'Т', 'У', 'Ф', 'Ъ', 'Ы', 'Ь', 'Э'
18
    );
19
20
    /**
21
     * Latin mapping.
22
     *
23
     * @var array
24
     */
25
    protected static $latMap = array(
26
        'ye', 'ye', 'zh', 'kh', 'ts', 'ch', 'sh', 'shch', 'yu', 'ya',
27
        'Ye', 'Ye', 'Zh', 'Kh', 'Ts', 'Ch', 'Sh', 'Shch', 'Yu', 'Ya',
28
        'a', 'b', 'v', 'g', 'd', 'z', 'i', 'y', 'k', 'l', 'm', 'n', 'o', 'p', 'r', 's', 't', 'u', 'f', 'ʺ', 'y', '–', 'e',
29
        'A', 'B', 'V', 'G', 'D', 'Z', 'I', 'Y', 'K', 'L', 'M', 'N', 'O', 'P', 'R', 'S', 'T', 'U', 'F', 'ʺ', 'Y', '–', 'E'
30
    );
31
    // @formatter:on
32
33
    public static function toCamelCase($string)
34
    {
35
        return preg_replace('~\s+~', '', lcfirst(ucwords(str_replace('_', ' ', $string))));
36
    }
37
38
    public static function fromCamelCase($string, $separator = '_')
39
    {
40
        return strtolower(preg_replace('/(?!^)[[:upper:]]+/', $separator.'$0', $string));
41
    }
42
43
    public static function isEmpty($variable)
44
    {
45
        $result = true;
46
47
        if (is_array($variable) && count($variable) > 0) {
48
            foreach ($variable as $value) {
49
                $result = $result && self::isEmpty($value);
50
            }
51
        } else {
52
            $result = empty($variable);
53
        }
54
55
        return $result;
56
    }
57
58
    public static function variable(&$value)
59
    {
60
        if ($value instanceof \DateTime) {
61
            $value = "'".addslashes(trim($value->format('Y-m-d H:i:s')))."'";
62
        } elseif (!is_numeric($value)) {
63
            $value = "'".addslashes(trim($value))."'";
64
        }
65
66
        $trim = trim($value);
67
        if ($trim === '' || $trim === "''") {
68
            $value = null;
69
        }
70
    }
71
72
    public static function getUniqueId($length = 20)
73
    {
74
        try {
75
            $output = bin2hex(random_bytes($length));
76
        } catch (\Exception $exception) {
77
            $output = substr(str_shuffle(str_repeat($x = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', ceil($length / strlen($x)))), 1, $length);
0 ignored issues
show
Bug introduced by
ceil($length / strlen($x)) of type double is incompatible with the type integer expected by parameter $multiplier of str_repeat(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

77
            $output = substr(str_shuffle(str_repeat($x = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', /** @scrutinizer ignore-type */ ceil($length / strlen($x)))), 1, $length);
Loading history...
78
        }
79
80
        return $output;
81
    }
82
83
    public static function validateDate($date)
84
    {
85
        $date = str_replace('-', '', $date);
86
        $day = (int)substr($date, 0, 2);
87
        $month = (int)substr($date, 2, 2);
88
89
        if ($month < 0 || $month > 12) {
90
            return false;
91
        }
92
        // @formatter:off
93
        $months = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
94
        // @formatter:on
95
        if ((int)substr($date, 4, 2) % 4 === 0) {
96
            $months[1] = 29;
97
        }
98
99
        return $day > 0 && $day <= $months[$month - 1];
100
    }
101
102
    public static function newPKValidate($personCode)
103
    {
104
        $personCode = str_replace('-', '', $personCode);
105
106
        // @formatter:off
107
        $calculations = [1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
108
        // @formatter:on
109
110
        $sum = 0;
111
        foreach($calculations as $key => $calculation){
112
            $sum += ($personCode[$key] * $calculation);
113
        }
114
115
        $remainder = $sum % 11;
116
117
        if (1 - $remainder < -1) {
118
            return $personCode[10] === (1 - $remainder + 11);
119
        }
120
121
        return $personCode[10] === (1 - $remainder);
122
    }
123
124
    public static function validatePersonCode($personCode = null)
125
    {
126
        if ($personCode !== null) {
127
            $personCode = str_replace('-', '', $personCode);
128
129
            if (strlen($personCode) !== 11) {
130
                return 'error_length';
131
            }
132
            if (preg_match('/^[0-9]+$/', $personCode) === null) {
133
                return 'error_symbols';
134
            }
135
            if ((int)substr($personCode, 0, 2) < 32) {
136
                if (!self::validateDate($personCode)) {
137
                    return 'error_invalid';
138
                }
139
            }
140
            if ((int)substr($personCode, 0, 2) > 32 || ((int)substr($personCode, 0, 2) === 32 && !self::newPKValidate($personCode))) {
141
                return 'error_invalid';
142
            }
143
144
            return true;
145
        }
146
147
        return 'error_empty';
148
    }
149
150
    public static function swap(&$foo, &$bar)
151
    {
152
        $tmp = $foo;
153
        $foo = $bar;
154
        $bar = $tmp;
155
    }
156
157
    public static function removeDuplicates(&$array)
158
    {
159
        $array = array_map('unserialize', array_unique(array_map('serialize', $array)));
160
    }
161
162
    public static function cleanText($text)
163
    {
164
        return html_entity_decode(self::oneSpace(str_replace(' ?', '', mb_convert_encoding(strip_tags($text), 'UTF-8', 'UTF-8'))));
165
    }
166
167
    public static function oneSpace($text)
168
    {
169
        return preg_replace('/\s+/S', ' ', $text);
170
    }
171
172
    /**
173
     * Transliterates cyrillic text to latin.
174
     *
175
     * @param  string $text cyrillic text
176
     *
177
     * @return string latin text
178
     */
179
    public static function translit2($text)
180
    {
181
        return str_replace(self::$cyrMap, self::$latMap, $text);
182
    }
183
184
    /**
185
     * Transliterates latin text to cyrillic.
186
     *
187
     * @param  string $text latin text
188
     *
189
     * @return string cyrillic text
190
     */
191
    public static function translit4($text)
192
    {
193
        return str_replace(self::$latMap, self::$cyrMap, $text);
194
    }
195
196
    public static function multiple(array $keys)
197
    {
198
        foreach ($keys as $key) {
199
            if (!is_array($key)) {
200
                return false;
201
            }
202
        }
203
204
        return true;
205
    }
206
207
    public static function multiset(array $keys)
208
    {
209
        foreach ($keys as $key) {
210
            if ($key === null) {
211
                return false;
212
            }
213
        }
214
215
        return true;
216
    }
217
}
218