Completed
Push — 2.1 ( 28b26f...4d9204 )
by Alexander
10:53
created

BaseStringHelper::normalizeNumber()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 7
cts 7
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 7
nc 4
nop 1
crap 4
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 358
    public static function byteLength($string)
30
    {
31 358
        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 http://www.php.net/manual/en/function.substr.php
43
     */
44 80
    public static function byteSubstr($string, $start, $length = null)
45
    {
46 80
        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 http://www.php.net/manual/en/function.basename.php
61
     */
62 15
    public static function basename($path, $suffix = '')
63
    {
64 15
        if (($len = mb_strlen($suffix)) > 0 && mb_substr($path, -$len) === $suffix) {
65 1
            $path = mb_substr($path, 0, -$len);
66
        }
67 15
        $path = rtrim(str_replace('\\', '/', $path), '/\\');
68 15
        if (($pos = mb_strrpos($path, '/')) !== false) {
69 15
            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 http://www.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 ($asHtml) {
108 1
            return static::truncateHtml($string, $length, $suffix, $encoding ?: Yii::$app->charset);
109
        }
110
111 1
        if (mb_strlen($string, $encoding ?: Yii::$app->charset) > $length) {
112 1
            return rtrim(mb_substr($string, 0, $length, $encoding ?: Yii::$app->charset)) . $suffix;
113
        }
114
115 1
        return $string;
116
    }
117
118
    /**
119
     * Truncates a string to the number of words specified.
120
     *
121
     * @param string $string The string to truncate.
122
     * @param int $count How many words from original string to include into truncated string.
123
     * @param string $suffix String to append to the end of truncated string.
124
     * @param bool $asHtml Whether to treat the string being truncated as HTML and preserve proper HTML tags.
125
     * This parameter is available since version 2.0.1.
126
     * @return string the truncated string.
127
     */
128 1
    public static function truncateWords($string, $count, $suffix = '...', $asHtml = false)
129
    {
130 1
        if ($asHtml) {
131 1
            return static::truncateHtml($string, $count, $suffix);
132
        }
133
134 1
        $words = preg_split('/(\s+)/u', trim($string), null, PREG_SPLIT_DELIM_CAPTURE);
135 1
        if (count($words) / 2 > $count) {
136 1
            return implode('', array_slice($words, 0, ($count * 2) - 1)) . $suffix;
137
        }
138
139 1
        return $string;
140
    }
141
142
    /**
143
     * Truncate a string while preserving the HTML.
144
     *
145
     * @param string $string The string to truncate
146
     * @param int $count
147
     * @param string $suffix String to append to the end of the truncated string.
148
     * @param string|bool $encoding
149
     * @return string
150
     * @since 2.0.1
151
     */
152 2
    protected static function truncateHtml($string, $count, $suffix, $encoding = false)
153
    {
154 2
        $config = \HTMLPurifier_Config::create(null);
155 2
        $config->set('Cache.SerializerPath', \Yii::$app->getRuntimePath());
156 2
        $lexer = \HTMLPurifier_Lexer::create($config);
157 2
        $tokens = $lexer->tokenizeHTML($string, $config, new \HTMLPurifier_Context());
158 2
        $openTokens = [];
159 2
        $totalCount = 0;
160 2
        $depth = 0;
161 2
        $truncated = [];
162 2
        foreach ($tokens as $token) {
0 ignored issues
show
Bug introduced by
The expression $tokens of type array<integer,object<HTMLPurifier_Token>>|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
163 2
            if ($token instanceof \HTMLPurifier_Token_Start) { //Tag begins
164 2
                $openTokens[$depth] = $token->name;
165 2
                $truncated[] = $token;
166 2
                ++$depth;
167 2
            } elseif ($token instanceof \HTMLPurifier_Token_Text && $totalCount <= $count) { //Text
168 2
                if (false === $encoding) {
169 1
                    preg_match('/^(\s*)/um', $token->data, $prefixSpace) ?: $prefixSpace = ['', ''];
170 1
                    $token->data = $prefixSpace[1] . self::truncateWords(ltrim($token->data), $count - $totalCount, '');
171 1
                    $currentCount = self::countWords($token->data);
172
                } else {
173 1
                    $token->data = self::truncate($token->data, $count - $totalCount, '', $encoding);
0 ignored issues
show
Bug introduced by
It seems like $encoding defined by parameter $encoding on line 152 can also be of type boolean; however, yii\helpers\BaseStringHelper::truncate() does only seem to accept string|null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
174 1
                    $currentCount = mb_strlen($token->data, $encoding);
175
                }
176 2
                $totalCount += $currentCount;
177 2
                $truncated[] = $token;
178 2
            } elseif ($token instanceof \HTMLPurifier_Token_End) { //Tag ends
179 2
                if ($token->name === $openTokens[$depth - 1]) {
180 2
                    --$depth;
181 2
                    unset($openTokens[$depth]);
182 2
                    $truncated[] = $token;
183
                }
184 2
            } elseif ($token instanceof \HTMLPurifier_Token_Empty) { //Self contained tags, i.e. <img/> etc.
185 2
                $truncated[] = $token;
186
            }
187 2
            if ($totalCount >= $count) {
188 2
                if (0 < count($openTokens)) {
189 2
                    krsort($openTokens);
190 2
                    foreach ($openTokens as $name) {
191 2
                        $truncated[] = new \HTMLPurifier_Token_End($name);
192
                    }
193
                }
194 2
                break;
195
            }
196
        }
197 2
        $context = new \HTMLPurifier_Context();
198 2
        $generator = new \HTMLPurifier_Generator($config, $context);
199 2
        return $generator->generateFromTokens($truncated) . ($totalCount >= $count ? $suffix : '');
200
    }
201
202
    /**
203
     * Check if given string starts with specified substring.
204
     * Binary and multibyte safe.
205
     *
206
     * @param string $string Input string
207
     * @param string $with Part to search inside the $string
208
     * @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.
209
     * @return bool Returns true if first input starts with second input, false otherwise
210
     */
211 19
    public static function startsWith($string, $with, $caseSensitive = true)
212
    {
213 19
        if (!$bytes = static::byteLength($with)) {
214 3
            return true;
215
        }
216 16
        if ($caseSensitive) {
217 15
            return strncmp($string, $with, $bytes) === 0;
218
        }
219
220 15
        return mb_strtolower(mb_substr($string, 0, $bytes, '8bit'), Yii::$app->charset) === mb_strtolower($with, Yii::$app->charset);
221
    }
222
223
    /**
224
     * Check if given string ends with specified substring.
225
     * Binary and multibyte safe.
226
     *
227
     * @param string $string Input string to check
228
     * @param string $with Part to search inside of the $string.
229
     * @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.
230
     * @return bool Returns true if first input ends with second input, false otherwise
231
     */
232 19
    public static function endsWith($string, $with, $caseSensitive = true)
233
    {
234 19
        if (!$bytes = static::byteLength($with)) {
235 3
            return true;
236
        }
237 16
        if ($caseSensitive) {
238
            // Warning check, see http://php.net/manual/en/function.substr-compare.php#refsect1-function.substr-compare-returnvalues
239 15
            if (static::byteLength($string) < $bytes) {
240 3
                return false;
241
            }
242 12
            return substr_compare($string, $with, -$bytes, $bytes) === 0;
243
        }
244
245 15
        return mb_strtolower(mb_substr($string, -$bytes, mb_strlen($string, '8bit'), '8bit'), Yii::$app->charset) === mb_strtolower($with, Yii::$app->charset);
246
    }
247
248
    /**
249
     * Explodes string into array, optionally trims values and skips empty ones
250
     *
251
     * @param string $string String to be exploded.
252
     * @param string $delimiter Delimiter. Default is ','.
253
     * @param mixed $trim Whether to trim each element. Can be:
254
     *   - boolean - to trim normally;
255
     *   - string - custom characters to trim. Will be passed as a second argument to `trim()` function.
256
     *   - callable - will be called for each value instead of trim. Takes the only argument - value.
257
     * @param bool $skipEmpty Whether to skip empty strings between delimiters. Default is false.
258
     * @return array
259
     * @since 2.0.4
260
     */
261 1
    public static function explode($string, $delimiter = ',', $trim = true, $skipEmpty = false)
262
    {
263 1
        $result = explode($delimiter, $string);
264 1
        if ($trim) {
265 1
            if ($trim === true) {
266 1
                $trim = 'trim';
267 1
            } elseif (!is_callable($trim)) {
268
                $trim = function ($v) use ($trim) {
269
                    return trim($v, $trim);
270
                };
271
            }
272 1
            $result = array_map($trim, $result);
273
        }
274 1
        if ($skipEmpty) {
275
            // Wrapped with array_values to make array keys sequential after empty values removing
276 1
            $result = array_values(array_filter($result, function ($value) {
277 1
                return $value !== '';
278 1
            }));
279
        }
280 1
        return $result;
281
    }
282
283
    /**
284
     * Counts words in a string
285
     * @since 2.0.8
286
     *
287
     * @param string $string
288
     * @return int
289
     */
290 2
    public static function countWords($string)
291
    {
292 2
        return count(preg_split('/\s+/u', $string, null, PREG_SPLIT_NO_EMPTY));
293
    }
294
295
    /**
296
     * Returns string represenation of number value with replaced commas to dots, if decimal point
297
     * of current locale is comma
298
     * @param int|float|string $value
299
     * @return string
300
     * @since 2.0.11
301
     */
302 14
    public static function normalizeNumber($value)
303
    {
304 14
        $value = "$value";
305
306 14
        $localeInfo = localeconv();
307 14
        $decimalSeparator = isset($localeInfo['decimal_point']) ? $localeInfo['decimal_point'] : null;
308
309 14
        if ($decimalSeparator !== null && $decimalSeparator !== '.') {
310 3
            $value = str_replace($decimalSeparator, '.', $value);
311
        }
312
313 14
        return $value;
314
    }
315
316
    /**
317
     * Encodes string into "Base 64 Encoding with URL and Filename Safe Alphabet" (RFC 4648)
318
     *
319
     * > Note: Base 64 padding `=` may be at the end of the returned string.
320
     * > `=` is not transparent to URL encoding.
321
     *
322
     * @see https://tools.ietf.org/html/rfc4648#page-7
323
     * @param string $input the string to encode.
324
     * @return string encoded string.
325
     * @since 2.0.12
326
     */
327 57
    public static function base64UrlEncode($input)
328
    {
329 57
        return strtr(base64_encode($input), '+/', '-_');
330
    }
331
332
    /**
333
     * Decodes "Base 64 Encoding with URL and Filename Safe Alphabet" (RFC 4648)
334
     *
335
     * @see https://tools.ietf.org/html/rfc4648#page-7
336
     * @param string $input encoded string.
337
     * @return string decoded string.
338
     * @since 2.0.12
339
     */
340 12
    public static function base64UrlDecode($input)
341
    {
342 12
        return base64_decode(strtr($input, '-_', '+/'));
343
    }
344
}
345