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 StringFilter.php |
36
|
|
|
* |
37
|
|
|
* The String 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 Platine\Template\Parser\AbstractFilter; |
53
|
|
|
use Traversable; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Class StringFilter |
57
|
|
|
* @package Platine\Template\Filter |
58
|
|
|
*/ |
59
|
|
|
class StringFilter extends AbstractFilter |
60
|
|
|
{ |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Return the length of string or array |
64
|
|
|
* @param mixed $variable |
65
|
|
|
* @return int|mixed |
66
|
|
|
*/ |
67
|
|
|
public static function length($variable) |
68
|
|
|
{ |
69
|
|
|
if ($variable instanceof Traversable) { |
70
|
|
|
return iterator_count($variable); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if (is_array($variable)) { |
74
|
|
|
return count($variable); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if (is_object($variable)) { |
78
|
|
|
if (method_exists($variable, 'size')) { |
79
|
|
|
return $variable->size(); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
if (!is_string($variable)) { |
84
|
|
|
return $variable; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
if (function_exists('mb_strlen')) { |
88
|
|
|
return mb_strlen($variable); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return strlen($variable); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Add one string to another |
96
|
|
|
* @param mixed $variable |
97
|
|
|
* @param mixed $value |
98
|
|
|
* @return string|mixed |
99
|
|
|
*/ |
100
|
|
|
public static function append($variable, $value) |
101
|
|
|
{ |
102
|
|
|
if (!is_string($variable) || !is_string($value)) { |
103
|
|
|
return $variable; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $variable . $value; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Prefix a string to variable |
111
|
|
|
* @param mixed $variable |
112
|
|
|
* @param mixed $value |
113
|
|
|
* @return string|mixed |
114
|
|
|
*/ |
115
|
|
|
public static function prepend($variable, $value) |
116
|
|
|
{ |
117
|
|
|
if (!is_string($variable) || !is_string($value)) { |
118
|
|
|
return $variable; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return $value . $variable; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Remove a string to variable |
126
|
|
|
* @param mixed $variable |
127
|
|
|
* @param mixed $value |
128
|
|
|
* @return string|mixed |
129
|
|
|
*/ |
130
|
|
|
public static function remove($variable, $value) |
131
|
|
|
{ |
132
|
|
|
if (!is_string($variable) || !is_string($value)) { |
133
|
|
|
return $variable; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return str_replace($value, '', $variable); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Replace occurrences of a string with another |
141
|
|
|
* @param mixed $variable |
142
|
|
|
* @param mixed $value |
143
|
|
|
* @param mixed $replacement |
144
|
|
|
* @return string|mixed |
145
|
|
|
*/ |
146
|
|
|
public static function replace($variable, $value, $replacement = '') |
147
|
|
|
{ |
148
|
|
|
if (!is_string($variable) || !is_string($value) || !is_string($replacement)) { |
149
|
|
|
return $variable; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return str_replace($value, $replacement, $variable); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Truncate a string down to x characters |
157
|
|
|
* @param mixed $variable |
158
|
|
|
* @param int|mixed $count |
159
|
|
|
* @param string|mixed $ending |
160
|
|
|
* @return string|mixed |
161
|
|
|
*/ |
162
|
|
|
public static function truncate($variable, $count = 100, $ending = '...') |
163
|
|
|
{ |
164
|
|
|
if (!is_string($variable) || !is_string($ending) || !is_numeric($count)) { |
165
|
|
|
return $variable; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
$numberChar = (int) $count; |
169
|
|
|
if (strlen($variable) > $numberChar) { |
170
|
|
|
return substr($variable, 0, $numberChar) . $ending; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
return $variable; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Truncate string down to x words |
178
|
|
|
* @param mixed $variable |
179
|
|
|
* @param int|mixed $count |
180
|
|
|
* @param string|mixed $ending |
181
|
|
|
* @return string|mixed |
182
|
|
|
*/ |
183
|
|
|
public static function truncateWord($variable, $count = 3, $ending = '...') |
184
|
|
|
{ |
185
|
|
|
if (!is_string($variable) || !is_string($ending) || !is_numeric($count)) { |
186
|
|
|
return $variable; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
$numberWords = (int) $count; |
190
|
|
|
$wordList = explode(' ', $variable); |
191
|
|
|
if (count($wordList) > $numberWords) { |
192
|
|
|
return implode(' ', array_slice($wordList, 0, $numberWords)) . $ending; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
return $variable; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Put all letters to upper case |
200
|
|
|
* @param mixed $variable |
201
|
|
|
* @return string|mixed |
202
|
|
|
*/ |
203
|
|
|
public static function upper($variable) |
204
|
|
|
{ |
205
|
|
|
if (!is_string($variable)) { |
206
|
|
|
return $variable; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
if (function_exists('mb_strtoupper')) { |
210
|
|
|
return mb_strtoupper($variable); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
return strtoupper($variable); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* URL encodes a string |
218
|
|
|
* @param mixed $variable |
219
|
|
|
* @return string|mixed |
220
|
|
|
*/ |
221
|
|
|
public static function urlEncode($variable) |
222
|
|
|
{ |
223
|
|
|
if (!is_string($variable)) { |
224
|
|
|
return $variable; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
return urlencode($variable); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Decodes a URL-encoded string |
232
|
|
|
* @param mixed $variable |
233
|
|
|
* @return string|mixed |
234
|
|
|
*/ |
235
|
|
|
public static function urlDecode($variable) |
236
|
|
|
{ |
237
|
|
|
if (!is_string($variable)) { |
238
|
|
|
return $variable; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
return urldecode($variable); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Explicit string conversion. |
246
|
|
|
* @param mixed $variable |
247
|
|
|
* @return string|mixed |
248
|
|
|
*/ |
249
|
|
|
public static function stringfy($variable) |
250
|
|
|
{ |
251
|
|
|
if (is_array($variable)) { |
252
|
|
|
return $variable; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
return strval($variable); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* Split input string into an array of sub |
260
|
|
|
* strings separated by given pattern. |
261
|
|
|
* @param string|mixed $variable |
262
|
|
|
* @param mixed $pattern |
263
|
|
|
* @return array<mixed>|mixed |
264
|
|
|
*/ |
265
|
|
|
public static function split($variable, $pattern) |
266
|
|
|
{ |
267
|
|
|
if (!is_string($variable) || !is_string($pattern)) { |
268
|
|
|
return $variable; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
return explode($pattern, $variable); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* Pseudo-filter: negates auto-added escape filter |
276
|
|
|
* @param mixed $variable |
277
|
|
|
* @return string|mixed |
278
|
|
|
*/ |
279
|
|
|
public static function raw($variable) |
280
|
|
|
{ |
281
|
|
|
return $variable; |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* Escape a string |
286
|
|
|
* @param mixed $variable |
287
|
|
|
* @return string|mixed |
288
|
|
|
*/ |
289
|
|
|
public static function escape($variable) |
290
|
|
|
{ |
291
|
|
|
if (!is_string($variable)) { |
292
|
|
|
return $variable; |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
return htmlentities($variable, ENT_QUOTES); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* Escape a string once, keeping all previous HTML entities intact |
300
|
|
|
* @param mixed $variable |
301
|
|
|
* @return string|mixed |
302
|
|
|
*/ |
303
|
|
|
public static function escapeOnce($variable) |
304
|
|
|
{ |
305
|
|
|
if (!is_string($variable)) { |
306
|
|
|
return $variable; |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
return htmlentities($variable, ENT_QUOTES, null, false); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* Set default value if is blank |
314
|
|
|
* @param mixed $variable |
315
|
|
|
* @param mixed $value |
316
|
|
|
* @return mixed |
317
|
|
|
*/ |
318
|
|
|
public static function defaultValue($variable, $value) |
319
|
|
|
{ |
320
|
|
|
$isBlank = (is_string($variable) && $variable === '') |
321
|
|
|
|| is_bool($variable) && $variable === false |
322
|
|
|
|| $variable === null; |
323
|
|
|
|
324
|
|
|
|
325
|
|
|
return $isBlank ? $value : $variable; |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* Join elements of an array with a given |
330
|
|
|
* character between them |
331
|
|
|
* @param array<mixed>|Traversable|mixed $variable |
332
|
|
|
* @param mixed $glue |
333
|
|
|
* @return string|mixed |
334
|
|
|
*/ |
335
|
|
|
public static function join($variable, $glue = ' ') |
336
|
|
|
{ |
337
|
|
|
if (!is_string($glue)) { |
338
|
|
|
return $variable; |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
if ($variable instanceof Traversable) { |
342
|
|
|
$str = ''; |
343
|
|
|
foreach ($variable as $element) { |
344
|
|
|
if ($str) { |
345
|
|
|
$str .= $glue; |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
$str .= $element; |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
return $str; |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
return is_array($variable) |
355
|
|
|
? implode($glue, $variable) |
356
|
|
|
: $variable; |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
/** |
360
|
|
|
* Put all letter to lower case |
361
|
|
|
* @param mixed $variable |
362
|
|
|
* @return string|mixed |
363
|
|
|
*/ |
364
|
|
|
public static function lower($variable) |
365
|
|
|
{ |
366
|
|
|
if (!is_string($variable)) { |
367
|
|
|
return $variable; |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
if (function_exists('mb_strtolower')) { |
371
|
|
|
return mb_strtolower($variable); |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
return strtolower($variable); |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
/** |
378
|
|
|
* Capitalize words in the input sentence |
379
|
|
|
* @param mixed $variable |
380
|
|
|
* @return string|mixed |
381
|
|
|
*/ |
382
|
|
|
public static function capitalize($variable) |
383
|
|
|
{ |
384
|
|
|
if (!is_string($variable)) { |
385
|
|
|
return $variable; |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
return (string) preg_replace_callback( |
389
|
|
|
'/(^|[^\p{L}\'])([\p{Ll}])/u', |
390
|
|
|
function ($matches) { |
391
|
|
|
return $matches[1] . ucfirst($matches[2]); |
392
|
|
|
}, |
393
|
|
|
ucwords($variable) |
394
|
|
|
); |
395
|
|
|
} |
396
|
|
|
|
397
|
|
|
/** |
398
|
|
|
* Remove the left blank characters |
399
|
|
|
* @param mixed $variable |
400
|
|
|
* @return string|mixed |
401
|
|
|
*/ |
402
|
|
|
public static function lstrip($variable) |
403
|
|
|
{ |
404
|
|
|
if (!is_string($variable)) { |
405
|
|
|
return $variable; |
406
|
|
|
} |
407
|
|
|
|
408
|
|
|
return ltrim($variable); |
409
|
|
|
} |
410
|
|
|
|
411
|
|
|
/** |
412
|
|
|
* Remove the right blank characters |
413
|
|
|
* @param mixed $variable |
414
|
|
|
* @return string|mixed |
415
|
|
|
*/ |
416
|
|
|
public static function rstrip($variable) |
417
|
|
|
{ |
418
|
|
|
if (!is_string($variable)) { |
419
|
|
|
return $variable; |
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
return rtrim($variable); |
423
|
|
|
} |
424
|
|
|
|
425
|
|
|
/** |
426
|
|
|
* Remove the left and right blank characters |
427
|
|
|
* @param mixed $variable |
428
|
|
|
* @return string|mixed |
429
|
|
|
*/ |
430
|
|
|
public static function strip($variable) |
431
|
|
|
{ |
432
|
|
|
if (!is_string($variable)) { |
433
|
|
|
return $variable; |
434
|
|
|
} |
435
|
|
|
|
436
|
|
|
return trim($variable); |
437
|
|
|
} |
438
|
|
|
|
439
|
|
|
/** |
440
|
|
|
* Removes HTML tags from text |
441
|
|
|
* @param mixed $variable |
442
|
|
|
* @return string|mixed |
443
|
|
|
*/ |
444
|
|
|
public static function stripHtml($variable) |
445
|
|
|
{ |
446
|
|
|
if (!is_string($variable)) { |
447
|
|
|
return $variable; |
448
|
|
|
} |
449
|
|
|
|
450
|
|
|
return strip_tags($variable); |
451
|
|
|
} |
452
|
|
|
|
453
|
|
|
/** |
454
|
|
|
* Strip all newlines (\n, \r) from string |
455
|
|
|
* @param mixed $variable |
456
|
|
|
* @return string|mixed |
457
|
|
|
*/ |
458
|
|
|
public static function stripNewLine($variable) |
459
|
|
|
{ |
460
|
|
|
if (!is_string($variable)) { |
461
|
|
|
return $variable; |
462
|
|
|
} |
463
|
|
|
|
464
|
|
|
return str_replace(["\n", "\r"], '', $variable); |
465
|
|
|
} |
466
|
|
|
} |
467
|
|
|
|