1 | <?php |
||
14 | class ByteNumberGenerator implements NumberGenerator |
||
15 | { |
||
16 | /** @var string[] Formats for different numbers of bytes */ |
||
17 | private static $byteFormats = ['Ca', 'na', 'Cb/na', 'Na', 'Cc/Na', 'nc/Na', 'Cd/nc/Na', 'Ja']; |
||
18 | |||
19 | /** @var int[] Default values for byte format values */ |
||
20 | private static $byteDefaults = ['a' => 0, 'b' => 0, 'c' => 0, 'd' => 0]; |
||
21 | |||
22 | /** @var Generator The underlying byte generator */ |
||
23 | private $byteGenerator; |
||
24 | |||
25 | /** |
||
26 | * NumberByteGenerator constructor. |
||
27 | * @param Generator $generator The underlying byte generator used to generate random bytes |
||
28 | */ |
||
29 | 97 | public function __construct(Generator $generator) |
|
33 | |||
34 | /** |
||
35 | * Tells if the underlying byte generator is supported by the system. |
||
36 | * @return bool True if the generator is supported, false if not |
||
37 | */ |
||
38 | 3 | public function isSupported() |
|
42 | |||
43 | /** |
||
44 | * Returns bytes read from the provided byte generator. |
||
45 | * @param int $count The number of bytes to read |
||
46 | * @return string A string of bytes |
||
47 | * @throws GeneratorException If there was an error generating the bytes |
||
48 | */ |
||
49 | 12 | public function getBytes($count) |
|
53 | |||
54 | /** |
||
55 | * Returns a random integer between given minimum and maximum. |
||
56 | * @param int $min The minimum possible value to return |
||
57 | * @param int $max The maximum possible value to return |
||
58 | * @return int A random number between the lower and upper limit (inclusive) |
||
59 | * @throws \InvalidArgumentException If the provided values are invalid |
||
60 | * @throws GeneratorException If an error occurs generating the number |
||
61 | */ |
||
62 | 42 | public function getNumber($min, $max) |
|
83 | |||
84 | /** |
||
85 | * Returns a random number generated using the random byte generator. |
||
86 | * @param int $limit Maximum value for the random number |
||
87 | * @return int The generated random number between 0 and the limit |
||
88 | * @throws GeneratorException If error occurs generating the random number |
||
89 | */ |
||
90 | 27 | private function getByteNumber($limit) |
|
108 | |||
109 | /** |
||
110 | * Returns a number from byte generator based on given number of bytes. |
||
111 | * @param int $bytes The number of bytes to read |
||
112 | * @return int A random number read from the bytes of the byte generator |
||
113 | * @throws GeneratorException If the errors occurs generating the bytes for the number |
||
114 | */ |
||
115 | 27 | private function readByteNumber($bytes) |
|
120 | } |
||
121 |