Completed
Push — develop ( 8bdaa2...ab8c4f )
by Alireza
01:51
created

Generator::getRandomKey()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 10
nc 3
nop 1
dl 0
loc 15
rs 9.4285
c 1
b 0
f 0
1
<?php
2
3
namespace Josh\Faker;
4
5
class Generator
6
{
7
8
    /**
9
     * List of first names
10
     *
11
     * @var array
12
     */
13
    protected $objects = [];
14
15
    /**
16
     * Set objects, Generator constructor.
17
     *
18
     * @author Alireza Josheghani <[email protected]>
19
     * @since 3 Feb 2017
20
     */
21
    public function __construct()
22
    {
23
        $this->objects = require __DIR__ . '/../objects.php';
24
    }
25
26
    /**
27
     * Get firtname
28
     *
29
     * @author Alireza Josheghani <[email protected]>
30
     * @since 13 Dec 2016
31
     * @return mixed
32
     */
33
    public function firstname()
34
    {
35
        return $this->getRandomKey('names');
36
    }
37
38
    /**
39
     * Get last name
40
     *
41
     * @author Alireza Josheghani <[email protected]>
42
     * @since 13 Dec 2016
43
     * @return mixed
44
     */
45
    public function lastname()
46
    {
47
        return $this->getRandomKey('families');
48
    }
49
    
50
    
51
    /**
52
     * Get company name
53
     *
54
     * @author Vahid Almasi <[email protected]>
55
     * @since 29 Jan 2017
56
     * @return mixed
57
     */
58
    public function company()
59
    {
60
        return $this->getRandomKey('companies');
61
    }
62
63
    /**
64
     * Get random firstname and lastname
65
     *
66
     * @author Alireza Josheghani <[email protected]>
67
     * @since 13 Dec 2016
68
     * @return string
69
     */
70
    public function fullname()
71
    {
72
        return $this->firstname() . ' ' . $this->lastname();
73
    }
74
75
    /**
76
     * Get telephone
77
     *
78
     * @author Alireza Josheghani <[email protected]>
79
     * @since 13 Dec 2016
80
     * @return string
81
     */
82
    public function mobile()
83
    {
84
        $prefix = $this->getRandomKey('prefixTelePhones');
85
86
        return string('0' . $prefix . randomNumber(7));
87
    }
88
89
    /**
90
     * Get random phone
91
     *
92
     * @author Alireza Josheghani <[email protected]>
93
     * @since 13 Dec 2016
94
     * @return string
95
     */
96
    public function telephone()
97
    {
98
        $prefix = $this->getRandomKey('prefixPhones');
99
100
        return string('0' . $prefix . randomNumber(7));
101
    }
102
103
    /**
104
     * Get random email
105
     *
106
     * @author Alireza Josheghani <[email protected]>
107
     * @since 13 Dec 2016
108
     * @return string
109
     */
110
    public function email()
111
    {
112
        $service = $this->getRandomKey('mailServices');
113
114
        return string(randomString(30,'lowercase') . '@' . $service);
115
    }
116
117
    /**
118
     * Get random domain
119
     *
120
     * @author Alireza Josheghani <[email protected]>
121
     * @since 13 Dec 2016
122
     * @return string
123
     */
124
    public function domain()
125
    {
126
        $domain = $this->getRandomKey('domains');
127
128
        return string(randomString(20,'lowercase') . $domain);
129
    }
130
131
    /**
132
     * Get random website
133
     *
134
     * @author Alireza Josheghani <[email protected]>
135
     * @since 13 Dec 2016
136
     * @return string
137
     */
138
    public function website()
139
    {
140
        $protocol = $this->getRandomKey('domains');
141
        
142
        return string($protocol . '://www' . $this->domain());
143
    }
144
145
    /**
146
     * Get random page url
147
     *
148
     * @author Alireza Josheghani <[email protected]>
149
     * @since 13 Dec 2016
150
     * @return string
151
     */
152
    public function pageUrl()
153
    {
154
        $rand = rand(3,7);
155
156
        $string = '';
157
158
        $count = rand(2,8);
159
160
        for($i=0;$i < $rand;$i++){
161
            $string .= randomString($count,'lowercase') . '/';
162
        }
163
164
        return rtrim($string,'/');
165
    }
166
167
    /**
168
     * Get random age
169
     *
170
     * @author Alireza Josheghani <[email protected]>
171
     * @since 13 Dec 2016
172
     * @param int $min
173
     * @param int $max
174
     * @return int
175
     */
176
    public function age($min = 16, $max = 70)
177
    {
178
        return rand($min,$max);
179
    }
180
181
    /**
182
     * Get random address
183
     *
184
     * @author Vahid Almasi <[email protected]>
185
     * @since 3 Feb 2017
186
     * @return mixed
187
     */
188
    public function address()
189
    {
190
        return $this->getRandomKey('address');
191
    }
192
193
    /**
194
     * Get random city
195
     *
196
     * @author Vahid Almasi <[email protected]>
197
     * @since 4 Feb 2017
198
     * @return mixed
199
     */
200
    public function city()
201
    {
202
        return $this->getRandomKey('city');
203
    }
204
205
    /**
206
     * Generate random melicode number
207
     *
208
     * @author Alireza Josheghani <[email protected]>
209
     * @since 4 Feb 2017
210
     * @return int|null|string
211
     */
212
    public function meliCode()
213
    {
214
        $i = 0;
215
        $code= null;
216
217
        while($i <= 100){
218
219
            $meli = randomNumber(10,true);
220
221
            if(strlen($meli) != 10){
222
                continue;
223
            }
224
225
            $c = substr($meli,9,1);
226
227
            $n = substr($meli,0,1) * 10 +
228
                substr($meli,1,1) * 9 +
229
                substr($meli,2,1) * 8 +
230
                substr($meli,3,1) * 7 +
231
                substr($meli,4,1) * 6 +
232
                substr($meli,5,1) * 5 +
233
                substr($meli,6,1) * 4 +
234
                substr($meli,7,1) * 3 +
235
                substr($meli,8,1) * 2;
236
237
            $r = $n - (int)($n / 11) * 11;
238
239
            if (($r == 0 && $r == $c) || ($r == 1 && $c == 1) || ($r > 1 && $c == 11 - $r)) {
240
241
                if(! isMeliCode($meli)){
242
                    continue;
243
                }
244
245
                $code = $meli;
246
247
            } else {
248
                $i++;
249
                continue;
250
            }
251
        }
252
253
        return $code;
254
    }
255
256
    /**
257
     * Get random key from array
258
     *
259
     * @author Alireza Josheghani <[email protected]>
260
     * @since 4 Feb 2017
261
     * @param $object
262
     * @return string
263
     */
264
    private function getRandomKey($object = null)
265
    {
266
        $name = null;
267
        $array = [];
268
269
        if(is_array($object)){
270
            $array = $object;
271
            $name = array_rand($object);
272
        } elseif(is_string($object)) {
273
            $array = $this->objects[$object];
274
            $name = array_rand($array);
275
        }
276
277
        return string($array[$name]);
278
    }
279
280
}
281