1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace allejo\stakx\Twig; |
4
|
|
|
|
5
|
|
|
use Twig_Error_Syntax; |
6
|
|
|
|
7
|
|
|
class WhereFilter |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @param array|\ArrayAccess[] $array The elements to filter through |
11
|
|
|
* @param string $key The key value in an associative array or FrontMatter |
12
|
|
|
* @param string $comparison The actual comparison symbols being used |
13
|
|
|
* @param mixed $value The value we're searching for |
14
|
|
|
* |
15
|
|
|
* @return array |
16
|
|
|
*/ |
17
|
21 |
|
public function __invoke ($array, $key, $comparison, $value) |
18
|
|
|
{ |
19
|
21 |
|
$results = array(); |
20
|
21 |
|
$this->search_r($array, $key, $comparison, $value, $results); |
21
|
|
|
|
22
|
20 |
|
return $results; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @return \Twig_SimpleFilter |
27
|
|
|
*/ |
28
|
2 |
|
public static function get () |
29
|
|
|
{ |
30
|
2 |
|
return new \Twig_SimpleFilter('where', new self()); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Recursive searching calling our comparison |
35
|
|
|
* |
36
|
|
|
* @param array|\ArrayAccess[] $array The elements to filter through |
37
|
|
|
* @param string $key The key value in an associative array or FrontMatter |
38
|
|
|
* @param string $comparison The actual comparison symbols being used |
39
|
|
|
* @param string $value The value we're searching for |
40
|
|
|
* @param array $results The reference to where to keep the filtered elements |
41
|
|
|
*/ |
42
|
21 |
|
private function search_r ($array, $key, $comparison, $value, &$results) |
|
|
|
|
43
|
|
|
{ |
44
|
21 |
|
if (!is_array($array) && !($array instanceof \ArrayAccess)) |
45
|
21 |
|
{ |
46
|
19 |
|
return; |
47
|
|
|
} |
48
|
|
|
|
49
|
21 |
|
if ($this->compare($array, $key, $comparison, $value)) |
50
|
21 |
|
{ |
51
|
15 |
|
$results[] = $array; |
52
|
15 |
|
} |
53
|
|
|
|
54
|
21 |
|
foreach ($array as $subarray) |
55
|
|
|
{ |
56
|
21 |
|
$this->search_r($subarray, $key, $comparison, $value, $results); |
57
|
20 |
|
} |
58
|
20 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* The logic for determining if an element matches the filter |
62
|
|
|
* |
63
|
|
|
* @param array|\ArrayAccess[] $array The elements to filter through |
64
|
|
|
* @param string $key The key value in an associative array or FrontMatter |
65
|
|
|
* @param string $comparison The actual comparison symbols being used |
66
|
|
|
* @param mixed $value The value we're searching for |
67
|
|
|
* |
68
|
|
|
* @return bool |
69
|
|
|
* |
70
|
|
|
* @throws Twig_Error_Syntax |
71
|
|
|
*/ |
72
|
21 |
|
private function compare ($array, $key, $comparison, $value) |
73
|
|
|
{ |
74
|
21 |
|
if (!isset($array[$key])) |
75
|
21 |
|
{ |
76
|
21 |
|
return false; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
switch ($comparison) |
80
|
|
|
{ |
81
|
20 |
|
case "==": return ($array[$key] === $value); |
|
|
|
|
82
|
14 |
|
case "!=": return ($array[$key] !== $value); |
|
|
|
|
83
|
11 |
|
case ">" : return ($array[$key] > $value); |
|
|
|
|
84
|
9 |
|
case ">=": return ($array[$key] >= $value); |
|
|
|
|
85
|
7 |
|
case "<" : return ($array[$key] < $value); |
|
|
|
|
86
|
5 |
|
case "<=": return ($array[$key] <= $value); |
|
|
|
|
87
|
3 |
|
case "~=": return ($this->contains($array[$key], $value)); |
|
|
|
|
88
|
|
|
|
89
|
1 |
|
default: |
90
|
1 |
|
throw new Twig_Error_Syntax("Invalid where comparison ({$comparison})"); |
91
|
1 |
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
2 |
|
private function contains ($haystack, $needle) |
95
|
|
|
{ |
96
|
2 |
|
return ((is_array($haystack) && in_array($needle, $haystack)) || |
97
|
2 |
|
(is_string($haystack) && strpos($haystack, $needle) !== false)); |
98
|
|
|
} |
99
|
|
|
} |
This check looks for method names that are not written in camelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes
databaseConnectionSeeker
.