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->matchesDocument($this->filterData, $entry); |
42
|
|
|
|
43
|
|
|
return $isMatch; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
|
48
|
|
|
private function matchesDocument($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) |
|
|
|
|
71
|
|
|
{ |
72
|
|
|
$isMatch = false; |
73
|
|
|
print_r($filterValue); |
74
|
|
|
print_r($entryValue); |
75
|
|
|
|
76
|
|
|
|
77
|
|
|
// If both are a scalar ... |
78
|
|
|
if ($this->isScalar($filterValue) && $this->isScalar($entryValue)) { |
79
|
|
|
echo "Is scalar\n"; |
80
|
|
|
$isMatch = $filterValue === $entryValue; |
81
|
|
|
} |
82
|
|
|
// If both are a list ... |
83
|
|
|
if ($this->isList($filterValue) && $this->isList($entryValue)) { |
84
|
|
|
echo "Is list\n"; |
85
|
|
|
$isMatch = $filterValue === $entryValue; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
// If both are a document ... |
89
|
|
|
if ($this->isDocument($filterValue) && $this->isDocument($entryValue)) { |
90
|
|
|
echo "Is document\n"; |
91
|
|
|
$isMatch = $this->matchesDocument($filterValue, $entryValue); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
// If filter is an operator ... |
95
|
|
|
$operatorManager = new OperatorManager(); |
96
|
|
|
if ($operatorManager->isOperator($filterValue)) { |
97
|
|
|
echo "Is operator\n"; |
98
|
|
|
$operator = $operatorManager->buildOperator($filterValue); |
99
|
|
|
$isMatch = $operator->matches($entryValue); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $isMatch; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
|
106
|
|
|
|
107
|
|
|
private function isList($array) |
108
|
|
|
{ |
109
|
|
|
$isList = array() === $array || array_keys($array) === range(0, count($array) - 1); |
110
|
|
|
|
111
|
|
|
return $isList; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
|
115
|
|
|
|
116
|
|
|
private function isDocument($array) |
117
|
|
|
{ |
118
|
|
|
$isDocument = is_array($array) && !$this->isList($array); |
119
|
|
|
|
120
|
|
|
return $isDocument; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
|
124
|
|
|
|
125
|
|
|
private function isScalar($scalar) |
126
|
|
|
{ |
127
|
|
|
$isScalar = !is_array($scalar); |
128
|
|
|
|
129
|
|
|
return $isScalar; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|