AbstractDigitList::isCaseSensitive()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
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