|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LAG\Component\StringUtils; |
|
4
|
|
|
|
|
5
|
|
|
class StringUtils |
|
6
|
|
|
{ |
|
7
|
|
|
/** |
|
8
|
|
|
* Camelize a string. |
|
9
|
|
|
* |
|
10
|
|
|
* @param string $id A string to camelize |
|
11
|
|
|
* @param bool $lowerCase |
|
12
|
|
|
* |
|
13
|
|
|
* @return string The camelized string |
|
14
|
|
|
*/ |
|
15
|
|
|
public static function camelize(string $id, bool $lowerCase = false): string |
|
16
|
|
|
{ |
|
17
|
|
|
$value = strtr(ucwords(strtr($id, ['_' => ' ', '.' => '_ ', '\\' => '_ '])), [' ' => '']); |
|
18
|
|
|
|
|
19
|
|
|
if ($lowerCase) { |
|
20
|
|
|
$value = strtolower($value); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
return $value; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* A string to underscore. |
|
28
|
|
|
* |
|
29
|
|
|
* @param string $value The string to underscore |
|
30
|
|
|
* |
|
31
|
|
|
* @return string The underscored string |
|
32
|
|
|
*/ |
|
33
|
|
|
public static function underscore(string $value): string |
|
34
|
|
|
{ |
|
35
|
|
|
$value = strtolower(preg_replace([ |
|
36
|
|
|
'/([A-Z]+)([A-Z][a-z])/', |
|
37
|
|
|
'/([a-z\d])([A-Z])/', |
|
38
|
|
|
], [ |
|
39
|
|
|
'\\1_\\2', |
|
40
|
|
|
'\\1_\\2', |
|
41
|
|
|
], |
|
42
|
|
|
str_replace('_', '.', $value)) |
|
43
|
|
|
); |
|
44
|
|
|
|
|
45
|
|
|
$value = preg_replace('/\B([A-Z])/', '_$1', $value); |
|
46
|
|
|
|
|
47
|
|
|
return $value; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Return the start of a string. |
|
52
|
|
|
* |
|
53
|
|
|
* @param string $string The string |
|
54
|
|
|
* @param int $length The number of characters |
|
55
|
|
|
* |
|
56
|
|
|
* @return string |
|
57
|
|
|
*/ |
|
58
|
|
|
public static function start(string $string, int $length = 1): string |
|
59
|
|
|
{ |
|
60
|
|
|
return substr($string, 0, $length); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Return the end of a string. |
|
65
|
|
|
* |
|
66
|
|
|
* @param string $string The string |
|
67
|
|
|
* @param int $length The number of characters |
|
68
|
|
|
* |
|
69
|
|
|
* @return string |
|
70
|
|
|
*/ |
|
71
|
|
|
public static function end(string $string, int $length = 1): string |
|
72
|
|
|
{ |
|
73
|
|
|
return substr($string, -$length); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Return true if the given string starts with $start. |
|
78
|
|
|
* |
|
79
|
|
|
* @param string $string |
|
80
|
|
|
* @param string $start |
|
81
|
|
|
* |
|
82
|
|
|
* @return bool |
|
83
|
|
|
*/ |
|
84
|
|
|
public static function startsWith(string $string, string $start): bool |
|
85
|
|
|
{ |
|
86
|
|
|
return self::start($string, strlen($start)) === $start; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Return true if the given string starts with $start. |
|
91
|
|
|
* |
|
92
|
|
|
* @param string $string |
|
93
|
|
|
* @param string $end |
|
94
|
|
|
* |
|
95
|
|
|
* @return bool |
|
96
|
|
|
*/ |
|
97
|
|
|
public static function endsWith(string $string, string $end): bool |
|
98
|
|
|
{ |
|
99
|
|
|
return self::end($string, strlen($end)) === $end; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|