-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStrNormalise.php
226 lines (198 loc) · 5.46 KB
/
StrNormalise.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<?php
/**
* Part of ww4 project.
*
* @copyright Copyright (C) 2019 __ORGANIZATION__.
* @license LGPL-2.0-or-later
*/
declare(strict_types=1);
namespace Windwalker\String;
use Windwalker\Utilities\Arr;
/**
* The StrNotmalise class.
*
* @since 3.5.17
*/
class StrNormalise
{
public static function splitCamelCase(string $str): array
{
return preg_split('/(?<=[^A-Z_])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][^A-Z_])/x', $str);
}
public static function splitBy(string $str, string $separator = '_'): array
{
return array_filter(explode($separator, $str), 'strlen');
}
public static function splitAll(string $str): array
{
$strs = preg_split('#[ \-_\.]+#', $str);
$strs = array_map([static::class, 'splitCamelCase'], $strs);
return array_values(Arr::flatten($strs));
}
/**
* Separate a string by custom separator.
*
* @param string $input The string input (ASCII only).
* @param string $separator The separator to want to separate it.
*
* @return string The string be converted.
*
* @since 2.1
*/
public static function separate(string $input, string $separator = '_'): string
{
return implode($separator, self::splitAll($input));
}
/**
* Method to convert a string into camel case.
*
* @param string $input The string input (ASCII only).
*
* @return string The camel case string.
*/
public static function toCamelCase(string $input): string
{
return lcfirst(static::toPascalCase($input));
}
/**
* Method to convert a string into pascal case.
*
* @param string $input The string input (ASCII only).
*
* @return string The camel case string.
*
* @since 2.0
*/
public static function toPascalCase(string $input): string
{
return implode('', array_map('ucfirst', self::splitAll($input)));
}
/**
* Method to convert a string into dash separated form.
*
* @param string $input The string input (ASCII only).
*
* @return string The dash separated string.
*
* @since 2.0
*/
public static function toDashSeparated(string $input): string
{
return self::separate($input, '-');
}
/**
* Dash separated alias.
*
* @param string $input
*
* @return string
*/
public static function toKebabCase(string $input): string
{
return static::toDashSeparated($input);
}
/**
* Method to convert a string into space separated form.
*
* @param string $input The string input (ASCII only).
*
* @return string The space separated string.
*
* @since 2.0
*/
public static function toSpaceSeparated(string $input): string
{
// Convert underscores and dashes to spaces.
return static::separate($input, ' ');
}
/**
* Method to convert a string into dot separated form.
*
* @param string $input The string input (ASCII only).
*
* @return string The dot separated string.
*
* @since 2.1
*/
public static function toDotSeparated(string $input): string
{
// Convert underscores and dashes to dots.
return static::separate($input, '.');
}
/**
* Method to convert a string into underscore separated form.
*
* @param string $input The string input (ASCII only).
*
* @return string The underscore separated string.
*
* @since 2.0
*/
public static function toSnakeCase(string $input): string
{
// Convert spaces and dashes to underscores.
return static::separate($input, '_');
}
/**
* Alias of toSnakeCase()
*
* @param string $input
*
* @return string
*/
public static function toUnderscoreSeparated(string $input): string
{
return static::toSnakeCase($input);
}
/**
* Method to convert a string into variable form.
*
* @param string $input The string input (ASCII only).
*
* @return string The variable string.
*
* @since 2.0
*/
public static function toVariable(string $input): string
{
// Remove dashes and underscores, then convert to camel case.
$input = self::toCamelCase($input);
// Remove leading digits.
$input = preg_replace('#^[0-9]+.*$#', '', $input);
// Lowercase the first character.
$input[0] = strtolower($input[0] ?? '');
return $input;
}
/**
* Method to convert a string into key form.
*
* @param string $input The string input (ASCII only).
*
* @return string The key string.
*
* @since 2.0
*/
public static function toKey(string $input): string
{
// Remove spaces and dashes, then convert to lower case.
$input = self::toUnderscoreSeparated($input);
$input = strtolower($input);
return $input;
}
/**
* Convert to standard PSR-0/PSR-4 class name.
*
* @param string $class The class name string.
*
* @return string Normalised class name.
*
* @since 2.0
*/
public static function toClassNamespace(string $class): string
{
$class = trim($class, '\\');
$class = (string) str_replace(['\\', '/'], ' ', $class);
$class = ucwords($class);
return str_replace(' ', '\\', $class);
}
}