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

Otherwise::__invoke()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
crap 2
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
 * Cherry-picks values that aren't in a list.
25
 */
26
class Otherwise implements Reducer
27
{
28
    /**
29
     * @var \Caridea\Filter\Chain
30
     */
31
    protected $chain;
32
    /**
33
     * @var array<string,mixed>
34
     */
35
    protected $ignores;
36
37
    /**
38
     * Creates a new Otherwise.
39
     *
40
     * @param \Caridea\Filter\Chain $chain The filter chain to apply
41
     * @param array<string,mixed> $ignores Array keys should be fields to skip
42
     */
43 1
    public function __construct(Chain $chain, array $ignores)
44
    {
45 1
        $this->chain = $chain;
46 1
        $this->ignores = $ignores;
47 1
    }
48
49
    /**
50
     * {@inheritDoc}
51
     */
52 1
    public function __invoke(array $input): array
53
    {
54 1
        $out = [];
55 1
        $f = $this->chain;
56 1
        foreach (array_diff_key($input, $this->ignores) as $k => $v) {
57 1
            $out[$k] = $f($v);
58
        }
59 1
        return $out;
60
    }
61
}
62