Passed
Push — master ( 21e5db...8fd11f )
by Dāvis
03:24
created

Helper::toCamelCase()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
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(strtr($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
        if (trim($value) === '' || trim($value) === "''") {
67
            $value = null;
68
        }
69
    }
70
71
    public static function getUniqueId($length = 20)
72
    {
73
        return bin2hex(openssl_random_pseudo_bytes($length));
74
    }
75
76
    public static function validateDate($date)
77
    {
78
        $date = str_replace('-', '', $date);
79
        $day = intval(substr($date, 0, 2));
80
        $month = intval(substr($date, 2, 2));
81
        $year = intval(substr($date, 4, 2));
82
83
        if ($month < 0 || $month > 12) {
84
            return false;
85
        }
86
        // @formatter:off
87
        $months = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
88
        // @formatter:on
89
        if ($year % 4 === 0) {
90
            $months[1] = 29;
91
        }
92
93
        return $day > 0 && $day <= $months[$month - 1];
94
    }
95
96
    public static function newPKValidate($personCode)
97
    {
98
        $personCode = str_replace('-', '', $personCode);
99
        // @formatter:off
100
        $sum =
101
            (substr($personCode, 0, 1) * 1) +
102
            (substr($personCode, 1, 1) * 6) +
103
            (substr($personCode, 2, 1) * 3) +
104
            (substr($personCode, 3, 1) * 7) +
105
            (substr($personCode, 4, 1) * 9) +
106
            (substr($personCode, 5, 1) * 10) +
107
            (substr($personCode, 6, 1) * 5) +
108
            (substr($personCode, 7, 1) * 8) +
109
            (substr($personCode, 8, 1) * 4) +
110
            (substr($personCode, 9, 1) * 2);
111
        // @formatter:on
112
113
        $remainder = $sum % 11;
114
115
        if (1 - $remainder < -1) {
116
            return substr($personCode, 10, 1) == (1 - $remainder + 11);
117
        } else {
118
            return substr($personCode, 10, 1) == (1 - $remainder);
119
        }
120
    }
121
122
    public static function validatePersonCode($personCode = null)
123
    {
124
        if ($personCode) {
125
            $personCode = str_replace('-', '', $personCode);
126
            if (strlen($personCode) !== 11) {
127
                return 'error_length';
128
            }
129
            if (preg_match("/^[0-9]+$/", $personCode) === null) {
130
                return 'error_symbols';
131
            }
132
            if (intval(substr($personCode, 0, 2)) < 32) {
133
                if (!self::validateDate($personCode)) {
134
                    return 'error_invalid';
135
                }
136
            }
137
            if (intval(substr($personCode, 0, 2)) > 32 || (intval(substr($personCode, 0, 2)) === 32 && !self::newPKValidate($personCode))) {
138
                return 'error_invalid';
139
            }
140
141
            return true;
142
        }
143
144
        return 'error_empty';
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
        $functions = [
162
            'strip_tags',
163
            'mb_convert_encoding' => [
164
                'variable' => 0,
165
                'params' => [
166
                    null,
167
                    'UTF-8',
168
                    'UTF-8',
169
                ],
170
            ],
171
            'str_replace' => [
172
                'variable' => 2,
173
                'params' => [
174
                    ' ?',
175
                    '',
176
                    null,
177
                ],
178
            ],
179
            'self::oneSpace',
180
            'html_entity_decode',
181
        ];
182
183
        foreach ($functions as $key => $function) {
184
            if (is_numeric($key)) {
185
                $key = $function;
186
                $function = is_array($function) ? array_values($function) : $function;
187
            }
188
            $params = isset($function['params']) ? $function['params'] : [];
189
            $variable = isset($function['variable']) ? intval($function['variable']) : 0;
190
            $params[$variable] = $text;
191
            $text = call_user_func_array($key, $params);
192
        }
193
194
        return $text;
195
    }
196
197
    public static function oneSpace($string)
198
    {
199
        return preg_replace('/\s+/S', ' ', $string);
200
    }
201
202
    /**
203
     * Transliterates cyrillic text to latin.
204
     *
205
     * @param  string $text cyrillic text
206
     *
207
     * @return string latin text
208
     */
209
    public static function translit2($text)
210
    {
211
        return str_replace(self::$cyrMap, self::$latMap, $text);
212
    }
213
214
    /**
215
     * Transliterates latin text to cyrillic.
216
     *
217
     * @param  string $text latin text
218
     *
219
     * @return string cyrillic text
220
     */
221
    public static function translit4($text)
222
    {
223
        return str_replace(self::$latMap, self::$cyrMap, $text);
224
    }
225
}