Condition::reverse()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2013-2017 2amigOS! Consulting Group LLC
4
 * @link http://2amigos.us
5
 * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
6
 */
7
namespace dosamigos\arrayquery\conditions;
8
9
10
/**
11
 * Condition abstract base class where all conditions extend from
12
 *
13
 * @author Antonio Ramirez <[email protected]>
14
 * @link http://www.ramirezcobos.com/
15
 * @link http://www.2amigos.us/
16
 * @package dosamigos\arrayquery\conditions
17
 */
18
abstract class Condition
19
{
20
    /**
21
     * @var mixed the value to match against
22
     */
23
    protected $value;
24
25
    /**
26
     * @var bool whether to reverse or not
27
     */
28
    protected $negate = false;
29
30
    /**
31
     * @param mixed $value the value to match against
32
     */
33
    public function __construct($value)
34
    {
35
        $this->value = $value;
36
    }
37
38
    /**
39
     * Reverses the condition
40
     * @return $this
41
     */
42
    public function reverse()
43
    {
44
        $this->negate = !$this->negate;
45
        return $this;
46
    }
47
48
    /**
49
     * Checks whether the value passes condition
50
     *
51
     * @param mixed $data the data to match
52
     *
53
     * @return mixed
54
     */
55
    abstract public function matches($data);
56
57
    /**
58
     * Checks whether the value and the data are of same type
59
     *
60
     * @param mixed $value
61
     * @param mixed $against
62
     *
63
     * @return bool true if both are of same type
64
     */
65
    protected function checkType($value, $against)
66
    {
67
        if (is_numeric($value) && is_numeric($against)) {
68
            $value = filter_var($value, FILTER_SANITIZE_NUMBER_FLOAT);
69
            $against = filter_var($against, FILTER_SANITIZE_NUMBER_FLOAT);
70
        }
71
72
        if (gettype($value) != gettype($against)) {
73
            return false;
74
        }
75
        return true;
76
    }
77
}
78