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
|
|
|
public function __invoke ($array, $key, $comparison, $value) |
18
|
21 |
|
{ |
19
|
|
|
$results = array(); |
20
|
21 |
|
$this->search_r($array, $key, $comparison, $value, $results); |
21
|
21 |
|
|
22
|
|
|
return $results; |
23
|
20 |
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @return \Twig_SimpleFilter |
27
|
|
|
*/ |
28
|
|
|
public static function get () |
29
|
2 |
|
{ |
30
|
|
|
return new \Twig_SimpleFilter('where', new self()); |
31
|
2 |
|
} |
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
|
|
|
private function search_r ($array, $key, $comparison, $value, &$results) |
|
|
|
|
43
|
21 |
|
{ |
44
|
|
|
if (!is_array($array) && !($array instanceof \ArrayAccess)) |
45
|
21 |
|
{ |
46
|
21 |
|
return; |
47
|
19 |
|
} |
48
|
|
|
|
49
|
|
|
if ($this->compare($array, $key, $comparison, $value)) |
50
|
21 |
|
{ |
51
|
21 |
|
$results[] = $array; |
52
|
15 |
|
} |
53
|
15 |
|
|
54
|
|
|
foreach ($array as $subarray) |
55
|
21 |
|
{ |
56
|
|
|
$this->search_r($subarray, $key, $comparison, $value, $results); |
57
|
21 |
|
} |
58
|
20 |
|
} |
59
|
20 |
|
|
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
|
|
|
private function compare ($array, $key, $comparison, $value) |
73
|
21 |
|
{ |
74
|
|
|
switch ($comparison) |
75
|
21 |
|
{ |
76
|
|
|
case "==": return ($array[$key] === $value); |
|
|
|
|
77
|
21 |
|
case "!=": return ($array[$key] !== $value); |
|
|
|
|
78
|
21 |
|
case ">" : return ($array[$key] > $value); |
|
|
|
|
79
|
1 |
|
case ">=": return ($array[$key] >= $value); |
|
|
|
|
80
|
1 |
|
case "<" : return ($array[$key] < $value); |
|
|
|
|
81
|
1 |
|
case "<=": return ($array[$key] <= $value); |
|
|
|
|
82
|
|
|
case "~=": return ($this->contains($array[$key], $value)); |
|
|
|
|
83
|
21 |
|
|
84
|
21 |
|
default: |
85
|
21 |
|
throw new Twig_Error_Syntax("Invalid where comparison ({$comparison})"); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
private function contains ($haystack, $needle) |
90
|
20 |
|
{ |
91
|
14 |
|
return ((is_array($haystack) && in_array($needle, $haystack)) || |
92
|
11 |
|
(is_string($haystack) && strpos($haystack, $needle) !== false)); |
93
|
|
|
} |
94
|
|
|
} |
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
.