AbstractFilter::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 10
ccs 6
cts 7
cp 0.8571
rs 9.4285
cc 3
eloc 6
nc 2
nop 3
crap 3.0261
1
<?php
2
namespace TRex\Collection;
3
4
abstract class AbstractFilter
5
{
6
    /**
7
     * @var mixed
8
     */
9
    private $value;
10
11
    /**
12
     * @var bool
13
     */
14
    private $isOneOfTheValues;
15
16
    /**
17
     * @var bool
18
     */
19
    private $isStrict;
20
21
    /**
22
     * @param mixed $value
23
     * @param bool $isOneOfTheValues
24
     * @param bool $isStrict
25
     */
26 5
    public function __construct($value, $isOneOfTheValues = false, $isStrict = true)
27
    {
28 5
        if ($isOneOfTheValues && !is_array($value)) {
29
            throw new \InvalidArgumentException('if $isOneOfTheValues is true, $value must be an array');
30
        }
31
32 5
        $this->value = $value;
33 5
        $this->isOneOfTheValues = $isOneOfTheValues;
34 5
        $this->isStrict = $isStrict;
35 5
    }
36
37
    /**
38
     * @param mixed $object
39
     * @return bool
40
     */
41 5
    public function __invoke($object)
42
    {
43 5
        $value = $this->invoke($object);
44
45 5
        if ($this->isOneOfTheValues) {
46 1
            return in_array($value, $this->value, $this->isStrict);
47
        }
48
49 4
        if ($this->isStrict) {
50 3
            return $value === $this->value;
51
        }
52
53 1
        return $value == $this->value;
54
    }
55
56
    /**
57
     * @param mixed $object
58
     * @return mixed
59
     */
60
    abstract protected function invoke($object);
61
}
62
63