|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sludio\HelperBundle\Script\Utils; |
|
4
|
|
|
|
|
5
|
|
|
class Helper |
|
6
|
|
|
{ |
|
7
|
|
|
public static function toCamelCase($string) |
|
8
|
|
|
{ |
|
9
|
|
|
return preg_replace('~\s+~', '', lcfirst(ucwords(strtr($string, '_', ' ')))); |
|
10
|
|
|
} |
|
11
|
|
|
|
|
12
|
|
|
public static function fromCamelCase($string, $separator = '_') |
|
13
|
|
|
{ |
|
14
|
|
|
return strtolower(preg_replace('/(?!^)[[:upper:]]+/', $separator.'$0', $string)); |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
public static function isEmpty($variable) |
|
18
|
|
|
{ |
|
19
|
|
|
$result = true; |
|
20
|
|
|
|
|
21
|
|
|
if (is_array($variable) && count($variable) > 0) { |
|
22
|
|
|
foreach ($variable as $value) { |
|
23
|
|
|
$result = $result && self::isEmpty($value); |
|
24
|
|
|
} |
|
25
|
|
|
} else { |
|
26
|
|
|
$result = empty($variable); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
return $result; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public static function variable(&$value) |
|
33
|
|
|
{ |
|
34
|
|
|
if ($value instanceof \DateTime) { |
|
35
|
|
|
$value = "'".addslashes(trim($value->format('Y-m-d H:i:s')))."'"; |
|
36
|
|
|
} elseif (!is_numeric($value)) { |
|
37
|
|
|
$value = "'".addslashes(trim($value))."'"; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
if (trim($value) === '' || trim($value) === "''") { |
|
41
|
|
|
$value = null; |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public static function getUniqueId($length = 20) |
|
46
|
|
|
{ |
|
47
|
|
|
return bin2hex(openssl_random_pseudo_bytes($length)); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public static function validateDate($date) |
|
51
|
|
|
{ |
|
52
|
|
|
$date = str_replace('-', '', $date); |
|
53
|
|
|
$day = intval(substr($date, 0, 2)); |
|
54
|
|
|
$month = intval(substr($date, 2, 2)); |
|
55
|
|
|
$year = intval(substr($date, 4, 2)); |
|
56
|
|
|
|
|
57
|
|
|
if ($month < 0 || $month > 12) { |
|
58
|
|
|
return false; |
|
59
|
|
|
} |
|
60
|
|
|
// @formatter:off |
|
61
|
|
|
$months = [31,28,31,30,31,30,31,31,30,31,30,31]; |
|
62
|
|
|
// @formatter:on |
|
63
|
|
|
if ($year % 4 === 0) { |
|
64
|
|
|
$months[1] = 29; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return $day > 0 && $day <= $months[$month - 1]; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public static function newPKValidate($personCode) |
|
71
|
|
|
{ |
|
72
|
|
|
$personCode = str_replace('-', '', $personCode); |
|
73
|
|
|
// @formatter:off |
|
74
|
|
|
$sum = |
|
75
|
|
|
(substr($personCode, 0, 1) * 1) + |
|
76
|
|
|
(substr($personCode, 1, 1) * 6) + |
|
77
|
|
|
(substr($personCode, 2, 1) * 3) + |
|
78
|
|
|
(substr($personCode, 3, 1) * 7) + |
|
79
|
|
|
(substr($personCode, 4, 1) * 9) + |
|
80
|
|
|
(substr($personCode, 5, 1) * 10) + |
|
81
|
|
|
(substr($personCode, 6, 1) * 5) + |
|
82
|
|
|
(substr($personCode, 7, 1) * 8) + |
|
83
|
|
|
(substr($personCode, 8, 1) * 4) + |
|
84
|
|
|
(substr($personCode, 9, 1) * 2); |
|
85
|
|
|
// @formatter:on |
|
86
|
|
|
|
|
87
|
|
|
$remainder = $sum % 11; |
|
88
|
|
|
|
|
89
|
|
|
if (1 - $remainder < -1) { |
|
90
|
|
|
return substr($personCode, 10, 1) == (1 - $remainder + 11); |
|
91
|
|
|
} else { |
|
92
|
|
|
return substr($personCode, 10, 1) == (1 - $remainder); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public static function validatePersonCode($personCode = null) |
|
97
|
|
|
{ |
|
98
|
|
|
if ($personCode) { |
|
99
|
|
|
$personCode = str_replace('-', '', $personCode); |
|
100
|
|
|
if (strlen($personCode) !== 11) { |
|
101
|
|
|
return 'error_length'; |
|
102
|
|
|
} |
|
103
|
|
|
if (preg_match("/^[0-9]+$/", $personCode) === null) { |
|
104
|
|
|
return 'error_symbols'; |
|
105
|
|
|
} |
|
106
|
|
|
if (intval(substr($personCode, 0, 2)) < 32) { |
|
107
|
|
|
if (!self::validateDate($personCode)) { |
|
108
|
|
|
return 'error_invalid'; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
if (intval(substr($personCode, 0, 2)) > 32 || (intval(substr($personCode, 0, 2)) === 32 && !self::newPKValidate($personCode))) { |
|
112
|
|
|
return 'error_invalid'; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
return true; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
return 'error_empty'; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
public static function swap(&$x, &$y) |
|
|
|
|
|
|
122
|
|
|
{ |
|
123
|
|
|
$tmp = $x; |
|
124
|
|
|
$x = $y; |
|
125
|
|
|
$y = $tmp; |
|
126
|
|
|
} |
|
127
|
|
|
} |
Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.