StringFilter::split()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
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   https://www.platine-php.com
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 mixed
65
     */
66
    public static function length(mixed $variable): mixed
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 mixed
98
     */
99
    public static function append(mixed $variable, mixed $value): mixed
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 mixed
113
     */
114
    public static function prepend(mixed $variable, mixed $value): mixed
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 string $variable
126
     * @param string $value
127
     * @return string
128
     */
129
    public static function remove(string $variable, string $value): string
130
    {
131
        return str_replace($value, '', $variable);
132
    }
133
134
    /**
135
     * Replace occurrences of a string with another
136
     * @param string $variable
137
     * @param string $value
138
     * @param string $replacement
139
     * @return string
140
     */
141
    public static function replace(
142
        string $variable,
143
        string $value,
144
        string $replacement = ''
145
    ): string {
146
        return str_replace($value, $replacement, $variable);
147
    }
148
149
    /**
150
     * Truncate a string down to x characters
151
     * @param string $variable
152
     * @param int|string $count
153
     * @param string $ending
154
     * @return string
155
     */
156
    public static function truncate(
157
        string $variable,
158
        int|string $count = 100,
159
        string $ending = '...'
160
    ): string {
161
        $numberChar = (int) $count;
162
        if (strlen($variable) > $numberChar) {
163
            return substr($variable, 0, $numberChar) . $ending;
164
        }
165
166
        return $variable;
167
    }
168
169
    /**
170
     * Truncate string down to x words
171
     * @param string $variable
172
     * @param int|string $count
173
     * @param string $ending
174
     * @return string
175
     */
176
    public static function truncateWord(
177
        string $variable,
178
        int|string $count = 3,
179
        string $ending = '...'
180
    ): string {
181
        if (!is_numeric($count)) {
182
            return $variable;
183
        }
184
185
        $numberWords = (int) $count;
186
        $wordList = explode(' ', $variable);
187
        if (count($wordList) > $numberWords) {
188
            return implode(' ', array_slice($wordList, 0, $numberWords)) . $ending;
189
        }
190
191
        return $variable;
192
    }
193
194
    /**
195
     * Put all letters to upper case
196
     * @param string $variable
197
     * @return string
198
     */
199
    public static function upper(string $variable): string
200
    {
201
        if (function_exists('mb_strtoupper')) {
202
            return mb_strtoupper($variable);
203
        }
204
205
        return strtoupper($variable);
206
    }
207
208
    /**
209
     * URL encodes a string
210
     * @param string $variable
211
     * @return string
212
     */
213
    public static function urlEncode(string $variable): string
214
    {
215
        return urlencode($variable);
216
    }
217
218
    /**
219
     * Decodes a URL-encoded string
220
     * @param string $variable
221
     * @return string
222
     */
223
    public static function urlDecode(string $variable): string
224
    {
225
        return urldecode($variable);
226
    }
227
228
    /**
229
     * Explicit string conversion.
230
     * @param mixed $variable
231
     * @return array<mixed>|string
232
     */
233
    public static function stringfy(mixed $variable): array|string
234
    {
235
        if (is_array($variable)) {
236
            return $variable;
237
        }
238
239
        return strval($variable);
240
    }
241
242
    /**
243
     * Split input string into an array of sub
244
     * strings separated by given pattern.
245
     * @param string $variable
246
     * @param string $pattern
247
     * @return array<mixed>
248
     */
249
    public static function split(string $variable, string $pattern): array
250
    {
251
        if (empty($pattern)) {
252
            return [$variable];
253
        }
254
255
        return explode($pattern, $variable);
256
    }
257
258
    /**
259
     * If the given value is part of the variable
260
     * @param string $variable
261
     * @param string $value
262
     * @return int|false
263
     */
264
    public static function find(string $variable, string $value): int|false
265
    {
266
        return strpos($variable, $value);
267
    }
268
269
    /**
270
     * Pseudo-filter: negates auto-added escape filter
271
     * @param mixed $variable
272
     * @return mixed
273
     */
274
    public static function raw(mixed $variable): mixed
275
    {
276
        return $variable;
277
    }
278
279
    /**
280
     * Escape a string
281
     * @param string|null $variable
282
     * @return string|null
283
     */
284
    public static function escape(?string $variable): ?string
285
    {
286
        if ($variable === null) {
287
            return null;
288
        }
289
290
        return htmlspecialchars($variable, ENT_QUOTES);
291
    }
292
293
    /**
294
     * Escape a string once, keeping all previous HTML entities intact
295
     * @param string|null $variable
296
     * @return string|null
297
     */
298
    public static function escapeOnce(?string $variable): ?string
299
    {
300
        if ($variable === null) {
301
            return null;
302
        }
303
304
        return htmlentities($variable, ENT_QUOTES, null, false);
305
    }
306
307
    /**
308
     * Set default value if is blank
309
     * @param mixed $variable
310
     * @param mixed $value
311
     * @return mixed
312
     */
313
    public static function defaultValue(mixed $variable, mixed $value): mixed
314
    {
315
        $isBlank = (is_string($variable) && $variable === '')
316
                    || is_bool($variable) && $variable === false
317
                    || $variable === null;
318
319
320
        return $isBlank ? $value : $variable;
321
    }
322
323
    /**
324
     * Join elements of an array with a given
325
     * character between them
326
     * @param array<mixed>|Traversable|iterable<mixed> $variable
327
     * @param string $glue
328
     * @return string
329
     */
330
    public static function join(iterable $variable, string $glue = ' '): string
331
    {
332
        if ($variable instanceof Traversable) {
333
            $str = '';
334
            foreach ($variable as $element) {
335
                if ($str) {
336
                    $str .= $glue;
337
                }
338
339
                $str .= $element;
340
            }
341
342
            return $str;
343
        }
344
345
        return implode($glue, $variable);
0 ignored issues
show
Bug introduced by
$variable of type iterable is incompatible with the type array expected by parameter $pieces of implode(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

345
        return implode($glue, /** @scrutinizer ignore-type */ $variable);
Loading history...
346
    }
347
348
    /**
349
     * Put all letter to lower case
350
     * @param string $variable
351
     * @return string
352
     */
353
    public static function lower(string $variable): string
354
    {
355
        if (function_exists('mb_strtolower')) {
356
            return mb_strtolower($variable);
357
        }
358
359
        return strtolower($variable);
360
    }
361
362
    /**
363
     * Capitalize words in the input sentence
364
     * @param string $variable
365
     * @return string
366
     */
367
    public static function capitalize(string $variable): string
368
    {
369
        return (string) preg_replace_callback(
370
            '/(^|[^\p{L}\'])([\p{Ll}])/u',
371
            function ($matches) {
372
                return $matches[1] . ucfirst($matches[2]);
373
            },
374
            ucwords($variable)
375
        );
376
    }
377
378
    /**
379
     * Remove the left blank characters
380
     * @param string $variable
381
     * @return string
382
     */
383
    public static function lstrip(string $variable): string
384
    {
385
        return ltrim($variable);
386
    }
387
388
    /**
389
     * Remove the right blank characters
390
     * @param string $variable
391
     * @return string
392
     */
393
    public static function rstrip(string $variable): string
394
    {
395
        return rtrim($variable);
396
    }
397
398
    /**
399
     * Remove the left and right blank characters
400
     * @param string $variable
401
     * @return string
402
     */
403
    public static function strip(string $variable): string
404
    {
405
        return trim($variable);
406
    }
407
408
    /**
409
     * Removes HTML tags from text
410
     * @param string $variable
411
     * @return string
412
     */
413
    public static function stripHtml(string $variable): string
414
    {
415
        return strip_tags($variable);
416
    }
417
418
    /**
419
     * Strip all newlines (\n, \r) from string
420
     * @param string $variable
421
     * @return string
422
     */
423
    public static function stripNewLine(string $variable): string
424
    {
425
        return str_replace(["\n", "\r"], '', $variable);
426
    }
427
}
428