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

ArbitraryBaseOutputTrait::getBool()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
/**
4
 * Trait implementation of arbitrary base formats generation for generator services.
5
 */
6
7
namespace CryptoManana\Core\Traits\Randomness;
8
9
use \CryptoManana\Core\StringBuilder as StringBuilder;
10
11
/**
12
 * Trait ArbitraryBaseOutputTrait - Reusable implementation of `ArbitraryBaseOutputInterface`.
13
 *
14
 * @see \CryptoManana\Core\Interfaces\Randomness\ArbitraryBaseOutputInterface The abstract specification.
15
 *
16
 * @package CryptoManana\Core\Traits\Randomness
17
 */
18
trait ArbitraryBaseOutputTrait
19
{
20
    /**
21
     * Generate a random boolean.
22
     *
23
     * @return bool Randomly generated boolean value.
24
     * @throws \Exception Validation errors.
25
     */
26 8
    public function getBool()
27
    {
28 8
        return $this->getInt() % 2 === 0;
0 ignored issues
show
Bug introduced by
It seems like getInt() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

28
        return $this->/** @scrutinizer ignore-call */ getInt() % 2 === 0;
Loading history...
29
    }
30
31
    /**
32
     * Generate a random ternary format (-1, 0, 1).
33
     *
34
     * Note: Passing `false` to the `$asInteger` parameter will convert values to `null`, `false` and `true`.
35
     *
36
     * @param bool|int $asInteger Flag for returning as integer (default => true).
37
     *
38
     * @return bool|int Randomly generated ternary value.
39
     * @throws \Exception Validation errors.
40
     */
41 3
    public function getTernary($asInteger = true)
42
    {
43 3
        $ternary = $this->getInt(-1, 1);
44
45 3
        if ($asInteger) {
46 3
            return $ternary;
47
        } else {
48 3
            switch ($ternary) {
49 3
                case 1:
50 2
                    return true;
51
                case -1:
52 3
                    return null;
53
                default: // case 0:
54 3
                    return false;
55
            }
56
        }
57
    }
58
59
    /**
60
     * Generate a random HEX string.
61
     *
62
     * @param int $length The output string length (default => 1).
63
     * @param bool $upperCase Flag for using uppercase output (default => false).
64
     *
65
     * @return string Randomly generated HEX string.
66
     * @throws \Exception Validation errors.
67
     */
68 3
    public function getHex($length = 1, $upperCase = false)
69
    {
70 3
        $hexString = bin2hex($this->getBytes($length));
0 ignored issues
show
Bug introduced by
It seems like getBytes() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

70
        $hexString = bin2hex($this->/** @scrutinizer ignore-call */ getBytes($length));
Loading history...
71
72 3
        return ($upperCase) ? StringBuilder::stringToUpper($hexString) : $hexString;
73
    }
74
75
    /**
76
     * Generate a random Base64 string.
77
     *
78
     * @param int $length The internal byte string length (default => 1).
79
     * @param bool $urlFriendly Flag for using URL friendly output (default => false).
80
     *
81
     * @return string Randomly generated Base64 RFC 4648 standard string.
82
     * @throws \Exception Validation errors.
83
     */
84 3
    public function getBase64($length = 1, $urlFriendly = false)
85
    {
86 3
        $base64 = base64_encode($this->getBytes($length));
87
88 3
        if ($urlFriendly) {
89 3
            return StringBuilder::stringReplace(['+', '/', '='], ['-', '_', ''], $base64);
90
        } else {
91 3
            return $base64;
92
        }
93
    }
94
}
95