ElementPicker   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 8
eloc 20
c 2
b 0
f 0
dl 0
loc 67
ccs 21
cts 21
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A pickCharacterElement() 0 15 3
A pickArrayElement() 0 33 5
1
<?php
2
3
/**
4
 * Utility class for random element picking.
5
 */
6
7
namespace CryptoManana\Utilities;
8
9
use CryptoManana\Core\Abstractions\Containers\AbstractRandomnessInjectable as RandomnessContainer;
10
use CryptoManana\Core\Interfaces\Containers\ElementPickingInterface as ElementChoosing;
11
use CryptoManana\Core\StringBuilder as StringBuilder;
12
13
/**
14
 * Class ElementPicker - Utility class for random element picking.
15
 *
16
 * @package CryptoManana\Utilities
17
 *
18
 * @property \CryptoManana\Core\Abstractions\Randomness\AbstractGenerator $randomnessSource The randomness generator.
19
 */
20
class ElementPicker extends RandomnessContainer implements ElementChoosing
21
{
22
    /**
23
     * Pick a random character from string.
24
     *
25
     * @param string $string The string with characters for choosing from.
26
     *
27
     * @return string The chosen character string.
28
     * @throws \Exception Validation errors.
29
     */
30 8
    public function pickCharacterElement($string = '')
31
    {
32 8
        if (!is_string($string)) {
33 2
            throw new \InvalidArgumentException('The supplied argument is not of type string.');
34
        }
35
36 6
        if (empty($string)) {
37 2
            return $string;
38
        }
39
40
        // Convert the string to an array
41 5
        $array = StringBuilder::stringSplit($string, 1);
42
43
        // Reuse the code for array element choosing
44 5
        return $this->pickArrayElement($array);
45
    }
46
47
    /**
48
     * Pick a random element from array.
49
     *
50
     * @param array $array The array with elements for choosing from.
51
     *
52
     * @return mixed The chosen element from the array.
53
     */
54 8
    public function pickArrayElement(array $array = [])
55
    {
56 8
        if (empty($array)) {
57 2
            return $array;
58
        }
59
60 7
        $count = count($array);
61
62 7
        $iterator = 0;
63
64
        // Choose random index
65 7
        $elementIndex = ($count === 1) ? 0 : $this->randomnessSource->getInt(0, $count - 1);
66
67
        // Reset pointer to begging
68 7
        reset($array);
69
70 7
        $tmpElement = null;
71
72
        // Iterate through array elements
73 7
        foreach ($array as $keyName => $value) {
74
            // If element is found
75 7
            if ($elementIndex == $iterator) {
76 7
                $tmpElement = $value;
77
78 7
                break;
79
            }
80
81
            // Update the index
82 7
            $iterator++;
83
        }
84
85
        // Return the chosen element
86 7
        return $tmpElement;
87
    }
88
}
89