Filter   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 12
lcom 0
cbo 1
dl 0
loc 72
ccs 23
cts 23
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 16 5
B __invoke() 0 19 7
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-2018 LibreWorks contributors
19
 * @license   Apache-2.0
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 reducers
36
     */
37
    protected $reducers = [];
38
    /**
39
     * @var \Caridea\Filter\Chain - The chain to run if field is unregistered
40
     */
41
    protected $otherwise;
42
43
    /**
44
     * Creates a new Filter (but you're probably better off using `Builder`).
45
     *
46
     * Any `Chain`s supplied to this method will be cloned. Modifications to the
47
     * originals will not appear once a `Filter` is constructed.
48
     *
49
     * @param array<string,\Caridea\Filter\Chain> $chains - The filters keyed by field
50
     * @param array<\Caridea\Filter\Reducer> $reducers - Any Reducer filters to include
51
     * @param \Caridea\Filter\Chain $otherwise - The chain to run for missing fields
52
     */
53 4
    public function __construct(array $chains, array $reducers = [], Chain $otherwise = null)
54
    {
55 4
        foreach ($chains as $k => $f) {
56 3
            if (!($f instanceof Chain)) {
57 1
                throw new \InvalidArgumentException("Must be an instance of Chain");
58
            }
59 2
            $this->chains[$k] = clone $f;
60
        }
61 3
        foreach ($reducers as $k => $f) {
62 3
            if (!($f instanceof Reducer)) {
63 1
                throw new \InvalidArgumentException("Must be an instance of Reducer");
64
            }
65 2
            $this->reducers[$k] = $f;
66
        }
67 2
        $this->otherwise = $otherwise;
68 2
    }
69
70
    /**
71
     * Runs the array filter.
72
     *
73
     * Chains are run in the order in which they were inserted. Reducers are run
74
     * afterward and operate on the filtered values. Each reducer is run
75
     * sequentially and operates on the values returned from the previous.
76
     *
77
     * @param array<string,mixed> $values The values to filter
78
     * @return array The filtered array
79
     */
80 2
    public function __invoke(array $values): array
81
    {
82 2
        $out = [];
83 2
        foreach ($this->chains as $field => $chain) {
84 2
            if (array_key_exists($field, $values) || $chain->isRequired()) {
85 2
                $out[$field] = $chain($values[$field] ?? null);
86
            }
87
        }
88 2
        if ($this->otherwise !== null) {
89 1
            $f = $this->otherwise;
90 1
            foreach (array_diff_key($values, $this->chains) as $k => $v) {
91 1
                $out[$k] = $f($v);
92
            }
93
        }
94 2
        foreach ($this->reducers as $multi) {
95 2
            $out = $multi($out);
96
        }
97 2
        return $out;
98
    }
99
}
100