StringDigitList::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 9
cts 9
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
nop 1
crap 3
1
<?php
2
3
namespace Riimu\Kit\BaseConversion\DigitList;
4
5
/**
6
 * Handles a list of digits provided as a string.
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 StringDigitList extends AbstractDigitList
12
{
13
    /**
14
     * Creates a new instance of StringDigitList.
15
     *
16
     * The list of digits must be provided as a string. Each character
17
     * represents a single digit and the position in the string represents the
18
     * value for that digit. For example, base 16 could be defined as
19
     * `0123456789ABCDEF`.
20
     *
21
     * @param string $digits Digits for the numeral system
22
     * @throws \InvalidArgumentException If the list of digits is invalid
23
     */
24 54
    public function __construct($digits)
25
    {
26 54
        if (strlen($digits) < 2) {
27 3
            throw new \InvalidArgumentException('Number base needs at least 2 characters');
28 51
        } elseif (strlen(count_chars($digits, 3)) !== strlen($digits)) {
29 3
            throw new \InvalidArgumentException('Number base cannot have duplicate characters');
30
        }
31
32 48
        $this->digits = str_split($digits);
33 48
        $this->stringConflict = false;
34 48
        $this->setValueMap(array_flip($this->digits));
35 48
    }
36
}
37