|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace allejo\stakx\Twig; |
|
4
|
|
|
|
|
5
|
|
|
use allejo\stakx\Object\ContentItem; |
|
6
|
|
|
|
|
7
|
|
|
class WhereFilter |
|
8
|
|
|
{ |
|
9
|
20 |
|
public function __invoke ($array, $key, $comparison, $value) |
|
10
|
|
|
{ |
|
11
|
20 |
|
$results = array(); |
|
12
|
20 |
|
$this->search_r($array, $key, $comparison, $value, $results); |
|
13
|
|
|
|
|
14
|
20 |
|
return $results; |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
1 |
|
public static function get () |
|
18
|
|
|
{ |
|
19
|
1 |
|
return new \Twig_SimpleFilter('where', new self()); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
20 |
|
private function search_r ($array, $key, $comparison, $value, &$results) |
|
|
|
|
|
|
23
|
|
|
{ |
|
24
|
20 |
|
if (!is_array($array) && !($array instanceof ContentItem)) |
|
25
|
20 |
|
{ |
|
26
|
20 |
|
return; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
20 |
|
if ($this->compare($array, $key, $comparison, $value)) |
|
30
|
20 |
|
{ |
|
31
|
14 |
|
$results[] = $array; |
|
32
|
14 |
|
} |
|
33
|
|
|
|
|
34
|
20 |
|
foreach ($array as $subarray) |
|
|
|
|
|
|
35
|
|
|
{ |
|
36
|
20 |
|
$this->search_r($subarray, $key, $comparison, $value, $results); |
|
37
|
20 |
|
} |
|
38
|
20 |
|
} |
|
39
|
|
|
|
|
40
|
20 |
|
private function compare ($array, $key, $comparison, $value) |
|
41
|
|
|
{ |
|
42
|
20 |
|
$array = ($array instanceof ContentItem) ? $array->getFrontMatter() : $array; |
|
43
|
|
|
|
|
44
|
20 |
|
if (!isset($array[$key])) |
|
45
|
20 |
|
{ |
|
46
|
20 |
|
return false; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
19 |
|
return (($comparison === "==" && $array[$key] === $value) || |
|
50
|
19 |
|
($comparison === "!=" && $array[$key] !== $value) || |
|
51
|
19 |
|
($comparison === ">" && $array[$key] > $value) || |
|
52
|
19 |
|
($comparison === ">=" && $array[$key] >= $value) || |
|
53
|
19 |
|
($comparison === "<" && $array[$key] < $value) || |
|
54
|
19 |
|
($comparison === "<=" && $array[$key] <= $value) || |
|
55
|
19 |
|
($comparison === "~=" && $this->contains($array[$key], $value))); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
2 |
|
private function contains ($haystack, $needle) |
|
59
|
|
|
{ |
|
60
|
2 |
|
return ((is_array($haystack) && in_array($needle, $haystack)) || |
|
61
|
2 |
|
(is_string($haystack) && strpos($haystack, $needle) !== false)); |
|
62
|
|
|
} |
|
63
|
|
|
} |
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.