-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStrings.php
More file actions
194 lines (152 loc) · 3.95 KB
/
Strings.php
File metadata and controls
194 lines (152 loc) · 3.95 KB
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
<?php declare(strict_types=1);
namespace h4kuna\DataType\Basic;
use h4kuna\DataType\Exceptions\InvalidStateException;
use h4kuna\DataType\Exceptions\InvalidTypeException;
use h4kuna\DataType\Location;
use Nette\StaticClass;
final class Strings
{
use StaticClass;
public static function nullable(mixed $value): ?string
{
return $value === null ? null : self::from($value);
}
public static function strokeToPoint(string $value): string
{
return strtr($value, ',', '.');
}
public static function key(string|int ...$values): string
{
return self::join($values, "\x00");
}
public static function toFloat(string $value): float
{
return Floats::from($value);
}
public static function from(mixed $value): string
{
if (is_int($value) || is_float($value) || is_null($value)) {
return (string) $value;
} elseif (is_string($value) === false) {
throw InvalidTypeException::invalidString($value);
}
return $value;
}
public static function toInt(string $value): int
{
return Integer::from($value);
}
/**
* @return array{lat: float, long: float}
*/
public static function toGps(string $value): array
{
return Location\Gps::fromString($value);
}
/**
* @return array<string, true>
*/
public static function toSet(string $value): array
{
return Set::fromString($value);
}
/**
* foo_bar => FooBar
*/
public static function toPascal(string $string): string
{
return ucfirst(self::toCamel($string));
}
/**
* foo_bar => fooBar
*/
public static function toCamel(string $string): string
{
return (string) preg_replace_callback('/_([a-z])/', static function (array $find): string {
return strtoupper($find[1]);
}, $string);
}
/**
* The original explode(',', '') return [''] right is [].
*
* @return array<string>
*/
public static function split(string $value, string $delimiter = ', '): array
{
if ($delimiter === '') {
throw new InvalidStateException('Delimiter like empty string is not allowed.');
} elseif ($value === '') {
return [];
}
return explode($delimiter, $value);
}
/**
* The original implode/join(',', ['', null, false, 'A']) return ',,,A' right is 'A'.
*
* @param array<scalar|null> $array
*/
public static function join(array $array, string $delimiter = ', '): string
{
return implode(
$delimiter,
array_filter($array, static fn (mixed $value): bool => $value !== false && $value !== '' && $value !== null)
);
}
/**
* FooBar => foo_bar
*/
public static function toUnderscore(string $string): string
{
return strtolower((string) preg_replace_callback('/(.)([A-Z][a-z])|([a-z])([A-Z])/', static function (
array $find,
): string {
if ($find[1] !== '') {
return $find[1] . '_' . $find[2];
}
return $find[3] . '_' . $find[4];
}, $string));
}
public static function replaceStart(
string $subject,
string $search,
string $replacement = '',
): string
{
return self::strictReplace($subject, $search, $replacement, '^%s', 1);
}
private static function strictReplace(
string $subject,
string $search,
string $replacement,
string $pattern,
int $limit = -1,
): string
{
return preg_replace(
sprintf(self::padIfNeed($pattern, '#', STR_PAD_BOTH), preg_quote($search, '#')),
$replacement,
$subject,
$limit,
) ?? $search;
}
public static function padIfNeed(string $string, string $padString = '/', int $padType = STR_PAD_LEFT): string
{
$length = mb_strlen($padString);
$prefix = $suffix = '';
if (($padType === STR_PAD_LEFT || $padType === STR_PAD_BOTH) && mb_substr($string, 0, $length) !== $padString) {
$prefix = $padString;
}
if (($padType === STR_PAD_RIGHT || $padType === STR_PAD_BOTH) && mb_substr($string, -$length) !== $padString) {
$suffix = $padString;
}
return "$prefix$string$suffix";
}
public static function replaceEnd(
string $subject,
string $search,
string $replacement = '',
): string
{
return self::strictReplace($subject, $search, $replacement, '%s$', 1);
}
}