1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sludio\HelperBundle\Script\Utils; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\HttpFoundation\Request; |
6
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
7
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
8
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
9
|
|
|
|
10
|
|
|
class Helper |
11
|
|
|
{ |
12
|
|
|
// @formatter:off |
13
|
|
|
/** |
14
|
|
|
* Cyrillic mapping. |
15
|
|
|
* |
16
|
|
|
* @var array |
17
|
|
|
*/ |
18
|
|
|
protected static $cyrMap = array( |
19
|
|
|
'е', 'ё', 'ж', 'х', 'ц', 'ч', 'ш', 'щ', 'ю', 'я', |
20
|
|
|
'Е', 'Ё', 'Ж', 'Х', 'Ц', 'Ч', 'Ш', 'Щ', 'Ю', 'Я', |
21
|
|
|
'а', 'б', 'в', 'г', 'д', 'з', 'и', 'й', 'к', 'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'ъ', 'ы', 'ь', 'э', |
22
|
|
|
'А', 'Б', 'В', 'Г', 'Д', 'З', 'И', 'Й', 'К', 'Л', 'М', 'Н', 'О', 'П', 'Р', 'С', 'Т', 'У', 'Ф', 'Ъ', 'Ы', 'Ь', 'Э' |
23
|
|
|
); |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Latin mapping. |
27
|
|
|
* |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
protected static $latMap = array( |
31
|
|
|
'ye', 'ye', 'zh', 'kh', 'ts', 'ch', 'sh', 'shch', 'yu', 'ya', |
32
|
|
|
'Ye', 'Ye', 'Zh', 'Kh', 'Ts', 'Ch', 'Sh', 'Shch', 'Yu', 'Ya', |
33
|
|
|
'a', 'b', 'v', 'g', 'd', 'z', 'i', 'y', 'k', 'l', 'm', 'n', 'o', 'p', 'r', 's', 't', 'u', 'f', 'ʺ', 'y', '–', 'e', |
34
|
|
|
'A', 'B', 'V', 'G', 'D', 'Z', 'I', 'Y', 'K', 'L', 'M', 'N', 'O', 'P', 'R', 'S', 'T', 'U', 'F', 'ʺ', 'Y', '–', 'E' |
35
|
|
|
); |
36
|
|
|
// @formatter:on |
37
|
|
|
|
38
|
|
|
public static function toCamelCase($string) |
39
|
|
|
{ |
40
|
|
|
return preg_replace('~\s+~', '', lcfirst(ucwords(str_replace('_', ' ', $string)))); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public static function fromCamelCase($string, $separator = '_') |
44
|
|
|
{ |
45
|
|
|
return strtolower(preg_replace('/(?!^)[[:upper:]]+/', $separator.'$0', $string)); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public static function isEmpty($variable) |
49
|
|
|
{ |
50
|
|
|
$result = true; |
51
|
|
|
|
52
|
|
|
if (\is_array($variable) && \count($variable) > 0) { |
53
|
|
|
foreach ($variable as $value) { |
54
|
|
|
$result = $result && self::isEmpty($value); |
55
|
|
|
} |
56
|
|
|
} else { |
57
|
|
|
$result = empty($variable); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $result; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public static function getUniqueId($length = 20) |
64
|
|
|
{ |
65
|
|
|
try { |
66
|
|
|
$output = bin2hex(random_bytes($length)); |
67
|
|
|
} catch (\Exception $exception) { |
68
|
|
|
$output = substr(str_shuffle(str_repeat($x = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', (int)ceil((int)($length / \strlen($x))))), 1, $length); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $output; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public static function validatePersonCode($personCode) |
75
|
|
|
{ |
76
|
|
|
$personCode = str_replace('-', '', $personCode); |
77
|
|
|
$result = true; |
78
|
|
|
|
79
|
|
|
if (\strlen($personCode) !== 11 || preg_match('/^\d+$/', $personCode) === null) { |
80
|
|
|
$result = false; |
81
|
|
|
} elseif (((int)substr($personCode, 0, 2) === 32 && !self::newPKValidate($personCode)) || !self::validateDate($personCode)) { |
82
|
|
|
$result = false; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $result; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public static function validateDate($date) |
89
|
|
|
{ |
90
|
|
|
$date = str_replace('-', '', $date); |
91
|
|
|
$day = (int)substr($date, 0, 2); |
92
|
|
|
$month = (int)substr($date, 2, 2); |
93
|
|
|
|
94
|
|
|
if ($month < 0 || $month > 12) { |
95
|
|
|
return false; |
96
|
|
|
} |
97
|
|
|
// @formatter:off |
98
|
|
|
$months = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; |
99
|
|
|
// @formatter:on |
100
|
|
|
if ((int)substr($date, 4, 2) % 4 === 0) { |
101
|
|
|
$months[1] = 29; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $day > 0 && $day <= $months[$month - 1]; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public static function validateDate2($date, $format = 'd-m-Y H:i:s') |
108
|
|
|
{ |
109
|
|
|
$object = \DateTime::createFromFormat($format, $date); |
110
|
|
|
|
111
|
|
|
return $object && $object->format($format) === $date; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public static function excelDate($timestamp, $format = 'd-m-Y H:i:s') |
115
|
|
|
{ |
116
|
|
|
$base = 25569; |
117
|
|
|
if ($timestamp >= $base) { |
118
|
|
|
$unix = ($timestamp - $base) * 86400; |
119
|
|
|
$date = gmdate($format, $unix); |
120
|
|
|
if (self::validateDate2($date, $format)) { |
121
|
|
|
return $date; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return $timestamp; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public static function newPKValidate($personCode) |
129
|
|
|
{ |
130
|
|
|
$personCode = str_replace('-', '', $personCode); |
131
|
|
|
|
132
|
|
|
// @formatter:off |
133
|
|
|
$calculations = [1, 6, 3, 7, 9, 10, 5, 8, 4, 2]; |
134
|
|
|
// @formatter:on |
135
|
|
|
|
136
|
|
|
$sum = 0; |
137
|
|
|
foreach ($calculations as $key => $calculation) { |
138
|
|
|
$sum += ($personCode[$key] * $calculation); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$remainder = $sum % 11; |
142
|
|
|
|
143
|
|
|
if (1 - $remainder < -1) { |
144
|
|
|
return $personCode[10] === (1 - $remainder + 11); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return $personCode[10] === (1 - $remainder); |
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
|
|
|
public static function useHttps(Request $request) |
219
|
|
|
{ |
220
|
|
|
return $request->server->get('HTTPS') || ($request->server->get('HTTP_X_FORWARDED_PROTO') && $request->server->get('HTTP_X_FORWARDED_PROTO') === 'https'); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
public static function initialize(array $arguments, ContainerAwareCommand $command) |
224
|
|
|
{ |
225
|
|
|
/** @var InputInterface $input */ |
226
|
|
|
/** @var OutputInterface $output */ |
227
|
|
|
list($params, $input, $output) = $arguments; |
228
|
|
|
|
229
|
|
|
foreach ($params as $param => $check) { |
230
|
|
|
$command->{$param} = $input->getOption($param); |
231
|
|
|
if (!$command->{$param}) { |
232
|
|
|
$output->writeln("Please provide --$param parameter!"); |
233
|
|
|
|
234
|
|
|
return false; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
if (!$check($command->{$param})) { |
238
|
|
|
$output->writeln("Incorrect input in --$param parameter!"); |
239
|
|
|
|
240
|
|
|
return false; |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
return true; |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
|