Passed
Push — master ( 06020c...274f20 )
by Dāvis
03:41
created

Helper::validatePersonCode()   C

Complexity

Conditions 9
Paths 8

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 13
nc 8
nop 1
dl 0
loc 23
rs 5.8541
c 0
b 0
f 0
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(&$foo, &$bar)
122
    {
123
        $tmp = $foo;
124
        $foo = $bar;
125
        $bar = $tmp;
126
    }
127
128
    public static function removeDuplicates(&$array)
129
    {
130
        $array = array_map('unserialize', array_unique(array_map('serialize', $array)));
131
    }
132
133
    public static function cleanText($text)
134
    {
135
        $functions = [
136
            'strip_tags',
137
            'mb_convert_encoding' => [
138
                'variable' => 0,
139
                'params' => [
140
                    null,
141
                    "UTF-8",
142
                    "UTF-8",
143
                ],
144
            ],
145
            'str_replace' => [
146
                'variable' => 2,
147
                'params' => [
148
                    ' ?',
149
                    '',
150
                    null,
151
                ],
152
            ],
153
            'self::oneSpace',
154
            'html_entity_decode',
155
        ];
156
157
        foreach ($functions as $key => $function) {
158
            if(is_numeric($key)){
159
                $key = $function;
160
                $function = array_values($function);
0 ignored issues
show
Bug introduced by
It seems like $function can also be of type string; however, parameter $input of array_values() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

160
                $function = array_values(/** @scrutinizer ignore-type */ $function);
Loading history...
161
            }
162
            $params = isset($function['params']) ? $function['params'] : [];
163
            $variable = isset($function['variable']) ? intval($function['variable']) : 0;
164
            $params[$variable] = $text;
165
            $text = call_user_func_array($key, $params);
166
        }
167
168
        return $text;
169
    }
170
171
    public static function oneSpace($string)
172
    {
173
        return preg_replace('/\s+/S', ' ', $string);
174
    }
175
}