IntegerDigitList::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Riimu\Kit\BaseConversion\DigitList;
4
5
/**
6
 * Handles a list of digits defined according to number base.
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
class IntegerDigitList extends AbstractDigitList
12
{
13
    /** @var string List of digits for bases smaller than 63 */
14
    private static $integerBase = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
15
16
    /** @var string List of digits for base 64 */
17
    private static $integerBase64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
18
19
    /**
20
     * Creates a new instance of IntegerDigitList.
21
     *
22
     * The digit list is defined by giving the radix (i.e. base) for the number
23
     * system that defines the number of digits in the digit list. The actual
24
     * digits are determined based on the given number.
25
     *
26
     * If the given radix is 62 or less, the digits from the list 0-9A-Za-z
27
     * are used. The digits are case insensitive, if the radix is 36 or less.
28
     *
29
     * If the radix is 64, then the digits from the base64 standard are used.
30
     * Base64 is always case sensitive.
31
     *
32
     * If the radix is 63 or 65 to 256, then digits are represented by a single
33
     * byte with equal byte value.
34
     *
35
     * If the radix is 257 or greater, then each digit is represented by a
36
     * string of #NNN (where NNN is the value of the digit). Each string has
37
     * equal length, which depends on the given radix.
38
     *
39
     * @param int $radix Radix for the numeral system
40
     * @throws \InvalidArgumentException If the radix is invalid
41
     */
42 237
    public function __construct($radix)
43
    {
44 237
        $radix = (int) $radix;
45
46 237
        if ($radix < 2) {
47 3
            throw new \InvalidArgumentException('Invalid radix');
48
        }
49
50 234
        $this->digits = $this->buildDigitList($radix);
51 234
        $this->stringConflict = false;
52 234
        $this->setValueMap(array_flip($this->digits));
53 234
    }
54
55
    /**
56
     * Builds the list of digits according to the radix.
57
     * @param int $radix Radix for the numeral system
58
     * @return string[] The list of digits
59
     */
60 234
    private function buildDigitList($radix)
61
    {
62 234
        if ($radix <= strlen(self::$integerBase)) {
63 219
            return str_split(substr(self::$integerBase, 0, $radix));
64 21
        } elseif ($radix === 64) {
65 12
            return str_split(self::$integerBase64);
66 18
        } elseif ($radix <= 256) {
67 9
            return range(chr(0), chr($radix - 1));
68
        }
69
70 12
        return $this->buildArbitraryList($radix);
71
    }
72
73
    /**
74
     * Builds the list of digits for arbitrary radix.
75
     * @param int $radix Radix for the numeral system
76
     * @return string[] The list of digits
77
     */
78 12
    private function buildArbitraryList($radix)
79
    {
80 12
        $format = '#%0' . strlen($radix - 1) . 'd';
81
82 12
        return array_map(function ($value) use ($format) {
83 12
            return sprintf($format, $value);
84 12
        }, range(0, $radix - 1));
85
    }
86
}
87