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

StringOutputTrait   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 18
eloc 41
dl 0
loc 127
c 0
b 0
f 0
ccs 42
cts 42
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getDigit() 0 3 2
B getString() 0 33 7
A getAscii() 0 12 3
A getAlphaNumeric() 0 15 3
A getLetter() 0 11 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\StringBuilder as StringBuilder;
10
11
/**
12
 * Trait StringOutputTrait - Reusable implementation of `StringOutputInterface`.
13
 *
14
 * @see \CryptoManana\Core\Interfaces\Randomness\StringOutputInterface The abstract specification.
15
 *
16
 * @package CryptoManana\Core\Traits\Randomness
17
 */
18
trait StringOutputTrait
19
{
20
    /**
21
     * Generate a random digit character.
22
     *
23
     * @param bool $includeZero Flag for including the zero digit (default => true).
24
     *
25
     * @return string Randomly generated digit character.
26
     * @throws \Exception Validation errors.
27
     */
28 9
    public function getDigit($includeZero = true)
29
    {
30 9
        return ($includeZero) ? (string)$this->getInt(0, 9) : (string)$this->getInt(1, 9);
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

30
        return ($includeZero) ? (string)$this->/** @scrutinizer ignore-call */ getInt(0, 9) : (string)$this->getInt(1, 9);
Loading history...
31
    }
32
33
    /**
34
     * Generate a random english letter character.
35
     *
36
     * @param bool $caseSensitive Flag for enabling case sensitive generation (default => true).
37
     *
38
     * @return string Randomly generated english letter character.
39
     * @throws \Exception Validation errors.
40
     */
41 6
    public function getLetter($caseSensitive = true)
42
    {
43 6
        if ($caseSensitive) {
44 6
            $upper = $this->getBool();
1 ignored issue
show
Bug introduced by
It seems like getBool() 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

44
            /** @scrutinizer ignore-call */ 
45
            $upper = $this->getBool();
Loading history...
45
46 6
            $letterCode = $upper ? $this->getInt(65, 90) : $this->getInt(97, 122);
47
        } else {
48 3
            $letterCode = $this->getInt(97, 122);
49
        }
50
51 6
        return StringBuilder::getChr($letterCode);
52
    }
53
54
    /**
55
     * Generate a random alphanumeric string.
56
     *
57
     * @param int $length The output string length (default => 1).
58
     * @param bool $caseSensitive Flag for enabling case sensitive generation (default => true).
59
     *
60
     * @return string Randomly generated alphanumeric string.
61
     * @throws \Exception Validation errors.
62
     */
63 6
    public function getAlphaNumeric($length = 1, $caseSensitive = true)
64
    {
65 6
        $this->validatePositiveInteger($length);
0 ignored issues
show
Bug introduced by
It seems like validatePositiveInteger() 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

65
        $this->/** @scrutinizer ignore-call */ 
66
               validatePositiveInteger($length);
Loading history...
66
67 6
        $id = '';
68
69 6
        for ($i = 1; $i <= $length; $i++) {
70 6
            if ($this->getBool()) {
71 6
                $id .= $this->getLetter($caseSensitive);
72
            } else {
73 6
                $id .= $this->getDigit(true);
74
            }
75
        }
76
77 6
        return $id;
78
    }
79
80
    /**
81
     * Generate a random ASCII (American Standard Code) string containing only printable characters.
82
     *
83
     * @param int $length The output string length (default => 1).
84
     * @param bool|int $includeSpace Flag for including the space character (default => true).
85
     *
86
     * @return string Randomly generated ASCII string.
87
     * @throws \Exception Validation errors.
88
     */
89 3
    public function getAscii($length = 1, $includeSpace = false)
90
    {
91 3
        $this->validatePositiveInteger($length);
92
93 3
        $asciiString = '';
94 3
        $startFrom = ($includeSpace == true) ? 32 : 33;
95
96 3
        for ($i = 1; $i <= $length; $i++) {
97 3
            $asciiString .= StringBuilder::getChr($this->getInt($startFrom, 126));
98
        }
99
100 3
        return $asciiString;
101
    }
102
103
    /**
104
     * Generate a random string with custom characters.
105
     *
106
     * @param int $length The output string length (default => 1).
107
     * @param array $characters The character map for the string generation (default => ASCII).
108
     *
109
     * @return string Randomly generated string using a custom character map.
110
     * @throws \Exception Validation errors.
111
     */
112 12
    public function getString($length = 1, array $characters = [])
113
    {
114 12
        if (empty($characters)) {
115 3
            return $this->getAscii($length, true);
116
        } else {
117 12
            foreach ($characters as $char) {
118 12
                if (!is_string($char)) {
119 3
                    throw new \InvalidArgumentException(
120 3
                        'The provided symbol map must contain only elements of type string.'
121
                    );
122 12
                } elseif (StringBuilder::stringLength($char) != 1) {
123 3
                    throw new \LengthException(
124 3
                        'The provided symbol map\'s values must only be of 1 character length.'
125
                    );
126
                }
127
            }
128
129 6
            if (count($characters) < 2) {
130 3
                throw new \LengthException(
131 3
                    'You must supply a set of at least 2 characters for the output string generation.'
132
                );
133
            }
134
135 3
            $this->validatePositiveInteger($length);
136
137 3
            $passwordString = '';
138 3
            $lastIndex = count($characters) - 1;
139
140 3
            for ($i = 1; $i <= $length; $i++) {
141 3
                $passwordString .= $characters[$this->getInt(0, $lastIndex)];
142
            }
143
144 3
            return $passwordString;
145
        }
146
    }
147
}
148