Passed
Push — develop ( 7c7957...55acaa )
by nguereza
02:50
created

StringFilter::find()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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