AbstractFilter   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 19
lcom 2
cbo 0
dl 0
loc 89
ccs 35
cts 35
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 4
B normalizeOptions() 0 20 7
A getUniqueId() 0 4 1
A setOption() 0 5 1
A setContext() 0 5 1
A filter() 0 13 5
filterSingle() 0 1 ?
1
<?php
2
declare(strict_types=1);
3
namespace Sirius\Filtration\Filter;
4
5
abstract class AbstractFilter
6
{
7
    protected $recursive = true;
8
9
    protected $options = [];
10
11
    protected $context;
12
13 58
    public function __construct($options = [], $recursive = true)
14
    {
15 58
        $options = $this->normalizeOptions($options);
16 57
        if (is_array($options) && ! empty($options)) {
17 9
            foreach ($options as $k => $v) {
18 9
                $this->setOption($k, $v);
19
            }
20
        }
21 57
        $this->recursive = $recursive;
22 57
    }
23
24 58
    protected function normalizeOptions($options)
25
    {
26 58
        if ($options && is_string($options)) {
27 4
            $startChar = substr($options, 0, 1);
28 4
            if ($startChar == '{' || $startChar == '[') {
29 2
                $options = json_decode($options, true);
30
            } else {
31 4
                parse_str($options, $output);
32 4
                $options = $output;
33
            }
34 56
        } elseif (! $options) {
35 53
            $options = [];
36
        }
37
38 58
        if (! is_array($options)) {
39 1
            throw new \InvalidArgumentException('Filtrator options should be an array, JSON string or query string');
40
        }
41
42 57
        return $options;
43
    }
44
45
    /**
46
     * Generates a unique string to identify the validator.
47
     * It can be used to compare 2 validators
48
     * (eg: so you don't add the same validator twice in a validator object)
49
     *
50
     * @return string
51
     */
52 2
    public function getUniqueId(): string
53
    {
54 2
        return __CLASS__ . '|' . json_encode(ksort($this->options));
55
    }
56
57
    /**
58
     * Set an option for the validator.
59
     *
60
     * The options are also be passed to the error message.
61
     *
62
     * @param string $name
63
     * @param mixed $value
64
     * @return self
65
     */
66 25
    public function setOption($name, $value)
67
    {
68 25
        $this->options[$name] = $value;
69 25
        return $this;
70
    }
71
72 13
    public function setContext($context)
73
    {
74 13
        $this->context = $context;
75 13
        return $this;
76
    }
77
78 42
    public function filter($value, string $valueIdentifier = null)
79
    {
80 42
        if ($this->recursive && is_array($value)) {
81 2
            $result = [];
82 2
            foreach ($value as $k => $v) {
83 2
                $vIdentifier = ($valueIdentifier) ? "{$valueIdentifier}[{$k}]" : $k;
84 2
                $result[$k] = $this->filter($v, (string) $vIdentifier);
85
            }
86 2
            return $result;
87
        } else {
88 42
            return $this->filterSingle($value, $valueIdentifier);
89
        }
90
    }
91
92
    abstract public function filterSingle($value, string $valueIdentifier = null);
93
}
94