Passed
Push — master ( 3f9dde...47232d )
by Dāvis
04:45
created

Helper::variable()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 7
nc 6
nop 1
dl 0
loc 11
rs 8.8571
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 getUniqueId($length = 20)
59
    {
60
        try {
61
            $output = bin2hex(random_bytes($length));
62
        } catch (\Exception $exception) {
63
            $output = substr(str_shuffle(str_repeat($x = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', (int)ceil((int)($length / \strlen($x))))), 1, $length);
64
        }
65
66
        return $output;
67
    }
68
69
    public static function validatePersonCode($personCode = null)
70
    {
71
        if ($personCode !== null) {
72
            $personCode = str_replace('-', '', $personCode);
73
74
            if (\strlen($personCode) !== 11) {
75
                return 'error_length';
76
            }
77
78
            if (preg_match('/^\d+$/', $personCode) === null) {
79
                return 'error_symbols';
80
            }
81
82
            if (((int)substr($personCode, 0, 2) === 32 && !self::newPKValidate($personCode)) || !self::validateDate($personCode)) {
83
                return 'error_invalid';
84
            }
85
86
            return true;
87
        }
88
89
        return 'error_empty';
90
    }
91
92
    public static function validateDate($date)
93
    {
94
        $date = str_replace('-', '', $date);
95
        $day = (int)substr($date, 0, 2);
96
        $month = (int)substr($date, 2, 2);
97
98
        if ($month < 0 || $month > 12) {
99
            return false;
100
        }
101
        // @formatter:off
102
        $months = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
103
        // @formatter:on
104
        if ((int)substr($date, 4, 2) % 4 === 0) {
105
            $months[1] = 29;
106
        }
107
108
        return $day > 0 && $day <= $months[$month - 1];
109
    }
110
111
    public static function newPKValidate($personCode)
112
    {
113
        $personCode = str_replace('-', '', $personCode);
114
115
        // @formatter:off
116
        $calculations = [1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
117
        // @formatter:on
118
119
        $sum = 0;
120
        foreach ($calculations as $key => $calculation) {
121
            $sum += ($personCode[$key] * $calculation);
122
        }
123
124
        $remainder = $sum % 11;
125
126
        if (1 - $remainder < -1) {
127
            return $personCode[10] === (1 - $remainder + 11);
128
        }
129
130
        return $personCode[10] === (1 - $remainder);
131
    }
132
133
    public static function swap(&$foo, &$bar)
134
    {
135
        $tmp = $foo;
136
        $foo = $bar;
137
        $bar = $tmp;
138
    }
139
140
    public static function removeDuplicates(&$array)
141
    {
142
        $array = array_map('unserialize', array_unique(array_map('serialize', $array)));
143
    }
144
145
    public static function cleanText($text)
146
    {
147
        return html_entity_decode(self::oneSpace(str_replace(' ?', '', mb_convert_encoding(strip_tags($text), 'UTF-8', 'UTF-8'))));
148
    }
149
150
    public static function oneSpace($text)
151
    {
152
        return preg_replace('/\s+/S', ' ', $text);
153
    }
154
155
    /**
156
     * Transliterates cyrillic text to latin.
157
     *
158
     * @param  string $text cyrillic text
159
     *
160
     * @return string latin text
161
     */
162
    public static function translit2($text)
163
    {
164
        return str_replace(self::$cyrMap, self::$latMap, $text);
165
    }
166
167
    /**
168
     * Transliterates latin text to cyrillic.
169
     *
170
     * @param  string $text latin text
171
     *
172
     * @return string cyrillic text
173
     */
174
    public static function translit4($text)
175
    {
176
        return str_replace(self::$latMap, self::$cyrMap, $text);
177
    }
178
179
    public static function multiple(array $keys)
180
    {
181
        foreach ($keys as $key) {
182
            if (!\is_array($key)) {
183
                return false;
184
            }
185
        }
186
187
        return true;
188
    }
189
190
    public static function multiset(array $keys)
191
    {
192
        foreach ($keys as $key) {
193
            if ($key === null) {
194
                return false;
195
            }
196
        }
197
198
        return true;
199
    }
200
}
201