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
|
|
|
use EmberDb\Logger; |
27
|
|
|
|
28
|
|
|
class Filter |
29
|
|
|
{ |
30
|
|
|
private $filterData = array(); |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
|
34
|
|
|
public function __construct(array $filterArray) |
35
|
|
|
{ |
36
|
|
|
$this->filterData = $filterArray; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
|
41
|
|
|
public function matchesEntry(array $entry) |
42
|
|
|
{ |
43
|
|
|
$isMatch = $this->matchesDocument($this->filterData, $entry); |
44
|
|
|
|
45
|
|
|
return $isMatch; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
|
50
|
|
|
private function matchesDocument(array $filterArray, array $entryArray) |
51
|
|
|
{ |
52
|
|
|
$isMatch = true; |
53
|
|
|
foreach ($filterArray as $filterKey => $filterValue) { |
54
|
|
|
|
55
|
|
|
// Check if the filter key exists and fetch corresponding value from entry |
56
|
|
|
if (array_key_exists($filterKey, $entryArray)) { |
57
|
|
|
$entryValue = $entryArray[$filterKey]; |
58
|
|
|
} else { |
59
|
|
|
$isMatch = false; |
60
|
|
|
break; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
// Check if the values match |
64
|
|
|
$isMatch &= $this->matchesValue($filterValue, $entryValue); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $isMatch; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
|
71
|
|
|
|
72
|
|
|
private function matchesValue($filterValue, $entryValue) |
|
|
|
|
73
|
|
|
{ |
74
|
|
|
$isMatch = false; |
75
|
|
|
|
76
|
|
|
// If both are a scalar ... |
77
|
|
View Code Duplication |
if ($this->isScalar($filterValue) && $this->isScalar($entryValue)) { |
|
|
|
|
78
|
|
|
Logger::log("query:" . $filterValue . " and entry:" . $entryValue . " are both scalars.\n"); |
79
|
|
|
$isMatch = $filterValue === $entryValue; |
80
|
|
|
} |
81
|
|
|
// If both are a list ... |
82
|
|
View Code Duplication |
if ($this->isList($filterValue) && $this->isList($entryValue)) { |
|
|
|
|
83
|
|
|
Logger::log("query:" . json_encode($filterValue) . " and entry:" . json_encode($entryValue) . " are both lists.\n"); |
84
|
|
|
$isMatch = $filterValue === $entryValue; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
// If both are a document ... |
88
|
|
|
if ($this->isDocument($filterValue) && $this->isDocument($entryValue)) { |
89
|
|
|
Logger::log("query:" . json_encode($filterValue) . " and entry:" . json_encode($entryValue) . " are both documents.\n"); |
90
|
|
|
|
91
|
|
|
// If filter is an operator ... |
92
|
|
|
if ($this->isOperator($filterValue)) { |
93
|
|
|
Logger::log("Is operator\n"); |
94
|
|
|
$operatorManager = new OperatorManager(); |
95
|
|
|
$operator = $operatorManager->buildOperator($filterValue); |
96
|
|
|
$isMatch = $operator->matches($entryValue); |
97
|
|
|
} else { |
98
|
|
|
$isMatch = $this->matchesDocument($filterValue, $entryValue); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
|
103
|
|
|
return $isMatch; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
|
107
|
|
|
|
108
|
|
|
private function isList($array) |
109
|
|
|
{ |
110
|
|
|
$isList = is_array($array) && (array() === $array || array_keys($array) === range(0, count($array) - 1)); |
111
|
|
|
|
112
|
|
|
return $isList; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
|
116
|
|
|
|
117
|
|
|
private function isDocument($array) |
118
|
|
|
{ |
119
|
|
|
$isDocument = is_array($array) && !$this->isList($array); |
120
|
|
|
|
121
|
|
|
return $isDocument; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
|
125
|
|
|
|
126
|
|
|
private function isScalar($scalar) |
127
|
|
|
{ |
128
|
|
|
$isScalar = !is_array($scalar); |
129
|
|
|
|
130
|
|
|
return $isScalar; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
|
134
|
|
|
|
135
|
|
|
private function isOperator($array) |
136
|
|
|
{ |
137
|
|
|
if ($this->isDocument($array)) { |
138
|
|
|
$firstKey = array_keys($array)[0]; |
139
|
|
|
$isOperator = strpos($firstKey, '$') === 0; |
140
|
|
|
} else { |
141
|
|
|
$isOperator = false; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return $isOperator; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|