Completed
Push — master ( bda47f...2097d9 )
by Antonio Carlos
02:33
created

Random::noPattern()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php
2
3
namespace PragmaRX\Random;
4
5
class Random
6
{
7
    const DEFAULT_STRING_SIZE = 16;
8
9
    const DEFAULT_PATTERN = '[A-Za-z0-9]';
10
11
    protected $lowercase = false;
12
13
    protected $uppercase = false;
14
15
    protected $numeric = false;
16
17
    protected $start = 0;
18
19
    protected $end = PHP_INT_MAX;
20
21
    protected $size = null;
22
23
    protected $pattern = '[A-Za-z0-9]';
24
25
    /**
26
     * Extract a string pattern from a string.
27
     *
28
     * @param $string
29
     * @return string
30
     */
31 8
    private function extractPattern($string)
32
    {
33 8
        if (is_null($pattern = $this->getPattern())) {
34 1
            return $string;
35
        }
36
37 7
        preg_match_all("/$pattern/", $string, $matches);
38
39 7
        return implode('', $matches[0]);
40
    }
41
42
    /**
43
     * Generate a random string.
44
     *
45
     * @return string
46
     */
47 8
    protected function generate()
48
    {
49 8
        return $this->numeric
50 3
            ? $this->generateNumeric()
51 8
            : $this->generateAlpha();
52
    }
53
54 1
    private function generateInteger()
55
    {
56 1
        return random_int($this->getStart(), $this->getEnd());
57
    }
58
59
    /**
60
     * Generate a random string.
61
     *
62
     * @param \Closure $generator
63
     * @return mixed
64
     */
65 8
    private function generateString($generator)
66
    {
67 8
        $string = '';
68
69 8
        while (strlen($string) < $size = $this->getSize()) {
70 8
            $string .= $this->extractPattern($generator($size));
71
        }
72
73 8
        return $this->trimToExpectedSize($string, $size);
74
    }
75
76
    /**
77
     * Get the alpha generator.
78
     *
79
     * @return mixed
80
     */
81
    private function getAlphaGenerator()
82
    {
83 7
        return function ($size) {
84 7
            return random_bytes($size);
85 7
        };
86
    }
87
88
    /**
89
     * Get the alpha generator.
90
     *
91
     * @return mixed
92
     */
93
    private function getNumericGenerator()
94
    {
95 3
        return function () {
96 3
            return random_int(0, PHP_INT_MAX);
97 3
        };
98
    }
99
100
    /**
101
     * Generate a random string.
102
     *
103
     * @return int|string
104
     */
105 7
    private function generateAlpha()
106
    {
107 7
        return $this->generateString($this->getAlphaGenerator());
108
    }
109
110
    /**
111
     * Generate a numeric random value.
112
     *
113
     * @return int|string
114
     */
115 3
    private function generateNumeric()
116
    {
117 3
        if (is_null($this->size) && $this->pattern == static::DEFAULT_PATTERN) {
118 1
            return $this->generateInteger();
119
        }
120
121 3
        return $this->generateString($this->getNumericGenerator());
122
    }
123
124
    /**
125
     * Get numeric end.
126
     *
127
     * @return int
128
     */
129 1
    public function getEnd()
130
    {
131 1
        return $this->end;
132
    }
133
134
    /**
135
     * Get string pattern.
136
     *
137
     * @return string
138
     */
139 8
    public function getPattern()
140
    {
141 8
        return $this->pattern;
142
    }
143
144
    /**
145
     * Get string pattern.
146
     *
147
     * @return string
148
     */
149 1
    public function noPattern()
150
    {
151 1
        $this->pattern = null;
152
153 1
        return $this;
154
    }
155
156
    /**
157
     * Get the final string size.
158
     *
159
     * @return integer
160
     */
161 8
    public function getSize()
162
    {
163 8
        return $this->size ?: static::DEFAULT_STRING_SIZE;
164
    }
165
166
    /**
167
     * Get numeric start.
168
     *
169
     * @return int
170
     */
171 1
    public function getStart()
172
    {
173 1
        return $this->start;
174
    }
175
176
    /**
177
     * Get lowercase state.
178
     *
179
     * @return bool
180
     */
181 8
    public function isLowercase()
182
    {
183 8
        return $this->lowercase;
184
    }
185
186
    /**
187
     * Get uppercase state.
188
     *
189
     * @return bool
190
     */
191 8
    public function isUppercase()
192
    {
193 8
        return $this->uppercase;
194
    }
195
196
    /**
197
     * Generate a random hex.
198
     *
199
     * @return string
200
     */
201 1
    public function hex()
202
    {
203 1
        $this->pattern('[a-f0-9]')->uppercase();
204
205 1
        return $this;
206
    }
207
208
    /**
209
     * Return a string in the proper case.
210
     *
211
     * @param $string
212
     * @return string
213
     */
214 8
    private function changeCase($string)
215
    {
216 8
        if ($this->isLowercase()) {
217 1
            return strtolower($string);
218
        }
219
220 8
        if ($this->isUppercase()) {
221 2
            return strtoupper($string);
222
        }
223
224 7
        return $string;
225
    }
226
227
    /**
228
     * Set the lowercase state.
229
     *
230
     * @param $state
231
     * @return $this
232
     */
233 1
    public function lowercase($state = true)
234
    {
235 1
        $this->mixedcase()->lowercase = $state;
236
237 1
        return $this;
238
    }
239
240
    /**
241
     * Set string pattern.
242
     *
243
     * @param string $pattern
244
     * @return $this
245
     */
246 2
    public function pattern($pattern)
247
    {
248 2
        $this->pattern = $pattern;
249
250 2
        return $this;
251
    }
252
253
    /**
254
     * Trim string to expected size.
255
     *
256
     * @param $string
257
     * @param int|null $size
258
     * @return string
259
     */
260 8
    private function trimToExpectedSize($string, $size = null)
261
    {
262 8
        return substr($string, 0, $size ?: $this->getSize());
263
    }
264
265
    /**
266
     * Set case to mixed.
267
     *
268
     * @return $this
269
     */
270 2
    public function mixedcase()
271
    {
272 2
        $this->uppercase = false;
273
274 2
        $this->lowercase = false;
275
276 2
        return $this;
277
    }
278
279
    /**
280
     * Set result to numeric.
281
     *
282
     * @param bool $state
283
     * @return $this
284
     */
285 3
    public function numeric($state = true)
286
    {
287 3
        $this->numeric = $state;
288
289 3
        return $this;
290
    }
291
292
    /**
293
     * Set result to alpha.
294
     *
295
     * @param bool $state
296
     * @return $this
297
     */
298 1
    public function alpha($state = true)
299
    {
300 1
        $this->numeric = !$state;
301
302 1
        return $this;
303
    }
304
305
    /**
306
     * Set numeric end.
307
     *
308
     * @param int $end
309
     * @return $this
310
     */
311 1
    public function end($end)
312
    {
313 1
        $this->end = $end;
314
315 1
        return $this;
316
    }
317
318
    /**
319
     * Set numeric start.
320
     *
321
     * @param int $start
322
     * @return $this
323
     */
324 1
    public function start($start)
325
    {
326 1
        $this->start = $start;
327
328 1
        return $this;
329
    }
330
331
    /**
332
     * Set the uppercase state.
333
     *
334
     * @param $state
335
     * @return $this
336
     * @internal param bool $uppercase
337
     */
338 2
    public function uppercase($state = true)
339
    {
340 2
        $this->mixedcase()->uppercase = $state;
341
342 2
        return $this;
343
    }
344
345
    /**
346
     * Set the return string size.
347
     *
348
     * @param $size
349
     * @return $this
350
     */
351 8
    public function size($size)
352
    {
353 8
        $this->size = $size;
354
355 8
        return $this;
356
    }
357
358
    /**
359
     * Generate a more truly "random" alpha-numeric string.
360
     *
361
     * Extracted from Laravel Framework: Illuminate\Support\Str
362
     *
363
     * @return string|int
364
     */
365 8
    public function get()
366
    {
367 8
        return $this->changeCase(
368 8
            $this->generate()
369
        );
370
    }
371
}
372