Passed
Push — master ( 4847bb...ef9a9d )
by Antonio Carlos
02:15
created

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