AbstractBase::returnValue()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 3

Importance

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