Passed
Pull Request — main (#106)
by Andrey
12:52
created

Str::match()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 5
ccs 1
cts 1
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Helldar\Support\Helpers;
4
5
use Helldar\Support\Facades\Helpers\Call as CallHelper;
6
use Helldar\Support\Facades\Tools\Replace;
7
use Illuminate\Contracts\Support\DeferringDisplayableValue;
0 ignored issues
show
Bug introduced by
The type Illuminate\Contracts\Sup...ferringDisplayableValue was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Illuminate\Contracts\Support\Htmlable;
0 ignored issues
show
Bug introduced by
The type Illuminate\Contracts\Support\Htmlable was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use voku\helper\ASCII;
10
11
final class Str
12
{
13
    /**
14
     * The cache of snake-cased words.
15
     *
16
     * @var array
17
     */
18
    protected static $snakeCache = [];
19
20
    /**
21
     * The cache of camel-cased words.
22
     *
23
     * @var array
24
     */
25
    protected static $camelCache = [];
26
27
    /**
28
     * The cache of studly-cased words.
29
     *
30
     * @var array
31
     */
32
    protected static $studlyCache = [];
33
34
    protected $escaping_methods = [
35
        DeferringDisplayableValue::class => 'resolveDisplayableValue',
36
        Htmlable::class                  => 'toHtml',
37
    ];
38
39
    /**
40
     * Get a new stringable object from the given string.
41
     *
42
     * @see https://github.com/illuminate/support/blob/master/Str.php
43
     *
44
     * @param  string|null  $value
45
     *
46
     * @return \Helldar\Support\Helpers\Stringable
47
     */
48 2
    public function of(?string $value): Stringable
49
    {
50 2
        return new Stringable($value);
51
    }
52
53
    /**
54
     * Escape HTML special characters in a string.
55
     *
56
     * @param  string|null  $value
57
     * @param  bool  $double
58
     *
59
     * @return string|null
60
     */
61 2
    public function e(?string $value, bool $double = true): ?string
62
    {
63 2
        if ($escaped = CallHelper::runOf($this->escaping_methods, $value)) {
64
            return $escaped;
65
        }
66
67 2
        return htmlspecialchars($value, ENT_QUOTES, 'UTF-8', $double);
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type null; however, parameter $string of htmlspecialchars() 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

67
        return htmlspecialchars(/** @scrutinizer ignore-type */ $value, ENT_QUOTES, 'UTF-8', $double);
Loading history...
68
    }
69
70
    /**
71
     * Convert special HTML entities back to characters.
72
     *
73
     * @param  string|null  $value
74
     *
75
     * @return string|null
76
     */
77 2
    public function de(?string $value): ?string
78
    {
79 2
        return htmlspecialchars_decode($value, ENT_QUOTES);
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type null; however, parameter $string of htmlspecialchars_decode() 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

79
        return htmlspecialchars_decode(/** @scrutinizer ignore-type */ $value, ENT_QUOTES);
Loading history...
80
    }
81
82
    /**
83
     * Replacing multiple spaces with a single space.
84
     *
85
     * @param  string|null  $value
86
     *
87
     * @return string|null
88
     */
89 6
    public function removeSpaces(?string $value): ?string
90
    {
91 6
        return preg_replace('!\s+!', ' ', $value);
92
    }
93
94
    /**
95
     * Get a string according to an integer value.
96
     *
97
     * @param  float  $number
98
     * @param  array  $choice
99
     * @param  string|null  $extra
100
     *
101
     * @return string
102
     */
103 2
    public function choice(float $number, array $choice = [], string $extra = null): string
104
    {
105 2
        $number = (int) $number;
106 2
        $mod    = $number % 10;
107
108
        switch (true) {
109 2
            case $mod === 0:
110 2
            case $mod >= 5 && $mod <= 9:
111 2
            case ($number % 100 >= 11) && ($number % 100 <= 20):
112 2
                $result = $choice[2] ?? '';
113 2
                break;
114
115 2
            case $mod >= 2 && $mod <= 4:
116
                $result = $choice[1] ?? '';
117
                break;
118
119
            default:
120 2
                $result = $choice[0] ?? '';
121
        }
122
123 2
        if (empty($extra)) {
124 2
            return trim($result);
125
        }
126
127 2
        return implode(' ', [trim($result), trim($extra)]);
128
    }
129
130
    /**
131
     * Begin a string with a single instance of a given value.
132
     *
133
     * @see https://github.com/illuminate/support/blob/master/Str.php
134
     *
135
     * @param  string|null  $value
136
     * @param  string  $prefix
137
     *
138
     * @return string
139
     */
140 4
    public function start(?string $value, string $prefix): string
141
    {
142 4
        $quoted = preg_quote($prefix, '/');
143
144 4
        return $prefix . preg_replace('/^(?:' . $quoted . ')+/u', '', $value);
145
    }
146
147
    /**
148
     * Cap a string with a single instance of a given value.
149
     *
150
     * @see https://github.com/illuminate/support/blob/master/Str.php
151
     *
152
     * @param  string  $value
153
     * @param  string  $cap
154
     *
155
     * @return string
156
     */
157 4
    public function finish(string $value, string $cap = '/'): string
158
    {
159 4
        $quoted = preg_quote($cap, '/');
160
161 4
        return preg_replace('/(?:' . $quoted . ')+$/u', '', $value) . $cap;
162
    }
163
164
    /**
165
     *  Determine if a given string starts with a given substring.
166
     *
167
     * @param  string  $haystack
168
     * @param  string|string[]  $needles
169
     *
170
     * @return bool
171
     */
172 94
    public function startsWith(string $haystack, $needles): bool
173
    {
174 94
        foreach ((array) $needles as $needle) {
175 94
            if ((string) $needle !== '' && str_starts_with($haystack, $needle)) {
176 94
                return true;
177
            }
178
        }
179
180 92
        return false;
181
    }
182
183
    /**
184
     * Determine if a given string ends with a given substring.
185
     *
186
     * @param  string  $haystack
187
     * @param  string|string[]  $needles
188
     *
189
     * @return bool
190
     */
191 6
    public function endsWith(string $haystack, $needles): bool
192
    {
193 6
        foreach ((array) $needles as $needle) {
194 6
            if ((string) $needle !== '' && str_ends_with($haystack, $needle)) {
195 6
                return true;
196
            }
197
        }
198
199 6
        return false;
200
    }
201
202
    /**
203
     * Convert the given string to lower-case.
204
     *
205
     * @see https://github.com/illuminate/support/blob/master/Str.php
206
     *
207
     * @param  string|null  $value
208
     *
209
     * @return string
210
     */
211 133
    public function lower(?string $value): string
212
    {
213 133
        return mb_strtolower($value, 'UTF-8');
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type null; however, parameter $string of mb_strtolower() 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

213
        return mb_strtolower(/** @scrutinizer ignore-type */ $value, 'UTF-8');
Loading history...
214
    }
215
216
    /**
217
     * Convert the given string to upper-case.
218
     *
219
     * @see https://github.com/illuminate/support/blob/master/Str.php
220
     *
221
     * @param  string|null  $value
222
     *
223
     * @return string
224
     */
225 6
    public function upper(?string $value): ?string
226
    {
227 6
        return mb_strtoupper($value, 'UTF-8');
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type null; however, parameter $string of mb_strtoupper() 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

227
        return mb_strtoupper(/** @scrutinizer ignore-type */ $value, 'UTF-8');
Loading history...
228
    }
229
230
    /**
231
     * Convert a value to studly caps case.
232
     *
233
     * @see https://github.com/illuminate/support/blob/master/Str.php
234
     *
235
     * @param  string|null  $value
236
     *
237
     * @return string|null
238
     */
239 42
    public function studly(?string $value): ?string
240
    {
241 42
        $key = $value;
242
243 42
        if (isset(self::$studlyCache[$key])) {
244 7
            return self::$studlyCache[$key];
245
        }
246
247 36
        $value = ucwords(str_replace(['-', '_'], ' ', $value));
248
249 36
        return self::$studlyCache[$key] = str_replace(' ', '', $value);
250
    }
251
252
    /**
253
     * Convert a value to camel case.
254
     *
255
     * @see https://github.com/illuminate/support/blob/master/Str.php
256
     *
257
     * @param  string|null  $value
258
     *
259
     * @return string|null
260
     */
261 38
    public function camel(?string $value): ?string
262
    {
263 38
        if (isset(self::$camelCache[$value])) {
264 4
            return self::$camelCache[$value];
265
        }
266
267 34
        return self::$camelCache[$value] = lcfirst($this->studly($value));
268
    }
269
270
    /**
271
     * Convert a string to snake case.
272
     *
273
     * @see https://github.com/illuminate/support/blob/master/Str.php
274
     *
275
     * @param  string|null  $value
276
     * @param  string  $delimiter
277
     *
278
     * @return string|null
279
     */
280 4
    public function snake(?string $value, string $delimiter = '_'): ?string
281
    {
282 4
        $key = $value;
283
284 4
        if (isset(self::$snakeCache[$key][$delimiter])) {
285 3
            return self::$snakeCache[$key][$delimiter];
286
        }
287
288 1
        if (! ctype_lower($value)) {
289 1
            $value = preg_replace('/\s+/u', '', ucwords($value));
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type null; however, parameter $string of ucwords() 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

289
            $value = preg_replace('/\s+/u', '', ucwords(/** @scrutinizer ignore-type */ $value));
Loading history...
290
291 1
            $value = $this->lower(preg_replace('/(.)(?=[A-Z])/u', '$1' . $delimiter, $value));
292
        }
293
294 1
        return self::$snakeCache[$key][$delimiter] = $value;
295
    }
296
297
    /**
298
     * Generate a URL friendly "slug" from a given string.
299
     *
300
     * @see https://github.com/illuminate/support/blob/master/Str.php
301
     *
302
     * @param  string  $title
303
     * @param  string  $separator
304
     * @param  string|null  $language
305
     *
306
     * @return string
307
     */
308 6
    public function slug(string $title, string $separator = '-', ?string $language = 'en')
309
    {
310 6
        $title = $language ? $this->ascii($title, $language) : $title;
311
312
        // Convert all dashes/underscores into separator
313 6
        $flip = $separator === '-' ? '_' : '-';
314
315 6
        $title = preg_replace('![' . preg_quote($flip) . ']+!u', $separator, $title);
316
317
        // Replace @ with the word 'at'
318 6
        $title = str_replace('@', $separator . 'at' . $separator, $title);
319
320
        // Remove all characters that are not the separator, letters, numbers, or whitespace.
321 6
        $title = preg_replace('![^' . preg_quote($separator) . '\pL\pN\s]+!u', '', $this->lower($title));
322
323
        // Replace all separator characters and whitespace by a single separator
324 6
        $title = preg_replace('![' . preg_quote($separator) . '\s]+!u', $separator, $title);
325
326 6
        return trim($title, $separator);
327
    }
328
329
    /**
330
     * Convert the given string to title case.
331
     *
332
     * @see https://github.com/illuminate/support/blob/master/Str.php
333
     *
334
     * @param  string|null  $value
335
     *
336
     * @return string|null
337
     */
338 4
    public function title(?string $value): ?string
339
    {
340 4
        if (is_numeric($value)) {
341 4
            return $value;
342
        }
343
344 4
        return mb_convert_case($value, MB_CASE_TITLE, 'UTF-8') ?: null;
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type null; however, parameter $string of mb_convert_case() 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

344
        return mb_convert_case(/** @scrutinizer ignore-type */ $value, MB_CASE_TITLE, 'UTF-8') ?: null;
Loading history...
345
    }
346
347
    /**
348
     * Return the length of the given string.
349
     *
350
     * @see https://github.com/illuminate/support/blob/master/Str.php
351
     *
352
     * @param  string|null  $value
353
     * @param  string|null  $encoding
354
     *
355
     * @return int
356
     */
357 2
    public function length(?string $value, string $encoding = null): int
358
    {
359 2
        return $encoding
360 2
            ? mb_strlen($value, $encoding)
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type null; however, parameter $string of mb_strlen() 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

360
            ? mb_strlen(/** @scrutinizer ignore-type */ $value, $encoding)
Loading history...
361 2
            : mb_strlen($value);
362
    }
363
364
    /**
365
     * Returns the portion of string specified by the start and length parameters.
366
     *
367
     * @see https://github.com/illuminate/support/blob/master/Str.php
368
     *
369
     * @param  string  $string
370
     * @param  int  $start
371
     * @param  int|null  $length
372
     *
373
     * @return string|null
374
     */
375 4
    public function substr(string $string, int $start, int $length = null): ?string
376
    {
377 4
        return mb_substr($string, $start, $length, 'UTF-8');
378
    }
379
380
    /**
381
     * Replace all occurrences of the search string with the replacement string.
382
     *
383
     * @param  string  $template
384
     * @param  array  $values
385
     * @param  string|null  $key_format
386
     *
387
     * @return string
388
     */
389 22
    public function replace(string $template, array $values, string $key_format = null): string
390
    {
391 22
        $keys = Replace::toFormatArray(array_keys($values), $key_format);
392
393 22
        return str_replace($keys, array_values($values), $template);
394
    }
395
396
    /**
397
     * Get the portion of a string before the first occurrence of a given value.
398
     *
399
     * @see https://github.com/illuminate/support/blob/master/Str.php
400
     *
401
     * @param  string  $subject
402
     * @param  string  $search
403
     *
404
     * @return string
405
     */
406 6
    public function before(string $subject, string $search): ?string
407
    {
408 6
        return ! empty($search) ? explode($search, $subject)[0] : null;
409
    }
410
411
    /**
412
     * Return the remainder of a string after the first occurrence of a given value.
413
     *
414
     * @see https://github.com/illuminate/support/blob/master/Str.php
415
     *
416
     * @param  string  $subject
417
     * @param  string  $search
418
     *
419
     * @return string
420
     */
421 94
    public function after(string $subject, string $search): ?string
422
    {
423 94
        return ! empty($search) ? array_reverse(explode($search, $subject, 2))[0] : null;
424
    }
425
426
    /**
427
     * Determine if a given string contains a given substring.
428
     *
429
     * @param  string  $haystack
430
     * @param  string|string[]  $needles
431
     *
432
     * @return bool
433
     */
434 26
    public function contains(string $haystack, $needles): bool
435
    {
436 26
        foreach ((array) $needles as $needle) {
437 26
            if ((string) $needle !== '' && str_contains($haystack, $needle)) {
438 24
                return true;
439
            }
440
        }
441
442 4
        return false;
443
    }
444
445
    /**
446
     * Generate a more truly "random" alpha-numeric string.
447
     *
448
     * @see https://github.com/illuminate/support/blob/master/Str.php
449
     *
450
     * @param  int  $length
451
     *
452
     * @throws \Exception
453
     *
454
     * @return string
455
     */
456 2
    public function random(int $length = 16): string
457
    {
458 2
        $string = '';
459
460 2
        while (($len = strlen($string)) < $length) {
461 2
            $size = $length - $len;
462
463 2
            $bytes = random_bytes($size);
464
465 2
            $string .= substr(str_replace(['/', '+', '='], '', base64_encode($bytes)), 0, $size);
466
        }
467
468 2
        return $string;
469
    }
470
471
    /**
472
     * Get the string matching the given pattern.
473
     *
474
     * @see https://github.com/illuminate/support/blob/master/Str.php
475
     *
476
     * @param  string  $value
477
     * @param  string  $pattern
478 8
     *
479
     * @return string|null
480 8
     */
481
    public function match(string $value, string $pattern): ?string
482 8
    {
483
        preg_match($pattern, $value, $matches);
484
485
        return ! $matches ? null : ($matches[1] ?? $matches[0]);
486
    }
487
488
    /**
489
     * Determines if the value is empty.
490
     *
491
     * @param  mixed  $value
492 2
     *
493
     * @return bool
494 2
     */
495
    public function isEmpty($value): bool
496
    {
497
        $value = is_string($value) ? trim($value) : $value;
498
499
        return empty($value) && ! is_numeric($value) && (is_string($value) || is_null($value));
500
    }
501
502
    /**
503
     * Determines if the value is doesn't empty.
504
     *
505
     * @param  mixed  $value
506
     *
507 10
     * @return bool
508
     */
509 10
    public function doesntEmpty($value): bool
510
    {
511
        return ! $this->isEmpty($value);
512
    }
513
514
    /**
515
     * Transliterate a UTF-8 value to ASCII.
516
     *
517
     * @see https://github.com/illuminate/support/blob/master/Str.php
518
     *
519 2
     * @param  string|null  $value
520
     * @param  string|null  $language
521 2
     *
522
     * @return string
523
     */
524
    public function ascii(?string $value, ?string $language = 'en'): string
525
    {
526
        return ASCII::to_ascii((string) $value, $language);
0 ignored issues
show
Bug introduced by
It seems like $language can also be of type null; however, parameter $language of voku\helper\ASCII::to_ascii() 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

526
        return ASCII::to_ascii((string) $value, /** @scrutinizer ignore-type */ $language);
Loading history...
527
    }
528
529
    /**
530
     * Converts a value to a string.
531
     *
532
     * @param  string|null  $value
533
     *
534
     * @return string
535
     */
536
    public function convertToString(?string $value): string
537
    {
538
        return is_null($value) ? 'null' : $value;
539
    }
540
}
541