AbstractBase   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 35
ccs 19
cts 19
cp 1
rs 10
c 0
b 0
f 0
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A filterValue() 0 23 4
A returnValue() 0 3 3
A __toString() 0 3 1
1
<?php
2
3
/*
4
 * This file is part of ocubom/base-convert
5
 *
6
 * © Oscar Cubo Medina <https://ocubom.github.io>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Ocubom\Math;
13
14
use Ocubom\Math\Exception\InvalidArgumentException;
15
16
abstract class AbstractBase implements BaseInterface
17
{
18 6361
    public function filterValue($value): string
19
    {
20
        // Force value as string representation of the number
21 6361
        $number = (is_numeric($value) ? strval($value) : $value) ?: '0';
22
23
        // Check if some digit is invalid
24 6361
        $invalid = array_diff_key(
25 6361
            array_flip(str_split($number)),
26 6361
            $this->getMap()
27 6361
        );
28
29 6361
        if (!empty($invalid)) {
30 2
            uksort($invalid, 'strnatcasecmp');
31
32 2
            throw new InvalidArgumentException(sprintf(
33 2
                'Found %s invalid characters "%s" on number "%s"',
34 2
                (string) $this,
35 2
                implode('', array_keys($invalid)),
36 2
                $number
37 2
            ));
38
        }
39
40 6359
        return $number;
41
    }
42
43 6358
    public function returnValue($value): string
44
    {
45 6358
        return (is_numeric($value) ? strval($value) : $value) ?: '0';
46
    }
47
48 3
    public function __toString(): string
49
    {
50 3
        return strtolower((new \ReflectionClass($this))->getShortName());
51
    }
52
}
53