Filter   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 25
c 1
b 0
f 0
dl 0
loc 89
ccs 20
cts 20
cp 1
rs 10
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A toArray() 0 3 1
A prepare() 0 14 3
A process() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Component Filter
7
 *
8
 * filter an array matching args
9
 *
10
 * @author pierrefromager
11
 */
12
13
namespace App\Component;
14
15
class Filter
16
{
17
    const INPUT_FILTER_FILTER = 'filter';
18
    const INPUT_FILTER_OPTIONS = 'options';
19
    const INPUT_FILTER_PROCESS = 'process';
20
21
    /**
22
     * filters arguments
23
     *
24
     * @var array
25
     */
26
    private $filterArgs;
27
28
    /**
29
     * input raw datas
30
     *
31
     * @var array
32
     */
33
    private $data;
34
35
    /**
36
     * prepared datas
37
     *
38
     * @var array
39
     */
40
    private $prepared;
41
42
    /**
43
     * processed datas results
44
     *
45
     * @var array
46
     */
47
    private $result;
48
49
    /**
50
     * __construct
51
     *
52
     * @param array $data
53
     * @param array $filterArgs
54
     */
55 4
    public function __construct(array $data = [], array $filterArgs = [])
56
    {
57 4
        $this->data = $data;
58 4
        $this->filterArgs = $filterArgs;
59 4
        $this->result = [];
60 4
        return $this;
61
    }
62
63
    /**
64
     * process result filter
65
     *
66
     * @return Filter
67
     */
68 2
    public function process(): Filter
69
    {
70 2
        $this->prepare();
71 2
        $this->result = \filter_var_array($this->data, $this->prepared);
72 2
        return $this;
73
    }
74
75
    /**
76
     * toArray
77
     *
78
     * @return array
79
     */
80 2
    public function toArray(): array
81
    {
82 2
        return $this->result;
83
    }
84
85
    /**
86
     * prepare datas
87
     *
88
     * @return Filter
89
     */
90 1
    protected function prepare(): Filter
91
    {
92 1
        $this->prepared = [];
93 1
        foreach ($this->filterArgs as $k => $v) {
94 1
            if (is_object($v)) {
95 1
                $this->prepared[$k] = [
96 1
                    self::INPUT_FILTER_FILTER => \FILTER_CALLBACK,
97 1
                    self::INPUT_FILTER_OPTIONS => [$v, self::INPUT_FILTER_PROCESS]
98
                ];
99
            } else {
100 1
                $this->prepared[$k] = $v;
101
            }
102
        }
103 1
        return $this;
104
    }
105
}
106