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\DependencyInjection\Helper; |
17
|
|
|
|
18
|
|
|
use JetBrains\PhpStorm\Immutable; |
19
|
|
|
|
20
|
|
|
/** @internal */ |
21
|
|
|
#[Immutable] |
22
|
|
|
final class StringHelper |
23
|
|
|
{ |
24
|
|
|
private function __construct() {} |
25
|
|
|
|
26
|
|
|
public static function toSnakeCase(string $value): string |
27
|
|
|
{ |
28
|
|
|
return strtolower(preg_replace('/[A-Z]/', '_\\0', lcfirst($value))); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public static function toKebabCase(string $value): string |
32
|
|
|
{ |
33
|
|
|
return strtolower(preg_replace('/[A-Z]/', '-\\0', lcfirst($value))); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public static function toPascalCase(string $value): string |
37
|
|
|
{ |
38
|
|
|
return str_replace(' ', '', ucwords(str_replace(['-', '_'], ' ', $value))); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public static function toCamelCase(string $value): string |
42
|
|
|
{ |
43
|
|
|
return lcfirst(str_replace(' ', '', ucwords(str_replace(['-', '_'], ' ', $value)))); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public static function contains(string $value, string $subValue): bool |
47
|
|
|
{ |
48
|
|
|
return str_contains($value, $subValue); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public static function makePrefix(string $prefix, ?string $value = null, string $delimiter = '-'): string |
52
|
|
|
{ |
53
|
|
|
return $value |
54
|
|
|
? self::normalize($prefix).$delimiter.$value |
55
|
|
|
: self::normalize($prefix); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public static function normalize(string $value): string |
59
|
|
|
{ |
60
|
|
|
return strtolower(preg_replace('/[^a-zA-Z\\d_-]/', '-', $value)); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public static function removeBrackets(string $value, array $brackets = ['[', ']']): string |
64
|
|
|
{ |
65
|
|
|
return str_replace($brackets, '', $value); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public static function removeNotWordCharacters(string $value): string |
69
|
|
|
{ |
70
|
|
|
return preg_replace('/[^a-zA-Z0-9\\\\]/', '', $value); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|