1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Riimu\Kit\BaseConversion\DigitList; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Provides common functionality for different digit lists. |
7
|
|
|
* @author Riikka Kalliomäki <[email protected]> |
8
|
|
|
* @copyright Copyright (c) 2015-2017 Riikka Kalliomäki |
9
|
|
|
* @license http://opensource.org/licenses/mit-license.php MIT License |
10
|
|
|
*/ |
11
|
|
|
abstract class AbstractDigitList implements DigitList |
12
|
|
|
{ |
13
|
|
|
/** @var array List of digits */ |
14
|
|
|
protected $digits; |
15
|
|
|
|
16
|
|
|
/** @var int[] List of digit values */ |
17
|
|
|
protected $valueMap; |
18
|
|
|
|
19
|
|
|
/** @var bool Tells if the digits are case sensitive or not */ |
20
|
|
|
protected $caseSensitive; |
21
|
|
|
|
22
|
|
|
/** @var bool Tells if the numeral system can be written using strings */ |
23
|
|
|
protected $stringConflict; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Sets the value map and determines if it's case sensitive. |
27
|
|
|
* @param int[] $map List of values for digits |
28
|
|
|
*/ |
29
|
270 |
|
protected function setValueMap(array $map) |
30
|
|
|
{ |
31
|
270 |
|
$lower = array_change_key_case($map); |
32
|
270 |
|
$this->caseSensitive = count($lower) !== count($map); |
33
|
270 |
|
$this->valueMap = $this->caseSensitive ? $map : $lower; |
34
|
270 |
|
} |
35
|
|
|
|
36
|
141 |
|
public function hasStringConflict() |
37
|
|
|
{ |
38
|
141 |
|
return $this->stringConflict; |
39
|
|
|
} |
40
|
|
|
|
41
|
12 |
|
public function isCaseSensitive() |
42
|
|
|
{ |
43
|
12 |
|
return $this->caseSensitive; |
44
|
|
|
} |
45
|
|
|
|
46
|
219 |
|
public function getDigits() |
47
|
|
|
{ |
48
|
219 |
|
return $this->digits; |
49
|
|
|
} |
50
|
|
|
|
51
|
213 |
|
public function getDigit($value) |
52
|
|
|
{ |
53
|
213 |
|
$value = (int) $value; |
54
|
|
|
|
55
|
213 |
|
if (!isset($this->digits[$value])) { |
56
|
3 |
|
throw new \InvalidArgumentException('Invalid digit value'); |
57
|
|
|
} |
58
|
|
|
|
59
|
210 |
|
return $this->digits[$value]; |
60
|
|
|
} |
61
|
|
|
|
62
|
231 |
|
public function getValue($digit) |
63
|
|
|
{ |
64
|
231 |
|
if (!is_scalar($digit)) { |
65
|
3 |
|
throw new InvalidDigitException('Invalid digit'); |
66
|
|
|
} |
67
|
|
|
|
68
|
228 |
|
$digit = $this->caseSensitive ? (string) $digit : strtolower((string) $digit); |
69
|
|
|
|
70
|
228 |
|
if (!isset($this->valueMap[$digit])) { |
71
|
18 |
|
throw new InvalidDigitException('Invalid digit'); |
72
|
|
|
} |
73
|
|
|
|
74
|
213 |
|
return $this->valueMap[$digit]; |
75
|
|
|
} |
76
|
|
|
|
77
|
231 |
|
public function count() |
78
|
|
|
{ |
79
|
231 |
|
return count($this->digits); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|