-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStringable.php
More file actions
268 lines (217 loc) · 7.56 KB
/
Copy pathStringable.php
File metadata and controls
268 lines (217 loc) · 7.56 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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
<?php
declare(strict_types=1);
namespace Zero\Lib\Support;
use BadMethodCallException;
use ReflectionMethod;
use ReflectionNamedType;
use ReflectionType;
use ReflectionUnionType;
final class Stringable
{
private string $value;
public function __construct(string $value)
{
$this->value = $value;
}
public function __toString(): string
{
return $this->value;
}
public function value(): string
{
return $this->value;
}
public function toString(): string
{
return $this->value;
}
public function __call(string $method, array $parameters): mixed
{
if (!method_exists(Str::class, $method)) {
throw new BadMethodCallException(sprintf('Method %s::%s does not exist.', Str::class, $method));
}
$reflection = new ReflectionMethod(Str::class, $method);
if ($reflection->getNumberOfParameters() === 0) {
throw new BadMethodCallException(sprintf('Method %s::%s is not chainable.', Str::class, $method));
}
$firstParameter = $reflection->getParameters()[0];
$type = $firstParameter->getType();
if ($type !== null && !self::typeAllowsString($type)) {
throw new BadMethodCallException(sprintf('Method %s::%s cannot be chained from Stringable.', Str::class, $method));
}
$result = Str::$method($this->value, ...$parameters);
if (is_string($result)) {
return new self($result);
}
return $result;
}
public function pipe(callable $callback): mixed
{
return $callback($this);
}
public function tap(callable $callback): self
{
$callback($this);
return $this;
}
public function append(string ...$values): self
{
return new self($this->value . implode('', $values));
}
public function prepend(string ...$values): self
{
return new self(implode('', $values) . $this->value);
}
public function basename(string $suffix = ''): self
{
return new self(basename($this->value, $suffix));
}
public function dirname(int $levels = 1): self
{
return new self(dirname($this->value, $levels));
}
public function classBasename(): self
{
$parts = explode('\\', $this->value);
return new self((string) end($parts));
}
public function exactly(string $value): bool
{
return $this->value === $value;
}
public function isEmpty(): bool
{
return $this->value === '';
}
public function isNotEmpty(): bool
{
return $this->value !== '';
}
/**
* @return array<int, string>
*/
public function explode(string $delimiter, int $limit = PHP_INT_MAX): array
{
return explode($delimiter, $this->value, $limit);
}
/**
* @return array<int, string>
*/
public function split(string $pattern, int $limit = -1, int $flags = 0): array
{
$result = preg_split($pattern, $this->value, $limit, $flags);
return $result === false ? [] : $result;
}
/**
* @return array<int|string, mixed>
*/
public function scan(string $format): array
{
$result = sscanf($this->value, $format);
return $result === null ? [] : $result;
}
public function hash(string $algorithm = 'sha256'): self
{
return new self(hash($algorithm, $this->value));
}
public function newLine(int $count = 1): self
{
return new self($this->value . str_repeat(PHP_EOL, max(0, $count)));
}
public function test(string $pattern): bool
{
return preg_match($pattern, $this->value) === 1;
}
public function stripTags(?string $allowed = null): self
{
return new self($allowed === null ? strip_tags($this->value) : strip_tags($this->value, $allowed));
}
public function when(mixed $condition, callable $callback, ?callable $default = null): self
{
$resolved = is_callable($condition) ? $condition($this) : $condition;
if ($resolved) {
$result = $callback($this, $resolved);
} elseif ($default !== null) {
$result = $default($this, $resolved);
} else {
$result = $this;
}
return $result instanceof self ? $result : (is_string($result) ? new self($result) : $this);
}
public function unless(mixed $condition, callable $callback, ?callable $default = null): self
{
$resolved = is_callable($condition) ? $condition($this) : $condition;
return $this->when(! $resolved, $callback, $default);
}
public function whenEmpty(callable $callback, ?callable $default = null): self
{
return $this->when($this->isEmpty(), $callback, $default);
}
public function whenNotEmpty(callable $callback, ?callable $default = null): self
{
return $this->when($this->isNotEmpty(), $callback, $default);
}
public function whenContains(string|array $needles, callable $callback, ?callable $default = null): self
{
$needles = is_array($needles) ? $needles : [$needles];
$matched = false;
foreach ($needles as $needle) {
if ($needle !== '' && str_contains($this->value, $needle)) {
$matched = true;
break;
}
}
return $this->when($matched, $callback, $default);
}
public function whenStartsWith(string|array $needles, callable $callback, ?callable $default = null): self
{
return $this->when(Str::startsWithAny($this->value, (array) $needles), $callback, $default);
}
public function whenEndsWith(string|array $needles, callable $callback, ?callable $default = null): self
{
return $this->when(Str::endsWithAny($this->value, (array) $needles), $callback, $default);
}
public function whenExactly(string $value, callable $callback, ?callable $default = null): self
{
return $this->when($this->value === $value, $callback, $default);
}
public function whenNotExactly(string $value, callable $callback, ?callable $default = null): self
{
return $this->when($this->value !== $value, $callback, $default);
}
public function whenIs(string|array $patterns, callable $callback, ?callable $default = null): self
{
return $this->when(Str::is($patterns, $this->value), $callback, $default);
}
public function whenIsAscii(callable $callback, ?callable $default = null): self
{
return $this->when(Str::isAscii($this->value), $callback, $default);
}
public function whenIsUuid(callable $callback, ?callable $default = null): self
{
return $this->when(Str::isUuid($this->value), $callback, $default);
}
public function whenIsUlid(callable $callback, ?callable $default = null): self
{
return $this->when(Str::isUlid($this->value), $callback, $default);
}
public function whenTest(string $pattern, callable $callback, ?callable $default = null): self
{
return $this->when($this->test($pattern), $callback, $default);
}
private static function typeAllowsString(ReflectionType $type): bool
{
if ($type instanceof ReflectionNamedType) {
return $type->getName() === 'string' || $type->getName() === 'mixed';
}
if ($type instanceof ReflectionUnionType) {
foreach ($type->getTypes() as $innerType) {
if (self::typeAllowsString($innerType)) {
return true;
}
}
return false;
}
return false;
}
}