Completed
Push — master ( 47b9c6...4c7c5e )
by Christian
03:50
created

RandomString::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 3
crap 1
1
<?php
2
3
namespace Didatus\RandomString;
4
5
/**
6
 * Class RandomString
7
 * @package Didatus\RandomString
8
 */
9
class RandomString {
10
    /**
11
     * contains the list of characters to be used for random string generation
12
     * @var string
13
     */
14
    private $character_pool = '';
15
16
    /**
17
     * contains the list of characters to be excluded from random string generation
18
     * @var string
19
     */
20
    private $except_characters = '';
21
22
    /**
23
     * defines wether a character is unique in a random string or not
24
     * @var bool
25
     */
26
    private $unique_characters = false;
27
28
    /**
29
     * RandomString constructor.
30
     * @param string $character_pool
31
     * @param string $except_characters
32
     * @param bool $unique_characters
33
     */
34 8
    public function __construct(
35
        string $character_pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
36
        string $except_characters = '',
37
        bool $unique_characters = false
38
    ) {
39 8
        $this->character_pool = $character_pool;
40 8
        $this->except_characters = $except_characters;
41 8
        $this->unique_characters = $unique_characters;
42 8
    }
43
44
    /**
45
     * generates one random string of $length and returns it
46
     * @param int $length
47
     * @return string
48
     */
49 8
    public function getString(int $length = 10):string
50
    {
51 8
        $string_length = 0;
52 8
        $string = '';
53 8
        while ($string_length < $length)
54
        {
55 8
            $character = $this->getRandomCharacter();
56 8
            if ($this->unique_characters && $this->containsCharacter($string, $character)) {
57 1
                continue;
58
            }
59 8
            $string .= $character;
60 8
            $string_length = strlen($string);
61
        }
62
63 8
        return $string;
64
    }
65
66
    /**
67
     * generates a list of random strings of $length and returns it
68
     * @param $length
69
     * @param $count
70
     * @return array
71
     */
72 2
    public function getListOfString(int $length, int $count):array
73
    {
74 2
        $list_count = 0;
75 2
        for ($strings = []; $list_count < $count;)
76
        {
77 2
            $strings[] = $this->getString($length);
78 2
            $list_count = count($strings);
79
        }
80
81 2
        return $strings;
82
    }
83
84
    /**
85
     * provides a random character
86
     * @return string
87
     */
88 8
    private function getRandomCharacter():string
89
    {
90 8
        return $this->getCharacterPool()[rand(0, strlen($this->getCharacterPool()) - 1)];
91
    }
92
93
    /**
94
     * generates the character pool without character exceptions
95
     * @return string
96
     */
97 8
    private function getCharacterPool():string
98
    {
99 8
        return implode("", array_diff(str_split($this->character_pool), str_split($this->except_characters)));
100
    }
101
102
    /**
103
     * checks if the given string contains given character
104
     * @param $string
105
     * @param $character
106
     * @return bool
107
     */
108 1
    private function containsCharacter($string, $character):bool
109
    {
110 1
        if (strpos($string, $character) !== false) {
111 1
            return true;
112
        }
113
114 1
        return false;
115
    }
116
}
117