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 Filter2 |
27
|
|
|
{ |
28
|
|
|
private $operatorHierarchy = array(); |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
|
32
|
|
|
public function __construct($filterArray) |
33
|
|
|
{ |
34
|
|
|
$this->operatorHierarchy = $this->buildOperatorHierarchy($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
|
|
View Code Duplication |
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
|
|
View Code Duplication |
private function matchesValue($filterValue, $entryValue) |
|
|
|
|
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
|
|
|
|
95
|
|
|
|
96
|
|
|
private function buildOperatorHierarchy($filterArray) |
97
|
|
|
{ |
98
|
|
|
$hierarchy = array(); |
99
|
|
|
foreach ($filterArray as $queryKey => $queryValue) { |
100
|
|
|
$hierarchy[$queryKey] = $this->buildOperator($queryValue); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return $hierarchy; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
|
107
|
|
|
|
108
|
|
|
private function buildOperator($operatorArray) |
109
|
|
|
{ |
110
|
|
|
$operatorManager = new OperatorManager(); |
111
|
|
|
if ($operatorManager->isOperator($operatorArray)) { |
112
|
|
|
$operator = $operatorManager->createOperator($operatorArray); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return $operator; |
|
|
|
|
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: