Passed
Push — master ( 5ad2cb...35956d )
by Antonio Carlos
03:45
created

Random::getStart()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace PragmaRX\Random;
4
5
class Random
6
{
7
    use Generators, CharCase, Faker, Trivia;
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 $array = false;
22
23
    protected $items = [];
24
25
    protected $count = 1;
26
27
    protected $pattern = '[A-Za-z0-9]';
28
29
    private $prefix;
30
31
    private $suffix;
32
33
    /**
34
     * Get a prefixed and/or suffixed string.
35
     *
36
     * @param $value
37
     * @return string
38
     */
39 12
    private function addPrefixSuffix($value)
40
    {
41 12
        if (!is_null($this->prefix) || !is_null($this->suffix)) {
42 3
            return (string) $this->prefix . $value . (string) $this->suffix;
43
        }
44
45 10
        return $value;
46
    }
47
48
    /**
49
     * Set the array items count.
50
     *
51
     * @param $count
52
     * @return $this
53
     */
54 1
    public function count($count)
55
    {
56 1
        $this->count = $count;
57
58 1
        return $this;
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
     * Configure to get a raw.
113
     *
114
     * @return string
115
     */
116 1
    public function raw()
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 12
    public function resetOneTimeValues()
238
    {
239 12
        $this->prefix = null;
240
241 12
        $this->suffix = null;
242
243 12
        $this->fakerString = null;
244
245 12
        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 12
    public function get()
280
    {
281 12
        $result = $this->addPrefixSuffix(
282 12
            $this->changeCase(
283 12
                $this->generate()
284
            )
285
        );
286
287 12
        $this->resetOneTimeValues();
288
289 12
        return $result;
290
    }
291
}
292