StringOutputTrait::validateCharacterMap()   A
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 6

Importance

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