Completed
Push — master ( ebd7bb...65be67 )
by Tony Karavasilev (Тони
04:40
created

AbstractGenerator::getTernary()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 14
ccs 9
cts 9
cp 1
rs 9.9
c 0
b 0
f 0
cc 4
nc 4
nop 1
crap 4
1
<?php
2
3
/**
4
 * Abstraction for pseudo-random generator objects.
5
 */
6
7
namespace CryptoManana\Core\Abstractions\Randomness;
8
9
use \CryptoManana\Core\Abstractions\Randomness\AbstractRandomness as RandomnessRepresentation;
10
use \CryptoManana\Core\Interfaces\Randomness\FloatOutputInterface as FloatOutput;
11
use \CryptoManana\Core\Interfaces\Randomness\ArbitraryBaseOutputInterface as BaseOutput;
12
use \CryptoManana\Core\Interfaces\Randomness\StringOutputInterface as StringOutput;
13
use \CryptoManana\Core\Interfaces\Randomness\IdentifierOutputInterface as UuidOutput;
14
use \CryptoManana\Core\Interfaces\Randomness\RgbOutputInterface as RgbOutput;
15
use \CryptoManana\Core\Traits\Randomness\FloatOutputTrait as FloatGeneration;
16
use \CryptoManana\Core\Traits\Randomness\ArbitraryBaseOutputTrait as BaseGeneration;
17
use \CryptoManana\Core\Traits\Randomness\StringOutputTrait as StringGeneration;
18
use \CryptoManana\Core\Traits\Randomness\IdentifierOutputTrait as UuidGeneration;
19
use \CryptoManana\Core\Traits\Randomness\RgbOutputTrait as RgbGeneration;
20
21
/**
22
 * Class AbstractGenerator - Abstraction for pseudo-random generator classes.
23
 *
24
 * @package CryptoManana\Core\Abstractions\Randomness
25
 *
26
 * @mixin FloatGeneration
27
 * @mixin BaseGeneration
28
 * @mixin StringGeneration
29
 * @mixin UuidGeneration
30
 * @mixin RgbGeneration
31
 */
32
abstract class AbstractGenerator extends RandomnessRepresentation implements
33
    FloatOutput,
34
    BaseOutput,
35
    StringOutput,
36
    UuidOutput,
37
    RgbOutput
38
{
39
    /**
40
     * Float generation formats.
41
     *
42
     * {@internal Reusable implementation of `FloatOutputInterface`. }}
43
     */
44
    use FloatGeneration;
45
46
    /**
47
     * Arbitrary base generation formats.
48
     *
49
     * {@internal Reusable implementation of `ArbitraryBaseOutputInterface`. }}
50
     */
51
    use BaseGeneration;
52
53
    /**
54
     * String generation formats.
55
     *
56
     * {@internal Reusable implementation of `StringOutputInterface`. }}
57
     */
58
    use StringGeneration;
59
60
    /**
61
     * Unique string identifier generation formats.
62
     *
63
     * {@internal Reusable implementation of `IdentifierOutputInterface`. }}
64
     */
65
    use UuidGeneration;
66
67
    /**
68
     * Red-Green-Blue (RGB) colour generation formats.
69
     *
70
     * {@internal Reusable implementation of `RgbOutputInterface`. }}
71
     */
72
    use RgbGeneration;
73
74
    /**
75
     * The default system precision storage.
76
     *
77
     * @var int|null The used default floating number precision.
78
     */
79
    protected static $systemPrecision = null;
80
81
    /**
82
     * Internal method for integer range validation.
83
     *
84
     * @param int $from The minimum number in the wanted range.
85
     * @param int $to The maximum number in the wanted range.
86
     *
87
     * @throws \Exception Validation errors.
88
     */
89 35
    protected function validateIntegerRange($from, $to)
90
    {
91 35
        $from = filter_var(
92 35
            $from,
93 35
            FILTER_VALIDATE_INT,
94
            [
95
                "options" => [
96 35
                    "min_range" => $this->getMinNumber(),
97 35
                    "max_range" => $this->getMaxNumber(),
98
                ],
99
            ]
100
        );
101
102 35
        $to = filter_var(
103 35
            $to,
104 35
            FILTER_VALIDATE_INT,
105
            [
106
                "options" => [
107 35
                    "min_range" => $this->getMinNumber(),
108 35
                    "max_range" => $this->getMaxNumber(),
109
                ],
110
            ]
111
        );
112
113 35
        if ($from === false || $to === false) {
114 9
            throw new \DomainException(
115 9
                "The provided values are of invalid type or are out of the supported range."
116
            );
117
        }
118
119 26
        if ($from >= $to) {
120 6
            throw new \LogicException(
121 6
                "The chosen generation maximum is less or equal the provided minimum value."
122
            );
123
        }
124 20
    }
125
126
    /**
127
     * Internal method for validation of positive integers.
128
     *
129
     * @param int $integer The positive integer value.
130
     * @param bool $includeZero Flag for enabling the zero as a valid value.
131
     *
132
     * @throws \Exception Validation errors.
133
     */
134 36
    protected function validatePositiveInteger($integer, $includeZero = false)
135
    {
136 36
        $integer = filter_var(
137 36
            $integer,
138 36
            FILTER_VALIDATE_INT,
139
            [
140
                "options" => [
141 36
                    "min_range" => $includeZero ? 0 : 1,
142 36
                    "max_range" => PHP_INT_MAX,
143
                ],
144
            ]
145
        );
146
147 36
        if ($integer === false) {
148 3
            throw new \DomainException('The provided value must be a positive integer.');
149
        }
150 33
    }
151
152
    /**
153
     * Randomness generator constructor.
154
     */
155 72
    public function __construct()
156
    {
157
        // Fetch the global system precision setting
158 72
        if (is_null(self::$systemPrecision)) {
159 1
            self::$systemPrecision = (int)ini_get('precision');
160
        }
161 72
    }
162
163
    /**
164
     * Randomness generator reinitialization tasks after unserialization.
165
     */
166 3
    public function __wakeup()
167
    {
168
        // Ensures randomness is reinitialized and auto-seeded
169 3
        $this->__construct();
170 3
    }
171
172
    /**
173
     * Get debug information for the class instance.
174
     *
175
     * @return array Debug information.
176
     */
177 3
    public function __debugInfo()
178
    {
179
        return [
180 3
            'systemPrecision' => self::$systemPrecision,
181
        ];
182
    }
183
}
184