1 | <?php |
||
20 | class NumberBase |
||
21 | { |
||
22 | /** @var DigitList\DigitList List of digits */ |
||
23 | private $digits; |
||
24 | |||
25 | /** @var string|int|false Pattern for splitting strings into digits */ |
||
26 | private $digitPattern; |
||
27 | |||
28 | /** |
||
29 | * Creates a new instance of NumberBase. |
||
30 | * |
||
31 | * The constructor takes a list of digits for the numeral system as the |
||
32 | * constructor parameter. This can either be an instance of DigitList or |
||
33 | * it can be a string, an integer or an array that is used to construct |
||
34 | * the appropriate type of DigitList. See the constructors for appropriate |
||
35 | * classes for how to define those digit lists. |
||
36 | * |
||
37 | * @param DigitList\DigitList|int|string|array $digitList List of digits |
||
38 | * @throws \InvalidArgumentException If the list of digits is invalid |
||
39 | */ |
||
40 | 309 | public function __construct($digitList) |
|
45 | |||
46 | /** |
||
47 | * Returns an appropriate type of digit list based on the parameter. |
||
48 | * @param int|string|array $digitList List of digits |
||
49 | * @return IntegerDigitList|StringDigitList|ArrayDigitList The built digit list |
||
50 | */ |
||
51 | 309 | private function buildDigitList($digitList) |
|
63 | |||
64 | /** |
||
65 | * Tells if numbers using this numeral system cannot be represented using a string. |
||
66 | * @return bool True if string representation is not supported, false if it is |
||
67 | */ |
||
68 | 129 | public function hasStringConflict() |
|
72 | |||
73 | /** |
||
74 | * Tells if this numeral system is case sensitive or not. |
||
75 | * @return bool True if case sensitive, false if not |
||
76 | */ |
||
77 | 6 | public function isCaseSensitive() |
|
81 | |||
82 | /** |
||
83 | * Returns the radix (i.e. base) of the numeral system. |
||
84 | * @return int Radix of the numeral system |
||
85 | */ |
||
86 | 216 | public function getRadix() |
|
90 | |||
91 | /** |
||
92 | * Returns list of all digits in the numeral system. |
||
93 | * @return array Array of digits in the numeral system |
||
94 | */ |
||
95 | 210 | public function getDigitList() |
|
99 | |||
100 | /** |
||
101 | * Tells if the given digit is part of this numeral system. |
||
102 | * @param mixed $digit The digit to look up |
||
103 | * @return bool True if the digit exists, false is not |
||
104 | */ |
||
105 | 3 | public function hasDigit($digit) |
|
115 | |||
116 | /** |
||
117 | * Returns the decimal value represented by the given digit. |
||
118 | * @param mixed $digit The digit to look up |
||
119 | * @return int The decimal value for the provided digit |
||
120 | * @throws DigitList\InvalidDigitException If the given digit is invalid |
||
121 | */ |
||
122 | 33 | public function getValue($digit) |
|
126 | |||
127 | /** |
||
128 | * Returns the decimal values for given digits. |
||
129 | * @param array $digits Array of digits to look up |
||
130 | * @return int[] Array of digit values |
||
131 | * @throws DigitList\InvalidDigitException If any of the digits is invalid |
||
132 | */ |
||
133 | 204 | public function getValues(array $digits) |
|
134 | { |
||
135 | 204 | $values = []; |
|
136 | |||
137 | 204 | foreach ($digits as $digit) { |
|
138 | 204 | $values[] = $this->digits->getValue($digit); |
|
139 | 64 | } |
|
140 | |||
141 | 192 | return $values; |
|
142 | } |
||
143 | |||
144 | /** |
||
145 | * Returns the digit representing the given decimal value. |
||
146 | * @param int $decimal Decimal value to lookup |
||
147 | * @return mixed The digit that represents the given decimal value |
||
148 | * @throws \InvalidArgumentException If the decimal value is not within the number system |
||
149 | */ |
||
150 | 165 | public function getDigit($decimal) |
|
154 | |||
155 | /** |
||
156 | * Returns the digits representing the given decimal values. |
||
157 | * @param int[] $decimals Decimal values to look up |
||
158 | * @return array Array of digits that represent the given decimal values |
||
159 | * @throws \InvalidArgumentException If any of the decimal values is invalid |
||
160 | */ |
||
161 | 192 | public function getDigits(array $decimals) |
|
162 | { |
||
163 | 192 | $digits = []; |
|
164 | |||
165 | 192 | foreach ($decimals as $decimal) { |
|
166 | 192 | $digits[] = $this->digits->getDigit($decimal); |
|
167 | 64 | } |
|
168 | |||
169 | 192 | return $digits; |
|
170 | } |
||
171 | |||
172 | /** |
||
173 | * Finds the largest integer root shared by the radix of both numeral systems. |
||
174 | * @param NumberBase $base Numeral system to compare against |
||
175 | * @return int|false Highest common integer root or false if none |
||
176 | */ |
||
177 | 129 | public function findCommonRadixRoot(NumberBase $base) |
|
183 | |||
184 | /** |
||
185 | * Returns all integer roots for the radix. |
||
186 | * @return int[] Array of integer roots for the radix |
||
187 | */ |
||
188 | 129 | private function getRadixRoots() |
|
189 | { |
||
190 | 129 | $radix = count($this->digits); |
|
191 | 129 | $roots = [$radix]; |
|
192 | |||
193 | 129 | for ($i = 2; ($root = (int) ($radix ** (1 / $i))) > 1; $i++) { |
|
194 | 123 | if ($root ** $i === $radix) { |
|
195 | 105 | $roots[] = $root; |
|
196 | 35 | } |
|
197 | 41 | } |
|
198 | |||
199 | 129 | return $roots; |
|
200 | } |
||
201 | |||
202 | /** |
||
203 | * Replaces all values in the array with actual digits from the digit list. |
||
204 | * |
||
205 | * This method takes a list of digits and returns the digits properly |
||
206 | * capitalized and typed. This can be used to canonize numbers when dealing |
||
207 | * with case insensitive and loosely typed number bases. |
||
208 | * |
||
209 | * @param array $digits List of digits to canonize |
||
210 | * @return array Canonized list of digits |
||
211 | * @throws DigitList\InvalidDigitException If any of the digits are invalid |
||
212 | */ |
||
213 | 195 | public function canonizeDigits(array $digits) |
|
219 | |||
220 | /** |
||
221 | * Splits number string into individual digits. |
||
222 | * @param string $string String to split into array of digits |
||
223 | * @return array Array of digits |
||
224 | * @throws \RuntimeException If numeral system does not support strings |
||
225 | */ |
||
226 | 105 | public function splitString($string) |
|
245 | |||
246 | /** |
||
247 | * Creates and returns the pattern for splitting strings into digits. |
||
248 | * @return string|int Pattern to split strings into digits |
||
249 | */ |
||
250 | 102 | private function getDigitPattern() |
|
268 | } |
||
269 |