Passed
Branch master (284f23)
by Antonio Carlos
01:53
created

Random::__call()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 2
crap 2
1
<?php
2
3
namespace PragmaRX\Random;
4
5
class Random
6
{
7
    use Generators, CharCase, Faker;
8
9
    const DEFAULT_STRING_SIZE = 16;
10
11
    const DEFAULT_PATTERN = '[A-Za-z0-9]';
12
13
    protected $numeric = false;
14
15
    protected $start = 0;
16
17
    protected $end = PHP_INT_MAX;
18
19
    protected $size = null;
20
21
    protected $pattern = '[A-Za-z0-9]';
22
23
    private $prefix;
24
25
    private $suffix;
26
27
    /**
28
     * Call faker.
29
     *
30
     * @param $name
31
     * @param $arguments
32
     * @return mixed
33
     * @throws \Exception
34
     */
35 2
    public function __call($name, $arguments)
36
    {
37
        try {
38 2
            $this->fakerString = $this->getFaker()->{$name}(...$arguments);
39 1
        } catch (\Error $e) {
40 1
            throw new \Exception('Faker is not installed. Call to undefined method PragmaRX\Random\Random::'.$name);
41
        }
42
43 1
        return $this;
44
    }
45
46
    /**
47
     * Get a prefixed and/or suffixed string.
48
     *
49
     * @param $value
50
     * @return string
51
     */
52 11
    private function addPrefixSuffix($value)
53
    {
54 11
        if (!is_null($this->prefix) || !is_null($this->suffix)) {
55 3
            return (string) $this->prefix . $value . (string) $this->suffix;
56
        }
57
58 9
        return $value;
59
    }
60
61
    /**
62
     * Set the string suffix.
63
     *
64
     * @param $string
65
     * @return $this
66
     */
67 1
    public function suffix($string)
68
    {
69 1
        $this->suffix = $string;
70
71 1
        return $this;
72
    }
73
74
    /**
75
     * Extract a string pattern from a string.
76
     *
77
     * @param $string
78
     * @return string
79
     */
80 10
    protected function extractPattern($string)
81
    {
82 10
        if (is_null($pattern = $this->getPattern())) {
83 1
            return $string;
84
        }
85
86 9
        preg_match_all("/$pattern/", $string, $matches);
87
88 9
        return implode('', $matches[0]);
89
    }
90
91
    /**
92
     * Get numeric end.
93
     *
94
     * @return int
95
     */
96 1
    public function getEnd()
97
    {
98 1
        return $this->end;
99
    }
100
101
    /**
102
     * Get string pattern.
103
     *
104
     * @return string
105
     */
106 10
    public function getPattern()
107
    {
108 10
        return $this->pattern;
109
    }
110
111
    /**
112
     * Get string pattern.
113
     *
114
     * @return string
115
     */
116 1
    public function noPattern()
117
    {
118 1
        $this->pattern = null;
119
120 1
        return $this;
121
    }
122
123
    /**
124
     * Get the final string size.
125
     *
126
     * @return integer
127
     */
128 10
    public function getSize()
129
    {
130 10
        return $this->size ?: static::DEFAULT_STRING_SIZE;
131
    }
132
133
    /**
134
     * Get numeric start.
135
     *
136
     * @return int
137
     */
138 1
    public function getStart()
139
    {
140 1
        return $this->start;
141
    }
142
143
    /**
144
     * Generate a random hex.
145
     *
146
     * @return string
147
     */
148 3
    public function hex()
149
    {
150 3
        $this->pattern('[a-f0-9]')->uppercase();
151
152 3
        return $this;
153
    }
154
155
    /**
156
     * Set string pattern.
157
     *
158
     * @param string $pattern
159
     * @return $this
160
     */
161 4
    public function pattern($pattern)
162
    {
163 4
        $this->pattern = $pattern;
164
165 4
        return $this;
166
    }
167
168
    /**
169
     * Set the string prefix.
170
     *
171
     * @param $string
172
     * @return $this
173
     */
174 2
    public function prefix($string)
175
    {
176 2
        $this->prefix = $string;
177
178 2
        return $this;
179
    }
180
181
    /**
182
     * Trim string to expected size.
183
     *
184
     * @param $string
185
     * @param int|null $size
186
     * @return string
187
     */
188 10
    protected function trimToExpectedSize($string, $size = null)
189
    {
190 10
        return substr($string, 0, $size ?: $this->getSize());
191
    }
192
193
    /**
194
     * Set result to numeric.
195
     *
196
     * @param bool $state
197
     * @return $this
198
     */
199 3
    public function numeric($state = true)
200
    {
201 3
        $this->numeric = $state;
202
203 3
        return $this;
204
    }
205
206
    /**
207
     * Set result to alpha.
208
     *
209
     * @param bool $state
210
     * @return $this
211
     */
212 1
    public function alpha($state = true)
213
    {
214 1
        $this->numeric = !$state;
215
216 1
        return $this;
217
    }
218
219
    /**
220
     * Set numeric end.
221
     *
222
     * @param int $end
223
     * @return $this
224
     */
225 1
    public function end($end)
226
    {
227 1
        $this->end = $end;
228
229 1
        return $this;
230
    }
231
232
    /**
233
     * Reset one-time values.
234
     *
235
     * @return $this
236
     */
237 11
    public function resetOneTimeValues()
238
    {
239 11
        $this->prefix = null;
240
241 11
        $this->suffix = null;
242
243 11
        $this->fakerString = null;
244
245 11
        return $this;
246
    }
247
248
    /**
249
     * Set numeric start.
250
     *
251
     * @param int $start
252
     * @return $this
253
     */
254 1
    public function start($start)
255
    {
256 1
        $this->start = $start;
257
258 1
        return $this;
259
    }
260
261
    /**
262
     * Set the return string size.
263
     *
264
     * @param $size
265
     * @return $this
266
     */
267 10
    public function size($size)
268
    {
269 10
        $this->size = $size;
270
271 10
        return $this;
272
    }
273
274
    /**
275
     * Get the generated random string/number.
276
     *
277
     * @return string|int
278
     */
279 11
    public function get()
280
    {
281 11
        $result = $this->addPrefixSuffix(
282 11
            $this->changeCase(
283 11
                $this->generate()
284
            )
285
        );
286
287 11
        $this->resetOneTimeValues();
288
289 11
        return $result;
290
    }
291
}
292