Passed
Push — master ( 778d70...e2773e )
by Alexander
09:59
created

BaseStringHelper::mask()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 2
nop 4
dl 0
loc 13
ccs 8
cts 8
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @link https://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license https://www.yiiframework.com/license/
6
 */
7
8
namespace yii\helpers;
9
10
use Yii;
11
12
/**
13
 * BaseStringHelper provides concrete implementation for [[StringHelper]].
14
 *
15
 * Do not use BaseStringHelper. Use [[StringHelper]] instead.
16
 *
17
 * @author Qiang Xue <[email protected]>
18
 * @author Alex Makarov <[email protected]>
19
 * @since 2.0
20
 */
21
class BaseStringHelper
22
{
23
    /**
24
     * Returns the number of bytes in the given string.
25
     * This method ensures the string is treated as a byte array by using `mb_strlen()`.
26
     *
27
     * @param string $string the string being measured for length
28
     * @return int the number of bytes in the given string.
29
     */
30 423
    public static function byteLength($string)
31
    {
32 423
        return mb_strlen((string)$string, '8bit');
33
    }
34
35
    /**
36
     * Returns the portion of string specified by the start and length parameters.
37
     * This method ensures the string is treated as a byte array by using `mb_substr()`.
38
     *
39
     * @param string $string the input string. Must be one character or longer.
40
     * @param int $start the starting position
41
     * @param int|null $length the desired portion length. If not specified or `null`, there will be
42
     * no limit on length i.e. the output will be until the end of the string.
43
     * @return string the extracted part of string, or FALSE on failure or an empty string.
44
     * @see https://www.php.net/manual/en/function.substr.php
45
     */
46 153
    public static function byteSubstr($string, $start, $length = null)
47
    {
48 153
        if ($length === null) {
49 47
            $length = static::byteLength($string);
50
        }
51
52 153
        return mb_substr((string)$string, $start, $length, '8bit');
53
    }
54
55
    /**
56
     * Returns the trailing name component of a path.
57
     * This method is similar to the php function `basename()` except that it will
58
     * treat both \ and / as directory separators, independent of the operating system.
59
     * This method was mainly created to work on php namespaces. When working with real
60
     * file paths, php's `basename()` should work fine for you.
61
     * Note: this method is not aware of the actual filesystem, or path components such as "..".
62
     *
63
     * @param string $path A path string.
64
     * @param string $suffix If the name component ends in suffix this will also be cut off.
65
     * @return string the trailing name component of the given path.
66
     * @see https://www.php.net/manual/en/function.basename.php
67
     */
68 23
    public static function basename($path, $suffix = '')
69
    {
70 23
        $path = (string)$path;
71
72 23
        $len = mb_strlen($suffix);
73 23
        if ($len > 0 && mb_substr($path, -$len) === $suffix) {
74 1
            $path = mb_substr($path, 0, -$len);
75
        }
76
77 23
        $path = rtrim(str_replace('\\', '/', $path), '/');
78 23
        $pos = mb_strrpos($path, '/');
79 23
        if ($pos !== false) {
80 23
            return mb_substr($path, $pos + 1);
81
        }
82
83 1
        return $path;
84
    }
85
86
    /**
87
     * Returns parent directory's path.
88
     * This method is similar to `dirname()` except that it will treat
89
     * both \ and / as directory separators, independent of the operating system.
90
     *
91
     * @param string $path A path string.
92
     * @return string the parent directory's path.
93
     * @see https://www.php.net/manual/en/function.basename.php
94
     */
95 11
    public static function dirname($path)
96
    {
97 11
        $normalizedPath = rtrim(
98 11
            str_replace('\\', '/', (string)$path),
99 11
            '/'
100
        );
101 11
        $separatorPosition = mb_strrpos($normalizedPath, '/');
102
103 11
        if ($separatorPosition !== false) {
104 9
            return mb_substr($path, 0, $separatorPosition);
105
        }
106
107 2
        return '';
108
    }
109
110
    /**
111
     * Truncates a string to the number of characters specified.
112
     *
113
     * In order to truncate for an exact length, the $suffix char length must be counted towards the $length. For example
114
     * to have a string which is exactly 255 long with $suffix `...` of 3 chars, then `StringHelper::truncate($string, 252, '...')`
115
     * must be used to ensure you have 255 long string afterwards.
116
     *
117
     * @param string $string The string to truncate.
118
     * @param int $length How many characters from original string to include into truncated string.
119
     * @param string $suffix String to append to the end of truncated string.
120
     * @param string|null $encoding The charset to use, defaults to charset currently used by application.
121
     * @param bool $asHtml Whether to treat the string being truncated as HTML and preserve proper HTML tags.
122
     * This parameter is available since version 2.0.1.
123
     * @return string the truncated string.
124
     */
125 1
    public static function truncate($string, $length, $suffix = '...', $encoding = null, $asHtml = false)
126
    {
127 1
        $string = (string)$string;
128
129 1
        if ($encoding === null) {
130 1
            $encoding = Yii::$app ? Yii::$app->charset : 'UTF-8';
131
        }
132 1
        if ($asHtml) {
133 1
            return static::truncateHtml($string, $length, $suffix, $encoding);
134
        }
135
136 1
        if (mb_strlen($string, $encoding) > $length) {
137 1
            return rtrim(mb_substr($string, 0, $length, $encoding)) . $suffix;
138
        }
139
140 1
        return $string;
141
    }
142
143
    /**
144
     * Truncates a string to the number of words specified.
145
     *
146
     * @param string $string The string to truncate.
147
     * @param int $count How many words from original string to include into truncated string.
148
     * @param string $suffix String to append to the end of truncated string.
149
     * @param bool $asHtml Whether to treat the string being truncated as HTML and preserve proper HTML tags.
150
     * This parameter is available since version 2.0.1.
151
     * @return string the truncated string.
152
     */
153 1
    public static function truncateWords($string, $count, $suffix = '...', $asHtml = false)
154
    {
155 1
        if ($asHtml) {
156 1
            return static::truncateHtml($string, $count, $suffix);
157
        }
158
159 1
        $words = preg_split('/(\s+)/u', trim($string), 0, PREG_SPLIT_DELIM_CAPTURE);
160 1
        if (count($words) / 2 > $count) {
161 1
            return implode('', array_slice($words, 0, ($count * 2) - 1)) . $suffix;
162
        }
163
164 1
        return $string;
165
    }
166
167
    /**
168
     * Truncate a string while preserving the HTML.
169
     *
170
     * @param string $string The string to truncate
171
     * @param int $count The counter
172
     * @param string $suffix String to append to the end of the truncated string.
173
     * @param string|bool $encoding Encoding flag or charset.
174
     * @return string
175
     * @since 2.0.1
176
     */
177 2
    protected static function truncateHtml($string, $count, $suffix, $encoding = false)
178
    {
179 2
        $config = \HTMLPurifier_Config::create(null);
180 2
        if (Yii::$app !== null) {
181
            $config->set('Cache.SerializerPath', Yii::$app->getRuntimePath());
182
        }
183 2
        $lexer = \HTMLPurifier_Lexer::create($config);
184 2
        $tokens = $lexer->tokenizeHTML($string, $config, new \HTMLPurifier_Context());
185 2
        $openTokens = [];
186 2
        $totalCount = 0;
187 2
        $depth = 0;
188 2
        $truncated = [];
189 2
        foreach ($tokens as $token) {
190 2
            if ($token instanceof \HTMLPurifier_Token_Start) { //Tag begins
191 2
                $openTokens[$depth] = $token->name;
192 2
                $truncated[] = $token;
193 2
                ++$depth;
194 2
            } elseif ($token instanceof \HTMLPurifier_Token_Text && $totalCount <= $count) { //Text
195 2
                if (false === $encoding) {
196 1
                    preg_match('/^(\s*)/um', $token->data, $prefixSpace) ?: $prefixSpace = ['', ''];
197 1
                    $token->data = $prefixSpace[1] . self::truncateWords(ltrim($token->data), $count - $totalCount, '');
198 1
                    $currentCount = self::countWords($token->data);
199
                } else {
200 1
                    $token->data = self::truncate($token->data, $count - $totalCount, '', $encoding);
0 ignored issues
show
Bug introduced by
It seems like $encoding can also be of type true; however, parameter $encoding of yii\helpers\BaseStringHelper::truncate() does only seem to accept null|string, maybe add an additional type check? ( Ignorable by Annotation )

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

200
                    $token->data = self::truncate($token->data, $count - $totalCount, '', /** @scrutinizer ignore-type */ $encoding);
Loading history...
201 1
                    $currentCount = mb_strlen($token->data, $encoding);
0 ignored issues
show
Bug introduced by
It seems like $encoding can also be of type true; however, parameter $encoding of mb_strlen() does only seem to accept null|string, maybe add an additional type check? ( Ignorable by Annotation )

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

201
                    $currentCount = mb_strlen($token->data, /** @scrutinizer ignore-type */ $encoding);
Loading history...
202
                }
203 2
                $totalCount += $currentCount;
204 2
                $truncated[] = $token;
205 2
            } elseif ($token instanceof \HTMLPurifier_Token_End) { //Tag ends
206 2
                if ($token->name === $openTokens[$depth - 1]) {
207 2
                    --$depth;
208 2
                    unset($openTokens[$depth]);
209 2
                    $truncated[] = $token;
210
                }
211 2
            } elseif ($token instanceof \HTMLPurifier_Token_Empty) { //Self contained tags, i.e. <img/> etc.
212 2
                $truncated[] = $token;
213
            }
214 2
            if ($totalCount >= $count) {
215 2
                if (0 < count($openTokens)) {
216 2
                    krsort($openTokens);
217 2
                    foreach ($openTokens as $name) {
218 2
                        $truncated[] = new \HTMLPurifier_Token_End($name);
219
                    }
220
                }
221 2
                break;
222
            }
223
        }
224 2
        $context = new \HTMLPurifier_Context();
225 2
        $generator = new \HTMLPurifier_Generator($config, $context);
226 2
        return $generator->generateFromTokens($truncated) . ($totalCount >= $count ? $suffix : '');
227
    }
228
229
    /**
230
     * Check if given string starts with specified substring. Binary and multibyte safe.
231
     *
232
     * @param string $string Input string
233
     * @param string $with Part to search inside the $string
234
     * @param bool $caseSensitive Case sensitive search. Default is true. When case sensitive is enabled, `$with` must
235
     * exactly match the starting of the string in order to get a true value.
236
     * @return bool Returns true if first input starts with second input, false otherwise
237
     */
238 20
    public static function startsWith($string, $with, $caseSensitive = true)
239
    {
240 20
        $string = (string)$string;
241 20
        $with = (string)$with;
242
243 20
        if (!$bytes = static::byteLength($with)) {
244 3
            return true;
245
        }
246 17
        if ($caseSensitive) {
247 16
            return strncmp($string, $with, $bytes) === 0;
248
        }
249
250 15
        $encoding = Yii::$app ? Yii::$app->charset : 'UTF-8';
251 15
        $string = static::byteSubstr($string, 0, $bytes);
252
253 15
        return mb_strtolower($string, $encoding) === mb_strtolower($with, $encoding);
254
    }
255
256
    /**
257
     * Check if given string ends with specified substring. Binary and multibyte safe.
258
     *
259
     * @param string $string Input string to check
260
     * @param string $with Part to search inside of the `$string`.
261
     * @param bool $caseSensitive Case sensitive search. Default is true. When case sensitive is enabled, `$with` must
262
     * exactly match the ending of the string in order to get a true value.
263
     * @return bool Returns true if first input ends with second input, false otherwise
264
     */
265 30
    public static function endsWith($string, $with, $caseSensitive = true)
266
    {
267 30
        $string = (string)$string;
268 30
        $with = (string)$with;
269
270 30
        if (!$bytes = static::byteLength($with)) {
271 3
            return true;
272
        }
273 27
        if ($caseSensitive) {
274
            // Warning check, see https://php.net/substr-compare#refsect1-function.substr-compare-returnvalues
275 16
            if (static::byteLength($string) < $bytes) {
276 3
                return false;
277
            }
278
279 13
            return substr_compare($string, $with, -$bytes, $bytes) === 0;
280
        }
281
282 25
        $encoding = Yii::$app ? Yii::$app->charset : 'UTF-8';
283 25
        $string = static::byteSubstr($string, -$bytes);
284
285 25
        return mb_strtolower($string, $encoding) === mb_strtolower($with, $encoding);
286
    }
287
288
    /**
289
     * Explodes string into array, optionally trims values and skips empty ones.
290
     *
291
     * @param string $string String to be exploded.
292
     * @param string $delimiter Delimiter. Default is ','.
293
     * @param mixed $trim Whether to trim each element. Can be:
294
     *   - boolean - to trim normally;
295
     *   - string - custom characters to trim. Will be passed as a second argument to `trim()` function.
296
     *   - callable - will be called for each value instead of trim. Takes the only argument - value.
297
     * @param bool $skipEmpty Whether to skip empty strings between delimiters. Default is false.
298
     * @return array
299
     * @since 2.0.4
300
     */
301 1
    public static function explode($string, $delimiter = ',', $trim = true, $skipEmpty = false)
302
    {
303 1
        $result = explode($delimiter, $string);
304 1
        if ($trim !== false) {
305 1
            if ($trim === true) {
306 1
                $trim = 'trim';
307 1
            } elseif (!is_callable($trim)) {
308
                $trim = function ($v) use ($trim) {
309 1
                    return trim($v, $trim);
310 1
                };
311
            }
312 1
            $result = array_map($trim, $result);
313
        }
314 1
        if ($skipEmpty) {
315
            // Wrapped with array_values to make array keys sequential after empty values removing
316 1
            $result = array_values(array_filter($result, function ($value) {
317 1
                return $value !== '';
318 1
            }));
319
        }
320
321 1
        return $result;
322
    }
323
324
    /**
325
     * Counts words in a string.
326
     *
327
     * @param string $string the text to calculate
328
     * @return int
329
     * @since 2.0.8
330
     */
331 2
    public static function countWords($string)
332
    {
333 2
        return count(preg_split('/\s+/u', $string, 0, PREG_SPLIT_NO_EMPTY));
334
    }
335
336
    /**
337
     * Returns string representation of number value with replaced commas to dots, if decimal point
338
     * of current locale is comma.
339
     *
340
     * @param int|float|string $value the value to normalize.
341
     * @return string
342
     * @since 2.0.11
343
     */
344 34
    public static function normalizeNumber($value)
345
    {
346 34
        $value = (string) $value;
347
348 34
        $localeInfo = localeconv();
349 34
        $decimalSeparator = isset($localeInfo['decimal_point']) ? $localeInfo['decimal_point'] : null;
350
351 34
        if ($decimalSeparator !== null && $decimalSeparator !== '.') {
352 4
            $value = str_replace($decimalSeparator, '.', $value);
353
        }
354
355 34
        return $value;
356
    }
357
358
    /**
359
     * Encodes string into "Base 64 Encoding with URL and Filename Safe Alphabet" (RFC 4648).
360
     *
361
     * > Note: Base 64 padding `=` may be at the end of the returned string.
362
     * > `=` is not transparent to URL encoding.
363
     *
364
     * @param string $input the string to encode.
365
     * @return string encoded string.
366
     * @see https://tools.ietf.org/html/rfc4648#page-7
367
     * @since 2.0.12
368
     */
369 111
    public static function base64UrlEncode($input)
370
    {
371 111
        return strtr(base64_encode($input), '+/', '-_');
372
    }
373
374
    /**
375
     * Decodes "Base 64 Encoding with URL and Filename Safe Alphabet" (RFC 4648).
376
     *
377
     * @param string $input encoded string.
378
     * @return string decoded string.
379
     * @see https://tools.ietf.org/html/rfc4648#page-7
380
     * @since 2.0.12
381
     */
382 13
    public static function base64UrlDecode($input)
383
    {
384 13
        return base64_decode(strtr($input, '-_', '+/'));
385
    }
386
387
    /**
388
     * Safely casts a float to string independent of the current locale.
389
     * The decimal separator will always be `.`.
390
     *
391
     * @param float|int $number a floating point number or integer.
392
     * @return string the string representation of the number.
393
     * @since 2.0.13
394
     */
395 10
    public static function floatToString($number)
396
    {
397
        // . and , are the only decimal separators known in ICU data,
398
        // so its safe to call str_replace here
399 10
        return str_replace(',', '.', (string) $number);
400
    }
401
402
    /**
403
     * Checks if the passed string would match the given shell wildcard pattern.
404
     * This function emulates [[fnmatch()]], which may be unavailable at certain environment, using PCRE.
405
     *
406
     * @param string $pattern the shell wildcard pattern.
407
     * @param string $string the tested string.
408
     * @param array $options options for matching. Valid options are:
409
     *
410
     * - caseSensitive: bool, whether pattern should be case sensitive. Defaults to `true`.
411
     * - escape: bool, whether backslash escaping is enabled. Defaults to `true`.
412
     * - filePath: bool, whether slashes in string only matches slashes in the given pattern. Defaults to `false`.
413
     *
414
     * @return bool whether the string matches pattern or not.
415
     * @since 2.0.14
416
     */
417 243
    public static function matchWildcard($pattern, $string, $options = [])
418
    {
419 243
        if ($pattern === '*' && empty($options['filePath'])) {
420 5
            return true;
421
        }
422
423
        $replacements = [
424 239
            '\\\\\\\\' => '\\\\',
425
            '\\\\\\*' => '[*]',
426
            '\\\\\\?' => '[?]',
427
            '\*' => '.*',
428
            '\?' => '.',
429
            '\[\!' => '[^',
430
            '\[' => '[',
431
            '\]' => ']',
432
            '\-' => '-',
433
        ];
434
435 239
        if (isset($options['escape']) && !$options['escape']) {
436 9
            unset($replacements['\\\\\\\\']);
437 9
            unset($replacements['\\\\\\*']);
438 9
            unset($replacements['\\\\\\?']);
439
        }
440
441 239
        if (!empty($options['filePath'])) {
442 12
            $replacements['\*'] = '[^/\\\\]*';
443 12
            $replacements['\?'] = '[^/\\\\]';
444
        }
445
446 239
        $pattern = strtr(preg_quote($pattern, '#'), $replacements);
447 239
        $pattern = '#^' . $pattern . '$#us';
448
449 239
        if (isset($options['caseSensitive']) && !$options['caseSensitive']) {
450 2
            $pattern .= 'i';
451
        }
452
453 239
        return preg_match($pattern, (string)$string) === 1;
454
    }
455
456
    /**
457
     * This method provides a unicode-safe implementation of built-in PHP function `ucfirst()`.
458
     *
459
     * @param string $string the string to be proceeded
460
     * @param string $encoding Optional, defaults to "UTF-8"
461
     * @return string
462
     * @see https://www.php.net/manual/en/function.ucfirst.php
463
     * @since 2.0.16
464
     */
465 227
    public static function mb_ucfirst($string, $encoding = 'UTF-8')
466
    {
467 227
        $firstChar = mb_substr((string)$string, 0, 1, $encoding);
468 227
        $rest = mb_substr((string)$string, 1, null, $encoding);
469
470 227
        return mb_strtoupper($firstChar, $encoding) . $rest;
471
    }
472
473
    /**
474
     * This method provides a unicode-safe implementation of built-in PHP function `ucwords()`.
475
     *
476
     * @param string $string the string to be proceeded
477
     * @param string $encoding Optional, defaults to "UTF-8"
478
     * @return string
479
     * @see https://www.php.net/manual/en/function.ucwords
480
     * @since 2.0.16
481
     */
482 224
    public static function mb_ucwords($string, $encoding = 'UTF-8')
483
    {
484 224
        $string = (string) $string;
485 224
        if (empty($string)) {
486 3
            return $string;
487
        }
488
489 221
        $parts = preg_split('/(\s+\W+\s+|^\W+\s+|\s+)/u', $string, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
490 221
        $ucfirstEven = trim(mb_substr($parts[0], -1, 1, $encoding)) === '';
491 221
        foreach ($parts as $key => $value) {
492 221
            $isEven = (bool)($key % 2);
493 221
            if ($ucfirstEven === $isEven) {
494 221
                $parts[$key] = static::mb_ucfirst($value, $encoding);
495
            }
496
        }
497
498 221
        return implode('', $parts);
499
    }
500
501
    /**
502
     * Masks a portion of a string with a repeated character.
503
     * This method is multibyte-safe.
504
     *
505
     * @param string $string The input string.
506
     * @param int $start The starting position from where to begin masking.
507
     *                   This can be a positive or negative integer.
508
     *                   Positive values count from the beginning,
509
     *                   negative values count from the end of the string.
510
     * @param int $length The length of the section to be masked.
511
     *                    The masking will start from the $start position
512
     *                    and continue for $length characters.
513
     * @param string $mask The character to use for masking. The default is '*'.
514
     * @return string The masked string.
515
     */
516 1
    public static function mask($string, $start, $length, $mask = '*') {
517 1
        $strLength = mb_strlen($string, 'UTF-8');
518
519
        // Return original string if start position is out of bounds
520 1
        if ($start >= $strLength || $start < -$strLength) {
521 1
            return $string;
522
        }
523
524 1
        $masked = mb_substr($string, 0, $start, 'UTF-8');
525 1
        $masked .= str_repeat($mask, abs($length));
0 ignored issues
show
Bug introduced by
It seems like abs($length) can also be of type double; however, parameter $times of str_repeat() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

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

525
        $masked .= str_repeat($mask, /** @scrutinizer ignore-type */ abs($length));
Loading history...
526 1
        $masked .= mb_substr($string, $start + abs($length), null, 'UTF-8');
0 ignored issues
show
Bug introduced by
$start + abs($length) of type double is incompatible with the type integer expected by parameter $start of mb_substr(). ( Ignorable by Annotation )

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

526
        $masked .= mb_substr($string, /** @scrutinizer ignore-type */ $start + abs($length), null, 'UTF-8');
Loading history...
527
528 1
        return $masked;
529
    }
530
}
531