OneOfFilter::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 3
nc 2
nop 1
crap 2
1
<?php
2
/**
3
 * This file is part of graze/array-filter
4
 *
5
 * Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license https://github.com/graze/array-filter/blob/master/LICENSE.md
11
 * @link    https://github.com/graze/array-filter
12
 */
13
14
namespace Graze\ArrayFilter;
15
16
class OneOfFilter extends AbstractFilter
17
{
18
    /**
19
     * @var callable[]
20
     */
21
    private $filters;
22
23
    /**
24
     * @param array $filters
25
     */
26 7
    public function __construct(array $filters = [])
27
    {
28 7
        foreach ($filters as $filter) {
29 5
            $this->addFilter($filter);
30 7
        }
31 7
    }
32
33
    /**
34
     * @param callable $filter
35
     */
36 6
    public function addFilter(callable $filter)
37
    {
38 6
        $this->filters[] = $filter;
39 6
    }
40
41
    /**
42
     * Loop through all the filters and only return if all match
43
     *
44
     * @param array $data
45
     *
46
     * @return bool
47
     */
48 6
    public function matches(array $data)
49
    {
50 6
        foreach ($this->filters as $filter) {
51 6
            if ($filter($data)) {
52 6
                return true;
53
            }
54 6
        }
55
56 6
        return false;
57
    }
58
}
59