1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
7
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
8
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
9
|
|
|
* |
10
|
|
|
* Copyright (c) 2024 Mykhailo Shtanko [email protected] |
11
|
|
|
* |
12
|
|
|
* For the full copyright and license information, please view the LICENSE.MD |
13
|
|
|
* file that was distributed with this source code. |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
namespace FRZB\Component\MetricsPower\Helper; |
17
|
|
|
|
18
|
|
|
use FRZB\Component\MetricsPower\Traits\WithPrivateEmptyConstructor; |
19
|
|
|
use JetBrains\PhpStorm\Immutable; |
20
|
|
|
|
21
|
|
|
/** @internal */ |
22
|
|
|
#[Immutable] |
23
|
|
|
final class StringHelper |
24
|
|
|
{ |
25
|
|
|
use WithPrivateEmptyConstructor; |
26
|
|
|
|
27
|
|
|
public static function toSnakeCase(string $value): string |
28
|
|
|
{ |
29
|
|
|
return strtolower(preg_replace('/[A-Z]/', '_\\0', lcfirst($value))); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public static function toKebabCase(string $value): string |
33
|
|
|
{ |
34
|
|
|
return strtolower(preg_replace('/[A-Z]/', '-\\0', lcfirst($value))); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public static function toPascalCase(string $value): string |
38
|
|
|
{ |
39
|
|
|
return str_replace(' ', '', ucwords(str_replace(['-', '_'], ' ', $value))); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public static function toCamelCase(string $value): string |
43
|
|
|
{ |
44
|
|
|
return lcfirst(str_replace(' ', '', ucwords(str_replace(['-', '_'], ' ', $value)))); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public static function contains(string $value, string $subValue): bool |
48
|
|
|
{ |
49
|
|
|
return str_contains($value, $subValue); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public static function makePrefix(string $prefix, ?string $value = null, string $delimiter = '-'): string |
53
|
|
|
{ |
54
|
|
|
return $value |
55
|
|
|
? self::normalize($prefix).$delimiter.$value |
56
|
|
|
: self::normalize($prefix); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public static function normalize(string $value): string |
60
|
|
|
{ |
61
|
|
|
return strtolower(preg_replace('/[^a-zA-Z\\d_-]/', '-', $value)); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public static function removeBrackets(string $value, array $brackets = ['[', ']']): string |
65
|
|
|
{ |
66
|
|
|
return str_replace($brackets, '', $value); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|