Passed
Push — master ( 742b46...ae2e05 )
by Tony Karavasilev (Тони
20:10
created

StringOutputTrait::getString()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 17
ccs 3
cts 3
cp 1
rs 9.9332
c 0
b 0
f 0
cc 3
nc 3
nop 2
crap 3
1
<?php
2
3
/**
4
 * Trait implementation of string format generation for generator services.
5
 */
6
7
namespace CryptoManana\Core\Traits\Randomness;
8
9
use \CryptoManana\Core\Traits\Randomness\RandomnessTrait as RandomnessSpecification;
10
use \CryptoManana\Core\StringBuilder as StringBuilder;
11
12
/**
13
 * Trait StringOutputTrait - Reusable implementation of `StringOutputInterface`.
14
 *
15
 * @see \CryptoManana\Core\Interfaces\Randomness\StringOutputInterface The abstract specification.
16
 *
17
 * @package CryptoManana\Core\Traits\Randomness
18
 *
19
 * @mixin RandomnessSpecification
20
 */
21
trait StringOutputTrait
22
{
23
    /**
24
     * Internal method for character map validation.
25
     *
26
     * @param array $charMap The character map array.
27
     *
28
     * @throws \Exception Validation errors.
29
     */
30
    protected function validateCharacterMap(array $charMap)
31
    {
32
        foreach ($charMap as $char) {
33
            if (!is_string($char)) {
34
                throw new \InvalidArgumentException(
35
                    'The provided symbol map must contain only elements of type string.'
36
                );
37
            } elseif (StringBuilder::stringLength($char) != 1) {
38 9
                throw new \LengthException(
39
                    'The provided symbol map\'s values must only be of 1 character length.'
40 9
                );
41
            }
42
        }
43
44
        if (count($charMap) < 2) {
45
            throw new \LengthException(
46
                'You must supply a set of at least 2 characters for the output string generation.'
47
            );
48
        }
49
    }
50
51 6
    /**
52
     * Forcing the implementation of the software abstract randomness.
53 6
     *
54 6
     * {@internal Forcing the implementation of `AbstractRandomness`. }}
55
     */
56 6
    use RandomnessSpecification;
57
58 3
    /**
59
     * Generate a random digit character.
60
     *
61 6
     * @param bool $includeZero Flag for including the zero digit (default => true).
62
     *
63
     * @return string Randomly generated digit character.
64
     * @throws \Exception Validation errors.
65
     */
66
    public function getDigit($includeZero = true)
67
    {
68
        return ($includeZero) ? (string)$this->getInt(0, 9) : (string)$this->getInt(1, 9);
69
    }
70
71
    /**
72
     * Generate a random english letter character.
73 6
     *
74
     * @param bool $caseSensitive Flag for enabling case sensitive generation (default => true).
75 6
     *
76
     * @return string Randomly generated english letter character.
77 6
     * @throws \Exception Validation errors.
78
     */
79 6
    public function getLetter($caseSensitive = true)
80 6
    {
81 6
        if ($caseSensitive) {
82
            $upper = $this->getBool();
83 6
84
            $letterCode = $upper ? $this->getInt(65, 90) : $this->getInt(97, 122);
85
        } else {
86
            $letterCode = $this->getInt(97, 122);
87 6
        }
88
89
        return StringBuilder::getChr($letterCode);
90
    }
91
92
    /**
93
     * Generate a random alphanumeric string.
94
     *
95
     * @param int $length The output string length (default => 1).
96
     * @param bool $caseSensitive Flag for enabling case sensitive generation (default => true).
97
     *
98
     * @return string Randomly generated alphanumeric string.
99 3
     * @throws \Exception Validation errors.
100
     */
101 3
    public function getAlphaNumeric($length = 1, $caseSensitive = true)
102
    {
103 3
        $this->validatePositiveInteger($length);
104 3
105
        $id = '';
106 3
107 3
        for ($i = 1; $i <= $length; $i++) {
108
            if ($this->getBool()) {
109
                $id .= $this->getLetter($caseSensitive);
110 3
            } else {
111
                $id .= $this->getDigit(true);
112
            }
113
        }
114
115
        return $id;
116
    }
117
118
    /**
119
     * Generate a random ASCII (American Standard Code) string containing only printable characters.
120
     *
121
     * @param int $length The output string length (default => 1).
122 12
     * @param bool|int $includeSpace Flag for including the space character (default => true).
123
     *
124 12
     * @return string Randomly generated ASCII string.
125 3
     * @throws \Exception Validation errors.
126
     */
127 12
    public function getAscii($length = 1, $includeSpace = false)
128 12
    {
129 3
        $this->validatePositiveInteger($length);
130 3
131
        $asciiString = '';
132 12
        $startFrom = ($includeSpace == true) ? 32 : 33;
133 3
134 3
        for ($i = 1; $i <= $length; $i++) {
135
            $asciiString .= StringBuilder::getChr($this->getInt($startFrom, 126));
136
        }
137
138
        return $asciiString;
139 6
    }
140 3
141 3
    /**
142
     * Generate a random string with custom characters.
143
     *
144
     * @param int $length The output string length (default => 1).
145 3
     * @param array $characters The character map for the string generation (default => ASCII).
146
     *
147 3
     * @return string Randomly generated string using a custom character map.
148 3
     * @throws \Exception Validation errors.
149
     */
150 3
    public function getString($length = 1, array $characters = [])
151 3
    {
152
        if (empty($characters)) {
153
            return $this->getAscii($length, true);
154 3
        } else {
155
            $this->validatePositiveInteger($length);
156
157
            $this->validateCharacterMap($characters);
158
159
            $passwordString = '';
160
            $lastIndex = count($characters) - 1;
161
162
            for ($i = 1; $i <= $length; $i++) {
163
                $passwordString .= $characters[$this->getInt(0, $lastIndex)];
164
            }
165
166
            return $passwordString;
167
        }
168
    }
169
}
170