helpers.php ➔ string()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Josh\Faker;
4
5
if(! function_exists('string')){
6
    /**
7
     * Return string of variable
8
     *
9
     * @author Alireza Josheghani <[email protected]>
10
     * @since 3 Feb 2016
11
     * @param $value
12
     * @return string
13
     */
14
    function string($value)
15
    {
16
        return (string)$value;
17
    }
18
}
19
20
if(! function_exists('faker')){
21
    /**
22
     * Return the faker instance
23
     *
24
     * @author Alireza Josheghani <[email protected]>
25
     * @since 3 Feb 2016
26
     * @return Generator
27
     */
28
    function faker()
29
    {
30
        return new Generator();
31
    }
32
}
33
34
if(! function_exists('randomNumber')){
35
    /**
36
     * Return random number
37
     *
38
     * @author Alireza Josheghani <[email protected]>
39
     * @since 3 Feb 2016
40
     * @param int $length
41
     * @param bool $int
42
     * @return int|string
43
     */
44
    function randomNumber($length = 20, $int = false)
45
    {
46
        $numbers = "0123456789";
47
48
        $number  = '';
49
50
        for($i = 1;$i <= $length;$i++){
51
            $num = $numbers[rand(0,strlen($numbers) - 1)];
52
            
53
            if($num == 0 && $i == 1){
54
                $i = 1;
55
                continue;
56
            }
57
58
            $number .= $num ;
59
        }
60
61
        if($int){
62
            return (integer)$number;
63
        }
64
65
        return string($number);
66
    }
67
}
68
69
if(! function_exists('randomString')){
70
71
    /**
72
     * Get random string
73
     *
74
     * @author Alireza Josheghani <[email protected]>
75
     * @since 13 Dec 2016
76
     * @param int $length
77
     * @param null $type
78
     * @return string
79
     * @throws \Exception
80
     */
81
    function randomString($length = 20, $type = null){
82
83
        $characters = [
84
            'uppercase' => 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
85
            'lowercase' => 'abcdefghijklmnopqrstuvwxyz'
86
        ];
87
88
        if(($type !== 'uppercase' && $type !== 'lowercase') && ! is_null($type)){
89
            throw new \Exception("The type of $type does not exists!");
90
        }
91
92
        if(! is_null($type)){
93
            $characters = $characters[$type];
94
        } else {
95
            $characters = $characters['lowercase'] . $characters['uppercase'];
96
        }
97
98
        $string  = '';
99
100
        for($i = 1;$i <= $length;$i++){
101
            $string .= $characters[rand(0,strlen($characters) - 1)];
102
        }
103
104
        return $string;
105
    }
106
107
}
108
109
if(! function_exists('isMeliCode')){
110
    /**
111
     * Check is meli code function
112
     *
113
     * @param $value
114
     * @return bool
115
     */
116
    function isMeliCode($value)
117
    {
118
        $sub = 0;
119
        if (!preg_match('/^\d{8,10}$/', $value)) {
120
            return false;
121
        }
122
        if (strlen($value) == 8) {
123
            $value = '00' . $value;
124
        } elseif (strlen($value) == 9) {
125
            $value = '0' . $value;
126
        }
127
        for ($i = 0; $i <= 8; $i++) {
128
            $sub = $sub + ( $value[$i] * ( 10 - $i ) );
129
        }
130
        if (( $sub % 11 ) < 2) {
131
            $control = ( $sub % 11 );
132
        } else {
133
            $control = 11 - ( $sub % 11 );
134
        }
135
        if ($value[9] == $control) {
136
            return true;
137
        } else {
138
            return false;
139
        }
140
    }
141
}
142