Completed
Push — master ( eb3c01...eceada )
by Carsten
05:38 queued 10s
created

NDeviationFormatter::__invoke()   B

Complexity

Conditions 8
Paths 14

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 8.3518

Importance

Changes 0
Metric Value
dl 0
loc 29
ccs 14
cts 17
cp 0.8235
rs 8.2114
c 0
b 0
f 0
cc 8
nc 14
nop 1
crap 8.3518
1
<?php
2
namespace FilmTools\NDeviation;
3
4
class NDeviationFormatter
5
{
6
7
    /**
8
     * @var string
9
     */
10
    public $format;
11
12
    /**
13
     * @param string $format Optional: sprintf format string
14
     */
15 21
    public function __construct( string $format = "%01.1f" )
16
    {
17 21
        $this->format = $format;
18 21
    }
19
20
    /**
21
     * @param  float|NDeviationProviderInterface|NDeviationInterface $N
22
     * @return string
23
     */
24 21
    public function __invoke( $N )
25
    {
26 21
        if ($N instanceOf NDeviationProviderInterface):
27
            $N = $N->getNDeviation()->getValue();
28
29 21
        elseif ($N instanceOf NDeviationInterface):
30 12
            $N = $N->getValue();
31 9
        elseif (is_string( $N )):
32 9
            if (filter_var($N, FILTER_VALIDATE_FLOAT) === false):
33 9
                return $N;
34
            endif;
35
        elseif (!is_numeric( $N )):
36
            throw new \InvalidArgumentException("Numeric value, NDeviationInterface, or NDeviationProviderInterface expected.");
37
        endif;
38
39
40 18
        $N_str = sprintf( $this->format, abs($N));
41
42 18
        if ($N > 0) {
43 9
            $N_str = "𝑵 +" . $N_str;
44 9
        } elseif ($N < 0) {
45 6
            $N_str = "𝑵 -" . $N_str;
46
        } else { // == 0.0
47
            // $N_str = "𝑵";
48 3
            $N_str = "𝑵 ±" . $N_str;
49
        }
50
51 18
        return $N_str;
52
    }
53
}
54