Completed
Push — master ( 1c9958...db493f )
by Pierre
05:09
created

Filter::process()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 5
ccs 0
cts 4
cp 0
crap 2
rs 10
1
<?php
2
3
/**
4
 * Component Filter
5
 * 
6
 * filter an array matching args
7
 *
8
 * @author pierrefromager
9
 */
10
11
namespace App\Component;
12
13
class Filter
14
{
15
    const INPUT_FILTER_FILTER = 'filter';
16
    const INPUT_FILTER_OPTIONS = 'options';
17
    const INPUT_FILTER_PROCESS = 'process';
18
19
    private $filterArgs;
20
    private $data;
21
    private $prepared;
22
    private $result;
23
24
    /**
25
     * __construct
26
     *
27
     * @param array $data
28
     * @param array $filterArgs
29
     */
30
    public function __construct(array $data, array $filterArgs)
31
    {
32
        $this->data = $data;
33
        $this->filterArgs = $filterArgs;
34
        return $this;
35
    }
36
37
    /**
38
     * process result filter
39
     *
40
     * @return Filter
41
     */
42
    public function process(): Filter
43
    {
44
        $this->prepare();
45
        $this->result = \filter_var_array($this->data, $this->prepared);
46
        return $this;
47
    }
48
49
    /**
50
     * toArray
51
     *
52
     * @return array
53
     */
54
    public function toArray()
55
    {
56
        return $this->result;
57
    }
58
59
    /**
60
     * prepare datas
61
     *
62
     * @return Filter
63
     */
64
    protected function prepare(): Filter
65
    {
66
        $this->prepared = [];
67
        foreach ($this->filterArgs as $k => $v) {
68
            if (is_object($v)) {
69
                $this->prepared[$k] = [
70
                    self::INPUT_FILTER_FILTER => FILTER_CALLBACK,
71
                    self::INPUT_FILTER_OPTIONS => [$v, self::INPUT_FILTER_PROCESS]
72
                ];
73
            } else {
74
                $this->prepared[$k] = $v;
75
            }
76
        }
77
        unset($this->filterArgs);
78
        return $this;
79
    }
80
}
81