Completed
Push — master ( e32b76...47b9c6 )
by Christian
06:40
created

RandomString   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Test Coverage

Coverage 55.17%

Importance

Changes 0
Metric Value
wmc 11
dl 0
loc 106
ccs 16
cts 29
cp 0.5517
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getRandomCharacter() 0 3 1
A __construct() 0 8 1
A getString() 0 15 4
A getCharacterPool() 0 3 1
A getListOfString() 0 10 2
A containsCharacter() 0 7 2
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
    public function __construct(
35
        string $character_pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
36
        string $except_characters = '',
37
        bool $unique_characters = false
38
    ) {
39
        $this->character_pool = $character_pool;
40
        $this->except_characters = $except_characters;
41
        $this->unique_characters = $unique_characters;
42
    }
43
44
    /**
45
     * generates one random string of $length and returns it
46
     * @param int $length
47
     * @return string
48
     */
49 6
    public function getString(int $length = 10):string
50
    {
51 6
        $string_length = 0;
52 6
        $string = '';
53 6
        while ($string_length < $length)
54
        {
55 6
            $character = $this->getRandomCharacter();
56 6
            if ($this->unique_characters && $this->containsCharacter($string, $character)) {
57 1
                continue;
58
            }
59 6
            $string .= $character;
60 6
            $string_length = strlen($string);
61
        }
62
63 6
        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
    private function getRandomCharacter():string
89
    {
90
        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
    private function getCharacterPool():string
98
    {
99
        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
    private function containsCharacter($string, $character):bool
109
    {
110
        if (strpos($string, $character) !== false) {
111
            return true;
112
        }
113
114
        return false;
115
    }
116
}
117