Completed
Push — master ( f3b91b...5eec2b )
by Jonathan
02:32
created

Filter::__invoke()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 8
cts 8
cp 1
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 8
nc 6
nop 1
crap 5
1
<?php
2
declare(strict_types=1);
3
/**
4
 * Caridea
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
7
 * use this file except in compliance with the License. You may obtain a copy of
8
 * the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
 * License for the specific language governing permissions and limitations under
16
 * the License.
17
 *
18
 * @copyright 2015 LibreWorks contributors
19
 * @license   http://opensource.org/licenses/Apache-2.0 Apache 2.0 License
20
 */
21
namespace Caridea\Filter;
22
23
/**
24
 * That's why I say hey man, nice shot.
25
 *
26
 * This class contains several chains of functions which sanitize values.
27
 */
28
class Filter implements Reducer
29
{
30
    /**
31
     * @var array<string,\Caridea\Filter\Chain> - The filters keyed by field
32
     */
33
    protected $chains = [];
34
    /**
35
     * @var array<\Caridea\Filter\Reducer> - The
36
     */
37
    protected $reducers = [];
38
39
    /**
40
     * Creates a new Filter (but you're probably better off using `Builder`).
41
     *
42
     * Any `Chain`s supplied to this method will be cloned. Modifications to the
43
     * originals will not appear once a `Filter` is constructed.
44
     *
45
     * @param array<string,\Caridea\Filter\Chain> $chains - The filters keyed by field
46
     * @param array<\Caridea\Filter\Reducer> $reducers - Any Reducer filters to include
47
     */
48 4
    public function __construct(array $chains, array $reducers = [])
49
    {
50 4
        foreach ($chains as $k => $f) {
51 3
            if (!($f instanceof Chain)) {
52 1
                throw new \InvalidArgumentException("Must be an instance of Chain");
53
            }
54 2
            $this->chains[$k] = clone $f;
55
        }
56 3
        foreach ($reducers as $k => $f) {
57 3
            if (!($f instanceof Reducer)) {
58 1
                throw new \InvalidArgumentException("Must be an instance of Reducer");
59
            }
60 2
            $this->reducers[$k] = $f;
61
        }
62 2
    }
63
64
    /**
65
     * Runs the array filter.
66
     *
67
     * Chains are run in the order in which they were inserted. Reducers are run
68
     * afterward and operate on the *original* values, not the filtered ones.
69
     *
70
     * @param array<string,mixed> $values The values to filter
71
     * @return array The filtered array
72
     */
73 2
    public function __invoke(array $values): array
74
    {
75 2
        $out = [];
76 2
        foreach ($this->chains as $field => $chain) {
77 2
            if (array_key_exists($field, $values) || $chain->isRequired()) {
78 2
                $out[$field] = $chain($values[$field] ?? null);
79
            }
80
        }
81 2
        foreach ($this->reducers as $multi) {
82 2
            $out = array_merge($out, $multi($values));
83
        }
84 2
        return $out;
85
    }
86
}
87