Passed
Push — master ( 742b46...c0a0b6 )
by Tony Karavasilev (Тони
01:29
created

StringOutputTrait::getString()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 17
ccs 10
cts 10
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 12
    protected function validateCharacterMap(array $charMap)
31
    {
32 12
        foreach ($charMap as $char) {
33 12
            if (!is_string($char)) {
34 3
                throw new \InvalidArgumentException(
35 3
                    'The provided symbol map must contain only elements of type string.'
36
                );
37 12
            } elseif (StringBuilder::stringLength($char) != 1) {
38 3
                throw new \LengthException(
39 3
                    'The provided symbol map\'s values must only be of 1 character length.'
40
                );
41
            }
42
        }
43
44 6
        if (count($charMap) < 2) {
45 3
            throw new \LengthException(
46 3
                'You must supply a set of at least 2 characters for the output string generation.'
47
            );
48
        }
49 3
    }
50
51
    /**
52
     * Forcing the implementation of the software abstract randomness.
53
     *
54
     * {@internal Forcing the implementation of `AbstractRandomness`. }}
55
     */
56
    use RandomnessSpecification;
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 9
    public function getDigit($includeZero = true)
67
    {
68 9
        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 6
    public function getLetter($caseSensitive = true)
80
    {
81 6
        if ($caseSensitive) {
82 6
            $upper = $this->getBool();
83
84 6
            $letterCode = $upper ? $this->getInt(65, 90) : $this->getInt(97, 122);
85
        } else {
86 3
            $letterCode = $this->getInt(97, 122);
87
        }
88
89 6
        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 6
    public function getAlphaNumeric($length = 1, $caseSensitive = true)
102
    {
103 6
        $this->validatePositiveInteger($length);
104
105 6
        $id = '';
106
107 6
        for ($i = 1; $i <= $length; $i++) {
108 6
            if ($this->getBool()) {
109 6
                $id .= $this->getLetter($caseSensitive);
110
            } else {
111 6
                $id .= $this->getDigit(true);
112
            }
113
        }
114
115 6
        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 3
    public function getAscii($length = 1, $includeSpace = false)
128
    {
129 3
        $this->validatePositiveInteger($length);
130
131 3
        $asciiString = '';
132 3
        $startFrom = ($includeSpace == true) ? 32 : 33;
133
134 3
        for ($i = 1; $i <= $length; $i++) {
135 3
            $asciiString .= StringBuilder::getChr($this->getInt($startFrom, 126));
136
        }
137
138 3
        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 12
    public function getString($length = 1, array $characters = [])
151
    {
152 12
        if (empty($characters)) {
153 3
            return $this->getAscii($length, true);
154
        } else {
155 12
            $this->validatePositiveInteger($length);
156
157 12
            $this->validateCharacterMap($characters);
158
159 3
            $passwordString = '';
160 3
            $lastIndex = count($characters) - 1;
161
162 3
            for ($i = 1; $i <= $length; $i++) {
163 3
                $passwordString .= $characters[$this->getInt(0, $lastIndex)];
164
            }
165
166 3
            return $passwordString;
167
        }
168
    }
169
}
170