1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Platine Template |
5
|
|
|
* |
6
|
|
|
* Platine Template is a template engine that has taken a lot of inspiration from Django. |
7
|
|
|
* |
8
|
|
|
* This content is released under the MIT License (MIT) |
9
|
|
|
* |
10
|
|
|
* Copyright (c) 2020 Platine Template |
11
|
|
|
* Copyright (c) 2014 Guz Alexander, http://guzalexander.com |
12
|
|
|
* Copyright (c) 2011, 2012 Harald Hanek, http://www.delacap.com |
13
|
|
|
* Copyright (c) 2006 Mateo Murphy |
14
|
|
|
* |
15
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
16
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
17
|
|
|
* in the Software without restriction, including without limitation the rights |
18
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
19
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
20
|
|
|
* furnished to do so, subject to the following conditions: |
21
|
|
|
* |
22
|
|
|
* The above copyright notice and this permission notice shall be included in all |
23
|
|
|
* copies or substantial portions of the Software. |
24
|
|
|
* |
25
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
26
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
27
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
28
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
29
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
30
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
31
|
|
|
* SOFTWARE. |
32
|
|
|
*/ |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @file NumberFilter.php |
36
|
|
|
* |
37
|
|
|
* The Array Filter class |
38
|
|
|
* |
39
|
|
|
* @package Platine\Template\Filter |
40
|
|
|
* @author Platine Developers Team |
41
|
|
|
* @copyright Copyright (c) 2020 |
42
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
43
|
|
|
* @link http://www.iacademy.cf |
44
|
|
|
* @version 1.0.0 |
45
|
|
|
* @filesource |
46
|
|
|
*/ |
47
|
|
|
|
48
|
|
|
declare(strict_types=1); |
49
|
|
|
|
50
|
|
|
namespace Platine\Template\Filter; |
51
|
|
|
|
52
|
|
|
use Iterator; |
53
|
|
|
use Platine\Stdlib\Helper\Json; |
54
|
|
|
use Platine\Template\Parser\AbstractFilter; |
55
|
|
|
use Traversable; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Class ArrayFilter |
59
|
|
|
* @package Platine\Template\Filter |
60
|
|
|
*/ |
61
|
|
|
class ArrayFilter extends AbstractFilter |
62
|
|
|
{ |
63
|
|
|
/** |
64
|
|
|
* Returns the first element of an array |
65
|
|
|
* @param array<mixed>|Iterator<mixed>|mixed $value |
66
|
|
|
* @return mixed |
67
|
|
|
*/ |
68
|
|
|
public static function first($value) |
69
|
|
|
{ |
70
|
|
|
if (is_array($value)) { |
71
|
|
|
return reset($value); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if ($value instanceof Iterator) { |
|
|
|
|
75
|
|
|
$value->rewind(); |
76
|
|
|
|
77
|
|
|
return $value->current(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $value; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Returns the last element of an array |
85
|
|
|
* @param array<mixed>|Traversable<mixed>|mixed $value |
86
|
|
|
* @return mixed |
87
|
|
|
*/ |
88
|
|
|
public static function last($value) |
89
|
|
|
{ |
90
|
|
|
if (is_array($value)) { |
91
|
|
|
return end($value); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
if ($value instanceof Traversable) { |
|
|
|
|
95
|
|
|
$last = null; |
96
|
|
|
foreach ($value as $elem) { |
97
|
|
|
$last = $elem; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $last; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return $value; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Sort an array. |
108
|
|
|
* @param array<int|string, mixed>|mixed $variable |
109
|
|
|
* @param mixed $property |
110
|
|
|
* @return mixed |
111
|
|
|
*/ |
112
|
|
|
public static function sort($variable, $property = null) |
113
|
|
|
{ |
114
|
|
|
if ($variable instanceof Traversable) { |
115
|
|
|
$variable = iterator_to_array($variable); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
if ($property === null) { |
119
|
|
|
asort($variable); |
120
|
|
|
} else { |
121
|
|
|
$first = reset($variable); |
122
|
|
|
if ($first !== false && is_array($first) && array_key_exists($property, $first)) { |
123
|
|
|
uasort($variable, function ($a, $b) use ($property) { |
124
|
|
|
return $a[$property] <=> $b[$property]; |
125
|
|
|
}); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return $variable; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Sort an array by key. |
134
|
|
|
* @param array<int|string, mixed>|mixed $variable |
135
|
|
|
* @return mixed |
136
|
|
|
*/ |
137
|
|
|
public static function sortKey($variable) |
138
|
|
|
{ |
139
|
|
|
if (is_array($variable)) { |
140
|
|
|
ksort($variable); |
141
|
|
|
return $variable; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return $variable; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Remove duplicate elements from an array |
149
|
|
|
* @param array<int|string, mixed>|mixed $variable |
150
|
|
|
* @return mixed |
151
|
|
|
*/ |
152
|
|
|
public static function unique($variable) |
153
|
|
|
{ |
154
|
|
|
if ($variable instanceof Traversable) { |
155
|
|
|
$variable = iterator_to_array($variable); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
return array_unique($variable); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Map/collect on a given property |
163
|
|
|
* @param array<mixed>|Traversable|mixed $variable |
164
|
|
|
* @param mixed $property |
165
|
|
|
* @return string|mixed |
166
|
|
|
*/ |
167
|
|
|
public static function map($variable, $property) |
168
|
|
|
{ |
169
|
|
|
if ($variable instanceof Traversable) { |
170
|
|
|
$variable = iterator_to_array($variable); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
if (!is_array($variable)) { |
174
|
|
|
return $variable; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
return array_map(function ($element) use ($property) { |
178
|
|
|
if (is_callable($element)) { |
179
|
|
|
return $element(); |
180
|
|
|
} elseif (is_array($element) && array_key_exists($property, $element)) { |
181
|
|
|
return $element[$property]; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
return null; |
185
|
|
|
}, |
186
|
|
|
$variable); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Reverse the elements of an array |
191
|
|
|
* @param array<mixed>|Traversable|mixed $variable |
192
|
|
|
* @return string|mixed |
193
|
|
|
*/ |
194
|
|
|
public static function reverse($variable) |
195
|
|
|
{ |
196
|
|
|
if ($variable instanceof Traversable) { |
197
|
|
|
$variable = iterator_to_array($variable); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
if (!is_array($variable)) { |
201
|
|
|
return $variable; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
return array_reverse($variable); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Return the JSON representation |
209
|
|
|
* @param mixed $variable |
210
|
|
|
* @return string |
211
|
|
|
*/ |
212
|
|
|
public static function json($variable) |
213
|
|
|
{ |
214
|
|
|
return Json::encode($variable); |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|