1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LAG\AdminBundle\Utils; |
4
|
|
|
|
5
|
|
|
class StringUtils |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Return the translation pattern with keys "{admin}" and "{key}" replaced by their values. |
9
|
|
|
* |
10
|
|
|
* @param string $translationPattern |
11
|
|
|
* @param string $adminName |
12
|
|
|
* @param string $key |
13
|
|
|
* |
14
|
|
|
* @return string |
15
|
|
|
*/ |
16
|
|
|
public static function getTranslationKey( |
17
|
|
|
string $translationPattern, |
18
|
|
|
string $adminName, |
19
|
|
|
string $key |
20
|
|
|
): string { |
21
|
|
|
$translationPattern = str_replace('{key}', $key, $translationPattern); |
22
|
|
|
$translationPattern = str_replace('{admin}', $adminName, $translationPattern); |
23
|
|
|
|
24
|
|
|
return $translationPattern; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Camelize a string. |
29
|
|
|
* |
30
|
|
|
* @param string $id A string to camelize |
31
|
|
|
* |
32
|
|
|
* @return string The camelized string |
33
|
|
|
*/ |
34
|
|
|
public static function camelize($id): string |
35
|
|
|
{ |
36
|
|
|
return strtr(ucwords(strtr($id, ['_' => ' ', '.' => '_ ', '\\' => '_ '])), [' ' => '']); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* A string to underscore. |
41
|
|
|
* |
42
|
|
|
* @param string $id The string to underscore |
43
|
|
|
* |
44
|
|
|
* @return string The underscored string |
45
|
|
|
*/ |
46
|
|
|
public static function underscore($id): string |
47
|
|
|
{ |
48
|
|
|
return strtolower(preg_replace([ |
49
|
|
|
'/([A-Z]+)([A-Z][a-z])/', |
50
|
|
|
'/([a-z\d])([A-Z])/' |
51
|
|
|
], [ |
52
|
|
|
'\\1_\\2', |
53
|
|
|
'\\1_\\2' |
54
|
|
|
], |
55
|
|
|
str_replace('_', '.', $id)) |
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Return true if the given string starts with $start. |
61
|
|
|
* |
62
|
|
|
* @param string $string |
63
|
|
|
* @param string $start |
64
|
|
|
* |
65
|
|
|
* @return bool |
66
|
|
|
*/ |
67
|
|
|
public static function startWith(string $string, string $start): bool |
68
|
|
|
{ |
69
|
|
|
return substr($string, 0, strlen($start)) === $start; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|