Passed
Push — master ( 55c7d5...aed71d )
by Dāvis
03:01
created

Helper::variable()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 6
nc 6
nop 1
dl 0
loc 10
rs 8.8571
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
    private static function subDate($date, $from, $to)
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $to. Configured minimum length is 3.

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.

Loading history...
51
    {
52
        return intval(substr($date, $from, $to));
53
    }
54
55
    public static function validateDate($date)
56
    {
57
        $date = str_replace('-', '', $date);
58
        $day = $month = $year = null;
59
60
        // @formatter:off
61
        $list = [
62
            'day' => [0, 2],
63
            'month' => [2, 2],
64
            'year' => [4, 2]
65
        ];
66
        // @formatter:on
67
68
        foreach ($list as $variable => $nums) {
69
            $$variable = self::subDate($date, $nums[0], $nums[1]);
70
        }
71
72
        if ($month === 0 || $month > 12) {
73
            return false;
74
        }
75
        // @formatter:off
76
        $months = [31,28,31,30,31,30,31,31,30,31,30,31];
77
        // @formatter:on
78
        if ($year % 4 === 0) {
79
            $months[1] = 29;
80
        }
81
82
        return $day > 0 && $day <= $months[$month - 1];
83
    }
84
85
    public static function newPKValidate($personCode)
86
    {
87
        $personCode = str_replace('-', '', $personCode);
88
        // @formatter:off
89
        $sum =
90
            (substr($personCode, 0, 1) * 1)  +
91
            (substr($personCode, 1, 1) * 6)  +
92
            (substr($personCode, 2, 1) * 3)  +
93
            (substr($personCode, 3, 1) * 7)  +
94
            (substr($personCode, 4, 1) * 9)  +
95
            (substr($personCode, 5, 1) * 10) +
96
            (substr($personCode, 6, 1) * 5)  +
97
            (substr($personCode, 7, 1) * 8)  +
98
            (substr($personCode, 8, 1) * 4)  +
99
            (substr($personCode, 9, 1) * 2);
100
        // @formatter:on
101
102
        $remainder = $sum % 11;
103
104
        if (1 - $remainder < -1) {
105
            return substr($personCode, 10, 1) == (1 - $remainder + 11);
106
        } else {
107
            return substr($personCode, 10, 1) == (1 - $remainder);
108
        }
109
    }
110
111
    public static function validatePersonCode($personCode = null)
112
    {
113
        if ($personCode) {
114
            $personCode = str_replace('-', '', $personCode);
115
            if (strlen($personCode) !== 11) {
116
                return 'error_length';
117
            }
118
            if (preg_match("/^[0-9]+$/", $personCode) === null) {
119
                return 'error_symbols';
120
            }
121
            if (intval(substr($personCode, 0, 2)) < 32) {
122
                if (!self::validateDate($personCode)) {
123
                    return 'error_invalid';
124
                }
125
            }
126
            if (intval(substr($personCode, 0, 2)) > 32 || (intval(substr($personCode, 0, 2)) === 32 && !self::newPKValidate($personCode))) {
127
                return 'error_invalid';
128
            }
129
130
            return true;
131
        }
132
133
        return 'error_empty';
134
    }
135
}