Passed
Push — master ( 9dbdd9...d5a428 )
by Alexander
04:15
created

framework/helpers/BaseStringHelper.php (2 issues)

1
<?php
2
/**
3
 * @link http://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license http://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
     * @param string $string the string being measured for length
27
     * @return int the number of bytes in the given string.
28
     */
29 437
    public static function byteLength($string)
30
    {
31 437
        return mb_strlen($string, '8bit');
32
    }
33
34
    /**
35
     * Returns the portion of string specified by the start and length parameters.
36
     * This method ensures the string is treated as a byte array by using `mb_substr()`.
37
     * @param string $string the input string. Must be one character or longer.
38
     * @param int $start the starting position
39
     * @param int $length the desired portion length. If not specified or `null`, there will be
40
     * no limit on length i.e. the output will be until the end of the string.
41
     * @return string the extracted part of string, or FALSE on failure or an empty string.
42
     * @see https://secure.php.net/manual/en/function.substr.php
43
     */
44 113
    public static function byteSubstr($string, $start, $length = null)
45
    {
46 113
        return mb_substr($string, $start, $length === null ? mb_strlen($string, '8bit') : $length, '8bit');
47
    }
48
49
    /**
50
     * Returns the trailing name component of a path.
51
     * This method is similar to the php function `basename()` except that it will
52
     * treat both \ and / as directory separators, independent of the operating system.
53
     * This method was mainly created to work on php namespaces. When working with real
54
     * file paths, php's `basename()` should work fine for you.
55
     * Note: this method is not aware of the actual filesystem, or path components such as "..".
56
     *
57
     * @param string $path A path string.
58
     * @param string $suffix If the name component ends in suffix this will also be cut off.
59
     * @return string the trailing name component of the given path.
60
     * @see https://secure.php.net/manual/en/function.basename.php
61
     */
62 27
    public static function basename($path, $suffix = '')
63
    {
64 27
        if (($len = mb_strlen($suffix)) > 0 && mb_substr($path, -$len) === $suffix) {
65 1
            $path = mb_substr($path, 0, -$len);
66
        }
67 27
        $path = rtrim(str_replace('\\', '/', $path), '/\\');
68 27
        if (($pos = mb_strrpos($path, '/')) !== false) {
69 27
            return mb_substr($path, $pos + 1);
70
        }
71
72 1
        return $path;
73
    }
74
75
    /**
76
     * Returns parent directory's path.
77
     * This method is similar to `dirname()` except that it will treat
78
     * both \ and / as directory separators, independent of the operating system.
79
     *
80
     * @param string $path A path string.
81
     * @return string the parent directory's path.
82
     * @see https://secure.php.net/manual/en/function.basename.php
83
     */
84 5
    public static function dirname($path)
85
    {
86 5
        $pos = mb_strrpos(str_replace('\\', '/', $path), '/');
87 5
        if ($pos !== false) {
88 5
            return mb_substr($path, 0, $pos);
89
        }
90
91
        return '';
92
    }
93
94
    /**
95
     * Truncates a string to the number of characters specified.
96
     *
97
     * @param string $string The string to truncate.
98
     * @param int $length How many characters from original string to include into truncated string.
99
     * @param string $suffix String to append to the end of truncated string.
100
     * @param string $encoding The charset to use, defaults to charset currently used by application.
101
     * @param bool $asHtml Whether to treat the string being truncated as HTML and preserve proper HTML tags.
102
     * This parameter is available since version 2.0.1.
103
     * @return string the truncated string.
104
     */
105 1
    public static function truncate($string, $length, $suffix = '...', $encoding = null, $asHtml = false)
106
    {
107 1
        if ($encoding === null) {
108 1
            $encoding = Yii::$app ? Yii::$app->charset : 'UTF-8';
109
        }
110 1
        if ($asHtml) {
111 1
            return static::truncateHtml($string, $length, $suffix, $encoding);
112
        }
113
114 1
        if (mb_strlen($string, $encoding) > $length) {
115 1
            return rtrim(mb_substr($string, 0, $length, $encoding)) . $suffix;
116
        }
117
118 1
        return $string;
119
    }
120
121
    /**
122
     * Truncates a string to the number of words specified.
123
     *
124
     * @param string $string The string to truncate.
125
     * @param int $count How many words from original string to include into truncated string.
126
     * @param string $suffix String to append to the end of truncated string.
127
     * @param bool $asHtml Whether to treat the string being truncated as HTML and preserve proper HTML tags.
128
     * This parameter is available since version 2.0.1.
129
     * @return string the truncated string.
130
     */
131 1
    public static function truncateWords($string, $count, $suffix = '...', $asHtml = false)
132
    {
133 1
        if ($asHtml) {
134 1
            return static::truncateHtml($string, $count, $suffix);
135
        }
136
137 1
        $words = preg_split('/(\s+)/u', trim($string), null, PREG_SPLIT_DELIM_CAPTURE);
138 1
        if (count($words) / 2 > $count) {
139 1
            return implode('', array_slice($words, 0, ($count * 2) - 1)) . $suffix;
140
        }
141
142 1
        return $string;
143
    }
144
145
    /**
146
     * Truncate a string while preserving the HTML.
147
     *
148
     * @param string $string The string to truncate
149
     * @param int $count
150
     * @param string $suffix String to append to the end of the truncated string.
151
     * @param string|bool $encoding
152
     * @return string
153
     * @since 2.0.1
154
     */
155 2
    protected static function truncateHtml($string, $count, $suffix, $encoding = false)
156
    {
157 2
        $config = \HTMLPurifier_Config::create(null);
158 2
        if (Yii::$app !== null) {
159
            $config->set('Cache.SerializerPath', Yii::$app->getRuntimePath());
160
        }
161 2
        $lexer = \HTMLPurifier_Lexer::create($config);
162 2
        $tokens = $lexer->tokenizeHTML($string, $config, new \HTMLPurifier_Context());
163 2
        $openTokens = [];
164 2
        $totalCount = 0;
165 2
        $depth = 0;
166 2
        $truncated = [];
167 2
        foreach ($tokens as $token) {
168 2
            if ($token instanceof \HTMLPurifier_Token_Start) { //Tag begins
169 2
                $openTokens[$depth] = $token->name;
170 2
                $truncated[] = $token;
171 2
                ++$depth;
172 2
            } elseif ($token instanceof \HTMLPurifier_Token_Text && $totalCount <= $count) { //Text
173 2
                if (false === $encoding) {
174 1
                    preg_match('/^(\s*)/um', $token->data, $prefixSpace) ?: $prefixSpace = ['', ''];
175 1
                    $token->data = $prefixSpace[1] . self::truncateWords(ltrim($token->data), $count - $totalCount, '');
176 1
                    $currentCount = self::countWords($token->data);
177
                } else {
178 1
                    $token->data = self::truncate($token->data, $count - $totalCount, '', $encoding);
0 ignored issues
show
It seems like $encoding can also be of type true; however, parameter $encoding of yii\helpers\BaseStringHelper::truncate() does only seem to accept 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

178
                    $token->data = self::truncate($token->data, $count - $totalCount, '', /** @scrutinizer ignore-type */ $encoding);
Loading history...
179 1
                    $currentCount = mb_strlen($token->data, $encoding);
0 ignored issues
show
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

179
                    $currentCount = mb_strlen($token->data, /** @scrutinizer ignore-type */ $encoding);
Loading history...
180
                }
181 2
                $totalCount += $currentCount;
182 2
                $truncated[] = $token;
183 2
            } elseif ($token instanceof \HTMLPurifier_Token_End) { //Tag ends
184 2
                if ($token->name === $openTokens[$depth - 1]) {
185 2
                    --$depth;
186 2
                    unset($openTokens[$depth]);
187 2
                    $truncated[] = $token;
188
                }
189 2
            } elseif ($token instanceof \HTMLPurifier_Token_Empty) { //Self contained tags, i.e. <img/> etc.
190 2
                $truncated[] = $token;
191
            }
192 2
            if ($totalCount >= $count) {
193 2
                if (0 < count($openTokens)) {
194 2
                    krsort($openTokens);
195 2
                    foreach ($openTokens as $name) {
196 2
                        $truncated[] = new \HTMLPurifier_Token_End($name);
197
                    }
198
                }
199 2
                break;
200
            }
201
        }
202 2
        $context = new \HTMLPurifier_Context();
203 2
        $generator = new \HTMLPurifier_Generator($config, $context);
204 2
        return $generator->generateFromTokens($truncated) . ($totalCount >= $count ? $suffix : '');
205
    }
206
207
    /**
208
     * Check if given string starts with specified substring.
209
     * Binary and multibyte safe.
210
     *
211
     * @param string $string Input string
212
     * @param string $with Part to search inside the $string
213
     * @param bool $caseSensitive Case sensitive search. Default is true. When case sensitive is enabled, $with must exactly match the starting of the string in order to get a true value.
214
     * @return bool Returns true if first input starts with second input, false otherwise
215
     */
216 20
    public static function startsWith($string, $with, $caseSensitive = true)
217
    {
218 20
        if (!$bytes = static::byteLength($with)) {
219 3
            return true;
220
        }
221 17
        if ($caseSensitive) {
222 16
            return strncmp($string, $with, $bytes) === 0;
223
224
        }
225 15
        $encoding = Yii::$app ? Yii::$app->charset : 'UTF-8';
226 15
        return mb_strtolower(mb_substr($string, 0, $bytes, '8bit'), $encoding) === mb_strtolower($with, $encoding);
227
    }
228
229
    /**
230
     * Check if given string ends with specified substring.
231
     * Binary and multibyte safe.
232
     *
233
     * @param string $string Input string to check
234
     * @param string $with Part to search inside of the $string.
235
     * @param bool $caseSensitive Case sensitive search. Default is true. When case sensitive is enabled, $with must exactly match the ending of the string in order to get a true value.
236
     * @return bool Returns true if first input ends with second input, false otherwise
237
     */
238 30
    public static function endsWith($string, $with, $caseSensitive = true)
239
    {
240 30
        if (!$bytes = static::byteLength($with)) {
241 3
            return true;
242
        }
243 27
        if ($caseSensitive) {
244
            // Warning check, see https://secure.php.net/manual/en/function.substr-compare.php#refsect1-function.substr-compare-returnvalues
245 16
            if (static::byteLength($string) < $bytes) {
246 3
                return false;
247
            }
248
249 13
            return substr_compare($string, $with, -$bytes, $bytes) === 0;
250
        }
251
252 25
        $encoding = Yii::$app ? Yii::$app->charset : 'UTF-8';
253 25
        return mb_strtolower(mb_substr($string, -$bytes, mb_strlen($string, '8bit'), '8bit'), $encoding) === mb_strtolower($with, $encoding);
254
    }
255
256
    /**
257
     * Explodes string into array, optionally trims values and skips empty ones.
258
     *
259
     * @param string $string String to be exploded.
260
     * @param string $delimiter Delimiter. Default is ','.
261
     * @param mixed $trim Whether to trim each element. Can be:
262
     *   - boolean - to trim normally;
263
     *   - string - custom characters to trim. Will be passed as a second argument to `trim()` function.
264
     *   - callable - will be called for each value instead of trim. Takes the only argument - value.
265
     * @param bool $skipEmpty Whether to skip empty strings between delimiters. Default is false.
266
     * @return array
267
     * @since 2.0.4
268
     */
269 1
    public static function explode($string, $delimiter = ',', $trim = true, $skipEmpty = false)
270
    {
271 1
        $result = explode($delimiter, $string);
272 1
        if ($trim !== false) {
273 1
            if ($trim === true) {
274 1
                $trim = 'trim';
275 1
            } elseif (!is_callable($trim)) {
276
                $trim = function ($v) use ($trim) {
277 1
                    return trim($v, $trim);
278 1
                };
279
            }
280 1
            $result = array_map($trim, $result);
281
        }
282 1
        if ($skipEmpty) {
283
            // Wrapped with array_values to make array keys sequential after empty values removing
284
            $result = array_values(array_filter($result, function ($value) {
285 1
                return $value !== '';
286 1
            }));
287
        }
288
289 1
        return $result;
290
    }
291
292
    /**
293
     * Counts words in a string.
294
     * @since 2.0.8
295
     *
296
     * @param string $string
297
     * @return int
298
     */
299 2
    public static function countWords($string)
300
    {
301 2
        return count(preg_split('/\s+/u', $string, null, PREG_SPLIT_NO_EMPTY));
302
    }
303
304
    /**
305
     * Returns string representation of number value with replaced commas to dots, if decimal point
306
     * of current locale is comma.
307
     * @param int|float|string $value
308
     * @return string
309
     * @since 2.0.11
310
     */
311 32
    public static function normalizeNumber($value)
312
    {
313 32
        $value = (string)$value;
314
315 32
        $localeInfo = localeconv();
316 32
        $decimalSeparator = isset($localeInfo['decimal_point']) ? $localeInfo['decimal_point'] : null;
317
318 32
        if ($decimalSeparator !== null && $decimalSeparator !== '.') {
319 4
            $value = str_replace($decimalSeparator, '.', $value);
320
        }
321
322 32
        return $value;
323
    }
324
325
    /**
326
     * Encodes string into "Base 64 Encoding with URL and Filename Safe Alphabet" (RFC 4648).
327
     *
328
     * > Note: Base 64 padding `=` may be at the end of the returned string.
329
     * > `=` is not transparent to URL encoding.
330
     *
331
     * @see https://tools.ietf.org/html/rfc4648#page-7
332
     * @param string $input the string to encode.
333
     * @return string encoded string.
334
     * @since 2.0.12
335
     */
336 104
    public static function base64UrlEncode($input)
337
    {
338 104
        return strtr(base64_encode($input), '+/', '-_');
339
    }
340
341
    /**
342
     * Decodes "Base 64 Encoding with URL and Filename Safe Alphabet" (RFC 4648).
343
     *
344
     * @see https://tools.ietf.org/html/rfc4648#page-7
345
     * @param string $input encoded string.
346
     * @return string decoded string.
347
     * @since 2.0.12
348
     */
349 13
    public static function base64UrlDecode($input)
350
    {
351 13
        return base64_decode(strtr($input, '-_', '+/'));
352
    }
353
354
    /**
355
     * Safely casts a float to string independent of the current locale.
356
     *
357
     * The decimal separator will always be `.`.
358
     * @param float|int $number a floating point number or integer.
359
     * @return string the string representation of the number.
360
     * @since 2.0.13
361
     */
362 18
    public static function floatToString($number)
363
    {
364
        // . and , are the only decimal separators known in ICU data,
365
        // so its safe to call str_replace here
366 18
        return str_replace(',', '.', (string) $number);
367
    }
368
369
    /**
370
     * Checks if the passed string would match the given shell wildcard pattern.
371
     * This function emulates [[fnmatch()]], which may be unavailable at certain environment, using PCRE.
372
     * @param string $pattern the shell wildcard pattern.
373
     * @param string $string the tested string.
374
     * @param array $options options for matching. Valid options are:
375
     *
376
     * - caseSensitive: bool, whether pattern should be case sensitive. Defaults to `true`.
377
     * - escape: bool, whether backslash escaping is enabled. Defaults to `true`.
378
     * - filePath: bool, whether slashes in string only matches slashes in the given pattern. Defaults to `false`.
379
     *
380
     * @return bool whether the string matches pattern or not.
381
     * @since 2.0.14
382
     */
383 238
    public static function matchWildcard($pattern, $string, $options = [])
384
    {
385 238
        if ($pattern === '*' && empty($options['filePath'])) {
386 5
            return true;
387
        }
388
389
        $replacements = [
390 234
            '\\\\\\\\' => '\\\\',
391
            '\\\\\\*' => '[*]',
392
            '\\\\\\?' => '[?]',
393
            '\*' => '.*',
394
            '\?' => '.',
395
            '\[\!' => '[^',
396
            '\[' => '[',
397
            '\]' => ']',
398
            '\-' => '-',
399
        ];
400
401 234
        if (isset($options['escape']) && !$options['escape']) {
402 9
            unset($replacements['\\\\\\\\']);
403 9
            unset($replacements['\\\\\\*']);
404 9
            unset($replacements['\\\\\\?']);
405
        }
406
407 234
        if (!empty($options['filePath'])) {
408 12
            $replacements['\*'] = '[^/\\\\]*';
409 12
            $replacements['\?'] = '[^/\\\\]';
410
        }
411
412 234
        $pattern = strtr(preg_quote($pattern, '#'), $replacements);
413 234
        $pattern = '#^' . $pattern . '$#us';
414
415 234
        if (isset($options['caseSensitive']) && !$options['caseSensitive']) {
416 2
            $pattern .= 'i';
417
        }
418
419 234
        return preg_match($pattern, $string) === 1;
420
    }
421
422
    /**
423
     * This method provides a unicode-safe implementation of built-in PHP function `ucfirst()`.
424
     *
425
     * @param string $string the string to be proceeded
426
     * @param string $encoding Optional, defaults to "UTF-8"
427
     * @return string
428
     * @see https://secure.php.net/manual/en/function.ucfirst.php
429
     * @since 2.0.16
430
     */
431 239
    public static function mb_ucfirst($string, $encoding = 'UTF-8')
432
    {
433 239
        $firstChar = mb_substr($string, 0, 1, $encoding);
434 239
        $rest = mb_substr($string, 1, null, $encoding);
435
436 239
        return mb_strtoupper($firstChar, $encoding) . $rest;
437
    }
438
439
    /**
440
     * This method provides a unicode-safe implementation of built-in PHP function `ucwords()`.
441
     *
442
     * @param string $string the string to be proceeded
443
     * @param string $encoding Optional, defaults to "UTF-8"
444
     * @return string
445
     * @see https://secure.php.net/manual/en/function.ucwords.php
446
     * @since 2.0.16
447
     */
448 235
    public static function mb_ucwords($string, $encoding = 'UTF-8')
449
    {
450 235
        $words = preg_split("/\s/u", $string, -1, PREG_SPLIT_NO_EMPTY);
451
452 235
        $titelized = array_map(function ($word) use ($encoding) {
453 233
            return static::mb_ucfirst($word, $encoding);
454 235
        }, $words);
455
456 235
        return implode(' ', $titelized);
457
    }
458
}
459