1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace FRZB\Component\RequestMapper\Helper; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @internal |
9
|
|
|
*/ |
10
|
|
|
final class StringHelper |
11
|
|
|
{ |
12
|
|
|
public static function toSnakeCase(string $value): string |
13
|
|
|
{ |
14
|
|
|
return strtolower(preg_replace('/[A-Z]/', '_\\0', lcfirst($value))); |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
public static function toKebabCase(string $value): string |
18
|
|
|
{ |
19
|
|
|
return strtolower(preg_replace('/[A-Z]/', '-\\0', lcfirst($value))); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public static function toCamelCase(string $value): string |
23
|
|
|
{ |
24
|
|
|
return str_replace(' ', '', ucwords(str_replace(['-', '_'], ' ', $value))); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public static function toLowerCamelCase(string $value): string |
28
|
|
|
{ |
29
|
|
|
return lcfirst(str_replace(' ', '', ucwords(str_replace(['-', '_'], ' ', $value)))); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public static function contains(string $value, string $subValue): bool |
33
|
|
|
{ |
34
|
|
|
return str_contains($value, $subValue); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public static function makePrefix(string $prefix, ?string $value = null, string $delimiter = '-'): string |
38
|
|
|
{ |
39
|
|
|
return $value |
40
|
|
|
? self::normalize($prefix).$delimiter.$value |
41
|
|
|
: self::normalize($prefix); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public static function normalize(string $value): string |
45
|
|
|
{ |
46
|
|
|
return strtolower(preg_replace('/[^a-zA-Z\\d_-]/', '-', $value)); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public static function removeBrackets(string $value, array $brackets = ['[', ']']): string |
50
|
|
|
{ |
51
|
|
|
return str_replace($brackets, '', $value); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|