Completed
Push — master ( 416125...7247d9 )
by Alexander
07:08
created

Filter::matchesExpression()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 2
1
<?php
2
/**
3
 * Ember Db - An embeddable document database for php.
4
 * Copyright (C) 2016 Alexander During
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18
 *
19
 * @link      http://github.com/alexanderduring/php-ember-db
20
 * @copyright Copyright (C) 2016 Alexander During
21
 * @license   http://www.gnu.org/licenses GNU General Public License v3.0
22
 */
23
24
namespace EmberDb\Filter;
25
26
class Filter
27
{
28
    private $filterData = array();
29
30
31
32
    public function __construct($filterArray)
33
    {
34
        $this->filterData = $filterArray;
35
    }
36
37
38
39
    public function matchesEntry(array $entry)
40
    {
41
        $isMatch = $this->matchesArray($this->filterData, $entry);
42
43
        return $isMatch;
44
    }
45
46
47
48
    private function matchesArray($filterArray, $entryArray)
49
    {
50
        $isMatch = true;
51
        foreach ($filterArray as $filterKey => $filterValue) {
52
53
            // Check if the filter key exists and fetch corresponding value from entry
54
            if (array_key_exists($filterKey, $entryArray)) {
55
                $entryValue = $entryArray[$filterKey];
56
            } else {
57
                $isMatch = false;
58
                break;
59
            }
60
61
            // Check if the values match
62
            $isMatch &= $this->matchesValue($filterValue, $entryValue);
63
        }
64
65
        return $isMatch;
66
    }
67
68
69
70
    private function matchesValue($filterValue, $entryValue)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
71
    {
72
        $isMatch = false;
73
74
        // If both are an array ...
75
        if (is_array($filterValue) && is_array($entryValue)) {
76
            $isMatch = $this->matchesArray($filterValue, $entryValue);
77
        }
78
79
        // If both aren't ...
80
        if (!is_array($filterValue) && !is_array($entryValue)) {
81
            $isMatch = $filterValue === $entryValue;
82
        }
83
84
        // If filter is an operator ...
85
        $operatorManager = new OperatorManager();
86
        if ($operatorManager->isOperator($filterValue)) {
87
            $operator = $operatorManager->createOperator($filterValue);
88
            $isMatch = $operator->matches($entryValue);
89
        }
90
91
        return $isMatch;
92
    }
93
}
94