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