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
|
|
|
* Return the translation pattern with keys "{admin}" and "{key}" replaced by their values. |
29
|
|
|
* |
30
|
|
|
* @param string $translationPattern |
31
|
|
|
* @param string $adminName |
32
|
|
|
* @param string $actionName |
33
|
|
|
* |
34
|
|
|
* @return string |
35
|
|
|
*/ |
36
|
|
|
public static function getActionTranslationKey( |
37
|
|
|
string $translationPattern, |
38
|
|
|
string $adminName, |
39
|
|
|
string $actionName |
40
|
|
|
): string { |
41
|
|
|
$translationPattern = str_replace('{key}', $actionName, $translationPattern); |
42
|
|
|
$translationPattern = str_replace('{admin}', $adminName, $translationPattern); |
43
|
|
|
|
44
|
|
|
return $translationPattern; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Camelize a string. |
49
|
|
|
* |
50
|
|
|
* @param string $id A string to camelize |
51
|
|
|
* |
52
|
|
|
* @return string The camelized string |
53
|
|
|
*/ |
54
|
|
|
public static function camelize($id): string |
55
|
|
|
{ |
56
|
|
|
return strtr(ucwords(strtr($id, ['_' => ' ', '.' => '_ ', '\\' => '_ '])), [' ' => '']); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* A string to underscore. |
61
|
|
|
* |
62
|
|
|
* @param string $id The string to underscore |
63
|
|
|
* |
64
|
|
|
* @return string The underscored string |
65
|
|
|
*/ |
66
|
|
|
public static function underscore($id): string |
67
|
|
|
{ |
68
|
|
|
return strtolower(preg_replace([ |
69
|
|
|
'/([A-Z]+)([A-Z][a-z])/', |
70
|
|
|
'/([a-z\d])([A-Z])/' |
71
|
|
|
], [ |
72
|
|
|
'\\1_\\2', |
73
|
|
|
'\\1_\\2' |
74
|
|
|
], |
75
|
|
|
str_replace('_', '.', $id)) |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Return true if the given string starts with $start. |
81
|
|
|
* |
82
|
|
|
* @param string $string |
83
|
|
|
* @param string $start |
84
|
|
|
* |
85
|
|
|
* @return bool |
86
|
|
|
*/ |
87
|
|
|
public static function startWith(string $string, string $start): bool |
88
|
|
|
{ |
89
|
|
|
return substr($string, 0, strlen($start)) === $start; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|