Passed
Push — master ( 82a58b...d35cf8 )
by Tomasz
02:32
created

Filter   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
dl 0
loc 52
ccs 19
cts 19
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A transformedValue() 0 7 2
A trim() 0 11 3
A name() 0 3 1
A __construct() 0 5 1
A simplifiedValue() 0 7 2
1
<?php
2
declare(strict_types=1);
3
4
namespace ReadModel\Filters;
5
6
use ReadModel\Filters\DataTransformer\DataTransformer;
7
use ReadModel\Filters\DataTransformer\NullTransformer;
8
9
final class Filter
10
{
11
    /** @var string */
12
    private $name;
13
14
    /** @var mixed */
15
    private $value;
16
17
    /** @var DataTransformer */
18
    private $transformer;
19
20 6
    public function __construct(string $name, $value, DataTransformer $transformer = null)
21
    {
22 6
        $this->name = $name;
23 6
        $this->value = $this->trim($value);
24 6
        $this->transformer = $transformer ?? new NullTransformer();
25 6
    }
26
27 4
    public function simplifiedValue()
28
    {
29 4
        if ($this->value === null) {
30 1
            return null;
31
        }
32
33 3
        return $this->transformer->simplify($this->value);
34
    }
35
36 4
    public function transformedValue()
37
    {
38 4
        if ($this->value === null) {
39 1
            return null;
40
        }
41
42 3
        return $this->transformer->transform($this->value);
43
    }
44
45 3
    public function name(): string
46
    {
47 3
        return $this->name;
48
    }
49
50 6
    private function trim($value)
51
    {
52 6
        if (is_string($value)) {
53 4
            return trim($value);
54
        }
55
56 2
        if (is_array($value)) {
57
            return array_map(function ($v) { return trim($v); }, $value);
58
        }
59
60
        return $value;
61
    }
62
}
63