ClosureFilter::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
crap 1
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 ClosureFilter extends AbstractFilter
17
{
18
    /**
19
     * @var string
20
     */
21
    private $property;
22
23
    /**
24
     * @var callable
25
     */
26
    private $function;
27
28
    /**
29
     * @param string   $property
30
     * @param callable $function ($value) -> bool
31
     */
32 49
    public function __construct($property, callable $function)
33
    {
34 49
        $this->property = $property;
35 49
        $this->function = $function;
36 49
    }
37
38
    /**
39
     * Does this filter match?
40
     *
41
     * @param array $data
42
     *
43
     * @return bool
44
     */
45 48
    public function matches(array $data)
46
    {
47 48
        return (isset($data[$this->property]) &&
48 48
            call_user_func($this->function, $data[$this->property]));
49
    }
50
}
51