1 | <?php |
||
11 | class ArrayDigitList extends AbstractDigitList |
||
12 | { |
||
13 | /** |
||
14 | * Creates a new instance of ArrayDigitList. |
||
15 | * |
||
16 | * The list of digits is provided as an array. The index provides value for |
||
17 | * the digits and the values provide the digits themselves. Any kind of |
||
18 | * value is an acceptable digit, but note that the digits are considered |
||
19 | * duplicate if their values are equal using a loose comparison. |
||
20 | * |
||
21 | * @param array $digits The list of digits for the numeral system |
||
22 | * @throws \InvalidArgumentException If the list of digits is invalid |
||
23 | */ |
||
24 | 45 | public function __construct(array $digits) |
|
25 | { |
||
26 | 45 | $this->validateDigits($digits); |
|
27 | |||
28 | 33 | $this->digits = $digits; |
|
29 | |||
30 | 33 | $mapped = array_map('strval', array_filter($digits, [$this, 'isMapped'])); |
|
31 | 33 | $this->valueMap = array_flip($mapped); |
|
32 | 33 | $this->stringConflict = count($mapped) === count($this->digits) |
|
33 | 33 | ? $this->detectConflict($mapped, 'strpos') : true; |
|
34 | |||
35 | 33 | $this->caseSensitive = $this->detectConflict($mapped, 'stripos'); |
|
36 | |||
37 | 33 | if (!$this->caseSensitive) { |
|
38 | 27 | array_change_key_case($this->valueMap); |
|
39 | 9 | } |
|
40 | 33 | } |
|
41 | |||
42 | /** |
||
43 | * Validates and sorts the list of digits. |
||
44 | * @param array $digits The list of digits for the numeral system |
||
45 | * @throws \InvalidArgumentException If the digit list is invalid |
||
46 | */ |
||
47 | 45 | private function validateDigits(& $digits) |
|
59 | |||
60 | /** |
||
61 | * Tells if the list of digits has duplicate values. |
||
62 | * @param array $digits The list of digits for the numeral system |
||
63 | * @return bool True if the list contains duplicate digits, false if not |
||
64 | */ |
||
65 | 39 | private function detectDuplicates(array $digits) |
|
66 | { |
||
67 | 39 | for ($i = count($digits); $i > 0; $i--) { |
|
68 | 39 | if (array_search(array_pop($digits), $digits) !== false) { |
|
69 | 6 | return true; |
|
70 | } |
||
71 | 12 | } |
|
72 | |||
73 | 33 | return false; |
|
74 | } |
||
75 | |||
76 | /** |
||
77 | * Tells if the digit can be mapped using a value map. |
||
78 | * @param mixed $digit The digit to test |
||
79 | * @return bool True if the digit can be mapped, false if not |
||
80 | */ |
||
81 | 33 | private function isMapped($digit) |
|
85 | |||
86 | /** |
||
87 | * Tells if a conflict exists between string values. |
||
88 | * @param string[] $digits The list of digits for the numeral system |
||
89 | * @param callable $detect Function used to detect the conflict |
||
90 | * @return bool True if a conflict exists, false if not |
||
91 | */ |
||
92 | 33 | private function detectConflict(array $digits, callable $detect) |
|
93 | { |
||
94 | 33 | foreach ($digits as $digit) { |
|
95 | 27 | if ($this->inDigits($digit, $digits, $detect)) { |
|
96 | 23 | return true; |
|
97 | } |
||
98 | 9 | } |
|
99 | |||
100 | 27 | return false; |
|
101 | } |
||
102 | |||
103 | /** |
||
104 | * Tells if a conflict exists for a digit in a list of digits. |
||
105 | * @param string $digit A single digit to test |
||
106 | * @param string[] $digits The list of digits for the numeral system |
||
107 | * @param callable $detect Function used to detect the conflict |
||
108 | * @return bool True if a conflict exists, false if not |
||
109 | */ |
||
110 | 27 | private function inDigits($digit, array $digits, callable $detect) |
|
111 | { |
||
112 | 27 | foreach ($digits as $haystack) { |
|
113 | 27 | if ($digit !== $haystack && $detect($haystack, $digit) !== false) { |
|
114 | 23 | return true; |
|
115 | } |
||
116 | 9 | } |
|
117 | |||
118 | 21 | return false; |
|
119 | } |
||
120 | |||
121 | 21 | public function getValue($digit) |
|
135 | } |
||
136 |