Completed
Push — master ( ef82f9...6cec72 )
by Antonio Carlos
01:48
created

Random::size()   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
use PragmaRX\Random\Generators\AlphaGenerator;
6
use PragmaRX\Random\Generators\ArrayGenerator;
7
use PragmaRX\Random\Generators\IntegerGenerator;
8
use PragmaRX\Random\Generators\StringGenerator;
9
10
class Random
11
{
12
    use CharCase,
13
        Faker,
14
        Trivia,
15
        AlphaGenerator,
16
        ArrayGenerator,
17
        StringGenerator,
18
        IntegerGenerator;
19
20
    const DEFAULT_STRING_SIZE = 16;
21
22
    const DEFAULT_PATTERN = '[A-Za-z0-9]';
23
24
    protected $numeric = false;
25
26
    protected $size = null;
27
28
    protected $pattern = '[A-Za-z0-9]';
29
30
    private $prefix;
31
32
    private $suffix;
33
34
    /**
35
     * Get a prefixed and/or suffixed string.
36
     *
37
     * @param $value
38
     * @return string
39
     */
40 12
    private function addPrefixSuffix($value)
41
    {
42 12
        if (!is_null($this->prefix) || !is_null($this->suffix)) {
43 3
            return (string) $this->prefix . $value . (string) $this->suffix;
44
        }
45
46 10
        return $value;
47
    }
48
49
    /**
50
     * Generate a random string.
51
     *
52
     * @return string|array
53
     */
54 12
    protected function generate()
55
    {
56 12
        if (! is_null($this->fakerString)) {
57 1
            return $this->fakerString;
58
        }
59
60 11
        if ($this->array) {
61 1
            return $this->generateArray();
62
        }
63
64 10
        return $this->numeric
65 3
            ? $this->generateNumeric()
66 10
            : $this->generateAlpha();
67
    }
68
69
    /**
70
     * Set the string suffix.
71
     *
72
     * @param $string
73
     * @return $this
74
     */
75 1
    public function suffix($string)
76
    {
77 1
        $this->suffix = $string;
78
79 1
        return $this;
80
    }
81
82
    /**
83
     * Extract a string pattern from a string.
84
     *
85
     * @param $string
86
     * @return string
87
     */
88 10
    protected function extractPattern($string)
89
    {
90 10
        if (is_null($pattern = $this->getPattern())) {
91 1
            return $string;
92
        }
93
94 9
        preg_match_all("/$pattern/", $string, $matches);
95
96 9
        return implode('', $matches[0]);
97
    }
98
99
    /**
100
     * Get string pattern.
101
     *
102
     * @return string
103
     */
104 10
    public function getPattern()
105
    {
106 10
        return $this->pattern;
107
    }
108
109
    /**
110
     * Configure to get a raw.
111
     *
112
     * @return string
113
     */
114 1
    public function raw()
115
    {
116 1
        $this->pattern = null;
117
118 1
        return $this;
119
    }
120
121
    /**
122
     * Generate a random hex.
123
     *
124
     * @return string
125
     */
126 3
    public function hex()
127
    {
128 3
        $this->pattern('[a-f0-9]')->uppercase();
129
130 3
        return $this;
131
    }
132
133
    /**
134
     * Set string pattern.
135
     *
136
     * @param string $pattern
137
     * @return $this
138
     */
139 4
    public function pattern($pattern)
140
    {
141 4
        $this->pattern = $pattern;
142
143 4
        return $this;
144
    }
145
146
    /**
147
     * Set the string prefix.
148
     *
149
     * @param $string
150
     * @return $this
151
     */
152 2
    public function prefix($string)
153
    {
154 2
        $this->prefix = $string;
155
156 2
        return $this;
157
    }
158
159
    /**
160
     * Set result to numeric.
161
     *
162
     * @param bool $state
163
     * @return $this
164
     */
165 3
    public function numeric($state = true)
166
    {
167 3
        $this->numeric = $state;
168
169 3
        return $this;
170
    }
171
172
    /**
173
     * Set result to alpha.
174
     *
175
     * @param bool $state
176
     * @return $this
177
     */
178 1
    public function alpha($state = true)
179
    {
180 1
        $this->numeric = !$state;
181
182 1
        return $this;
183
    }
184
185
    /**
186
     * Reset one-time values.
187
     *
188
     * @return $this
189
     */
190 12
    public function resetOneTimeValues()
191
    {
192 12
        $this->prefix = null;
193
194 12
        $this->suffix = null;
195
196 12
        $this->fakerString = null;
197
198 12
        return $this;
199
    }
200
201
    /**
202
     * Get the generated random string/number.
203
     *
204
     * @return string|int
205
     */
206 12
    public function get()
207
    {
208 12
        $result = $this->addPrefixSuffix(
209 12
            $this->changeCase(
210 12
                $this->generate()
211
            )
212
        );
213
214 12
        $this->resetOneTimeValues();
215
216 12
        return $result;
217
    }
218
}
219