Repository navigation
Expand file tree
/
Copy pathQueryMatchFilter.php
More file actions
120 lines (96 loc) · 4.26 KB
/
Copy pathQueryMatchFilter.php
File metadata and controls
120 lines (96 loc) · 4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
/**
* JSONPath implementation for PHP.
*
* @license https://github.com/SoftCreatR/JSONPath/blob/main/LICENSE MIT License
*/
namespace Flow\JSONPath\Filters;
use Flow\JSONPath\AccessHelper;
use RuntimeException;
class QueryMatchFilter extends AbstractFilter
{
const MATCH_QUERY_OPERATORS = '
@(\.(?<key>[^\s<>!=]+)|\[["\']?(?<keySquare>.*?)["\']?\])
(\s*(?<operator>==|=~|=|<>|!==|!=|>=|<=|>|<|in|!in|nin)\s*(?<comparisonValue>.+))?
';
/**
* @inheritDoc
*/
public function filter($collection)
{
preg_match('/^' . static::MATCH_QUERY_OPERATORS . '$/x', $this->token->value, $matches);
if (!isset($matches[1])) {
throw new RuntimeException('Malformed filter query');
}
$key = $matches['key'] ?: $matches['keySquare'];
if ($key === '') {
throw new RuntimeException('Malformed filter query: key was not set');
}
$operator = isset($matches['operator']) ? $matches['operator'] : null;
$comparisonValue = isset($matches['comparisonValue']) ? $matches['comparisonValue'] : null;
if (is_string($comparisonValue)) {
if (strpos($comparisonValue, "[") === 0 && substr($comparisonValue, -1) === "]") {
$comparisonValue = substr($comparisonValue, 1, -1);
$comparisonValue = preg_replace('/^[\'"]/', '', $comparisonValue);
$comparisonValue = preg_replace('/[\'"]$/', '', $comparisonValue);
$comparisonValue = preg_replace('/[\'"],[ ]*[\'"]/', ',', $comparisonValue);
$comparisonValue = explode(",", $comparisonValue);
} else {
$comparisonValue = preg_replace('/^[\'"]/', '', $comparisonValue);
$comparisonValue = preg_replace('/[\'"]$/', '', $comparisonValue);
if (strtolower($comparisonValue) === 'false') {
$comparisonValue = false;
} elseif (strtolower($comparisonValue) === 'true') {
$comparisonValue = true;
} elseif (strtolower($comparisonValue) === 'null') {
$comparisonValue = null;
}
}
}
$return = [];
foreach ($collection as $value) {
if (AccessHelper::keyExists($value, $key, $this->magicIsAllowed)) {
$value1 = AccessHelper::getValue($value, $key, $this->magicIsAllowed);
if ($operator === null && $value1) {
$return[] = $value;
}
/** @noinspection TypeUnsafeComparisonInspection */
// phpcs:ignore -- This is a loose comparison by design.
if (($operator === '=' || $operator === '==') && $value1 == $comparisonValue) {
$return[] = $value;
}
/** @noinspection TypeUnsafeComparisonInspection */
// phpcs:ignore -- This is a loose comparison by design.
if (($operator === '!=' || $operator === '!==' || $operator === '<>') && $value1 != $comparisonValue) {
$return[] = $value;
}
if ($operator === '=~' && @preg_match($comparisonValue, $value1)) {
$return[] = $value;
}
if ($operator === '>' && $value1 > $comparisonValue) {
$return[] = $value;
}
if ($operator === '>=' && $value1 >= $comparisonValue) {
$return[] = $value;
}
if ($operator === '<' && $value1 < $comparisonValue) {
$return[] = $value;
}
if ($operator === '<=' && $value1 <= $comparisonValue) {
$return[] = $value;
}
if ($operator === 'in' && is_array($comparisonValue) && in_array($value1, $comparisonValue, true)) {
$return[] = $value;
}
if (
($operator === 'nin' || $operator === '!in') &&
is_array($comparisonValue) &&
!in_array($value1, $comparisonValue, true)
) {
$return[] = $value;
}
}
}
return $return;
}
}