AllOfFilter::__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 AllOfFilter extends AbstractFilter
17
{
18
    /**
19
     * @var callable[]
20
     */
21
    private $filters;
22
23
    /**
24
     * @param array $filters
25
     */
26 14
    public function __construct(array $filters = [])
27
    {
28 14
        foreach ($filters as $filter) {
29 12
            $this->addFilter($filter);
30 14
        }
31 14
    }
32
33
    /**
34
     * @param callable $filter
35
     */
36 13
    public function addFilter(callable $filter)
37
    {
38 13
        $this->filters[] = $filter;
39 13
    }
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 13
    public function matches(array $data)
49
    {
50 13
        foreach ($this->filters as $filter) {
51 13
            if (!$filter($data)) {
52 10
                return false;
53
            }
54 12
        }
55
56 11
        return true;
57
    }
58
}
59