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