Random::getBoolean()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 2
c 1
b 0
f 1
dl 0
loc 5
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gambling\Tech;
6
7
use Throwable;
8
use ErrorException;
9
use Gambling\Tech\Exception\GamblingTechException;
10
use Gambling\Tech\Exception\InvalidArgumentException;
11
12
/**
13
 * Random generation of values
14
 */
15
class Random
16
{
17
    /**
18
     * Generate random bytes using different approaches
19
     *
20
     * @param int $length
21
     * @return string
22
     * @throws GamblingTechException
23
     */
24
    public static function getBytes(int $length): string
25
    {
26
        try {
27
            return random_bytes($length);
28
        } catch (Throwable $e) {
29
            if ($e instanceof ErrorException) {
30
                throw new InvalidArgumentException($e->getMessage(), 0, $e);
31
            }
32
33
            throw new GamblingTechException($e->getMessage(), 0, $e);
34
        }
35
    }
36
37
    /**
38
     * @param int $min
39
     * @param int $max
40
     * @return int
41
     * @throws GamblingTechException
42
     */
43
    public static function getInteger(int $min, int $max): int
44
    {
45
        try {
46
            return random_int($min, $max);
47
        } catch (Throwable $e) {
48
            if ($e instanceof ErrorException) {
49
                throw new InvalidArgumentException($e->getMessage(), 0, $e);
50
            }
51
52
            throw new GamblingTechException($e->getMessage(), 0, $e);
53
        }
54
    }
55
56
    /**
57
     * @return bool
58
     * @throws GamblingTechException
59
     */
60
    public static function getBoolean(): bool
61
    {
62
        $byte = static::getBytes(1);
63
64
        return (bool)(ord($byte) % 2);
65
    }
66
67
    /**
68
     * @return float
69
     * @throws GamblingTechException
70
     */
71
    public static function getFloat(): float
72
    {
73
        $bytes = static::getBytes(7);
74
75
        $bytes[6] = $bytes[6] | chr(0xF0);
76
        $bytes .= chr(63); // exponent bias (1023)
77
        $float = unpack('d', $bytes)[1];
78
79
        return ($float - 1);
80
    }
81
82
    /**
83
     * @param int $length
84
     * @param string|null $charList
85
     * @return string
86
     * @throws GamblingTechException
87
     * @throws InvalidArgumentException
88
     */
89
    public static function getString(int $length, ?string $charList = null): string
90
    {
91
        if ($length < 1) {
92
            throw new InvalidArgumentException('Length should be >= 1');
93
        }
94
95
        // charList is empty or not provided
96
        if (empty($charList)) {
97
            $numBytes = (int)ceil($length * 0.75);
98
            $bytes = static::getBytes($numBytes);
99
100
            return mb_substr(rtrim(base64_encode($bytes), '='), 0, $length, '8bit');
101
        }
102
103
        $listLen = mb_strlen($charList, '8bit');
104
105
        if ($listLen === 1) {
106
            return str_repeat($charList, $length);
107
        }
108
109
        $result = '';
110
        for ($i = 0; $i < $length; $i++) {
111
            $pos = static::getInteger(0, $listLen - 1);
112
            $result .= $charList[$pos];
113
        }
114
115
        return $result;
116
    }
117
}
118