Random::numeric()   A
last analyzed

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