|
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\Operator; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* The operator ElemMatch matches documents that contain an array field with at least one element |
|
28
|
|
|
* that matches all the specified filter criteria. |
|
29
|
|
|
* |
|
30
|
|
|
* There are three types of possible operand values: |
|
31
|
|
|
* |
|
32
|
|
|
* 1. A single operator, e.g. {"list":{"$elemMatch":{"$lte":100}}} |
|
33
|
|
|
* |
|
34
|
|
|
* 2. An array of operators, e.g. {"list":{"$elemMatch":{"$lte":100,"$ne":55}}} |
|
35
|
|
|
* Matches all documents that have at least on element in the field "list" |
|
36
|
|
|
* that is a number below or equal to 100 and is not equal to 55. |
|
37
|
|
|
* |
|
38
|
|
|
* 2. |
|
39
|
|
|
*/ |
|
40
|
|
|
class ElemMatch extends AbstractOperator |
|
41
|
|
|
{ |
|
42
|
|
|
public function matches($value) |
|
43
|
|
|
{ |
|
44
|
|
|
// $value has to be an indexed array |
|
45
|
|
|
if ($this->isList($value)) { |
|
46
|
|
|
$isMatch = false; |
|
47
|
|
|
foreach ($value as $listElement) { |
|
48
|
|
|
$isMatch |= $this->matchesListElement($listElement); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
} else { |
|
52
|
|
|
$isMatch = false; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
return $isMatch; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
public function isValid() |
|
62
|
|
|
{ |
|
63
|
|
|
$isValid = $this->isNumber($this->operand); |
|
64
|
|
|
|
|
65
|
|
|
return $isValid; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
private function matchesListElement($listElement) |
|
|
|
|
|
|
71
|
|
|
{ |
|
72
|
|
|
$isMatch = $this->operand === $listElement; |
|
73
|
|
|
|
|
74
|
|
|
return $isMatch; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
private function isList($array) |
|
80
|
|
|
{ |
|
81
|
|
|
$isList = array() === $array || array_keys($array) === range(0, count($array) - 1); |
|
82
|
|
|
|
|
83
|
|
|
return $isList; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|