NDeviationFormatter   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 90.91%

Importance

Changes 0
Metric Value
wmc 10
lcom 0
cbo 2
dl 0
loc 55
ccs 20
cts 22
cp 0.9091
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B __invoke() 0 34 9
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 27
    public function __construct( string $format = "%01.1f" )
16
    {
17 27
        $this->format = $format;
18 27
    }
19
20
    /**
21
     * @param  float|NDeviationProviderInterface|NDeviationInterface|string $N
22
     * @return string
23
     */
24 27
    public function __invoke( $N )
25
    {
26 27
        if ($N instanceOf NDeviationProviderInterface):
27
            $N = $N->getNDeviation();
28
        endif;
29
30 27
        if ($N instanceOf NDeviationInterface):
31 15
            $N = $N->getValue();
32
        endif;
33
34 27
        if (is_string( $N )):
35 9
            if (filter_var($N, FILTER_VALIDATE_FLOAT) === false):
36 9
                return $N;
37
            endif;
38 18
        elseif (is_null($N)):
39 6
            return '';
40 12
        elseif (!is_numeric( $N )):
41
            throw new \InvalidArgumentException("Numeric value, NDeviationInterface, or NDeviationProviderInterface expected.");
42
        endif;
43
44
45 18
        $N_str = sprintf( $this->format, abs($N));
46
47 18
        if ($N > 0) {
48 9
            $N_str = "𝑵 +" . $N_str;
49 9
        } elseif ($N < 0) {
50 6
            $N_str = "𝑵 -" . $N_str;
51
        } else { // == 0.0
52
            // $N_str = "𝑵";
53 3
            $N_str = "𝑵 ±" . $N_str;
54
        }
55
56 18
        return $N_str;
57
    }
58
}
59