1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Valkyrja Framework package. |
7
|
|
|
* |
8
|
|
|
* (c) Melech Mizrachi <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Valkyrja\Type\Types; |
15
|
|
|
|
16
|
|
|
use Valkyrja\Type\StringT as Contract; |
17
|
|
|
use Valkyrja\Type\Support\Str as Helper; |
18
|
|
|
use Valkyrja\Type\Support\StrCase; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class Str. |
22
|
|
|
* |
23
|
|
|
* @author Melech Mizrachi |
24
|
|
|
* |
25
|
|
|
* @extends Type<string> |
26
|
|
|
*/ |
27
|
|
|
class StringT extends Type implements Contract |
28
|
|
|
{ |
29
|
|
|
public function __construct(string $subject) |
30
|
|
|
{ |
31
|
|
|
parent::__construct($subject); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @inheritDoc |
36
|
|
|
*/ |
37
|
|
|
public static function fromValue(mixed $value): static |
38
|
|
|
{ |
39
|
|
|
return new static((string) $value); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @inheritDoc |
44
|
|
|
*/ |
45
|
|
|
public function asValue(): string |
46
|
|
|
{ |
47
|
|
|
return $this->subject; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @inheritDoc |
52
|
|
|
*/ |
53
|
|
|
public function asFlatValue(): string |
54
|
|
|
{ |
55
|
|
|
return $this->asValue(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @inheritDoc |
60
|
|
|
*/ |
61
|
|
|
public function startsWith(string $needle): bool |
62
|
|
|
{ |
63
|
|
|
return Helper::startsWith($this->subject, $needle); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @inheritDoc |
68
|
|
|
*/ |
69
|
|
|
public function startsWithAny(string ...$needles): bool |
70
|
|
|
{ |
71
|
|
|
return Helper::startsWithAny($this->subject, ...$needles); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @inheritDoc |
76
|
|
|
*/ |
77
|
|
|
public function endsWith(string $needle): bool |
78
|
|
|
{ |
79
|
|
|
return Helper::endsWith($this->subject, $needle); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @inheritDoc |
84
|
|
|
*/ |
85
|
|
|
public function endsWithAny(string ...$needles): bool |
86
|
|
|
{ |
87
|
|
|
return Helper::endsWithAny($this->subject, ...$needles); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @inheritDoc |
92
|
|
|
*/ |
93
|
|
|
public function contains(string $needle): bool |
94
|
|
|
{ |
95
|
|
|
return Helper::contains($this->subject, $needle); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @inheritDoc |
100
|
|
|
*/ |
101
|
|
|
public function containsAny(string ...$needles): bool |
102
|
|
|
{ |
103
|
|
|
return Helper::containsAny($this->subject, ...$needles); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @inheritDoc |
108
|
|
|
*/ |
109
|
|
|
public function min(int $min = 0): bool |
110
|
|
|
{ |
111
|
|
|
return Helper::min($this->subject, $min); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @inheritDoc |
116
|
|
|
*/ |
117
|
|
|
public function max(int $max = 256): bool |
118
|
|
|
{ |
119
|
|
|
return Helper::max($this->subject, $max); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @inheritDoc |
124
|
|
|
*/ |
125
|
|
|
public function replace(string $replace, string $replacement): static |
126
|
|
|
{ |
127
|
|
|
return $this->modify(static fn (string $subject): string => Helper::replace($subject, $replace, $replacement)); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @inheritDoc |
132
|
|
|
*/ |
133
|
|
|
public function replaceAll(array $replace, array $replacement): static |
134
|
|
|
{ |
135
|
|
|
return $this->modify(static fn (string $subject): string => Helper::replaceAll($subject, $replace, $replacement)); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @inheritDoc |
140
|
|
|
*/ |
141
|
|
|
public function replaceAllWith(array $replace, string $replacement): static |
142
|
|
|
{ |
143
|
|
|
return $this->modify(static fn (string $subject): string => Helper::replaceAllWith($subject, $replace, $replacement)); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @inheritDoc |
148
|
|
|
*/ |
149
|
|
|
public function substr(int $start, int|null $length = null): static |
150
|
|
|
{ |
151
|
|
|
return $this->modify(static fn (string $subject): string => Helper::substr($subject, $start, $length)); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @inheritDoc |
156
|
|
|
*/ |
157
|
|
|
public function toTitleCase(): static |
158
|
|
|
{ |
159
|
|
|
return $this->modify(static fn (string $subject): string => StrCase::toTitleCase($subject)); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @inheritDoc |
164
|
|
|
*/ |
165
|
|
|
public function toLowerCase(): static |
166
|
|
|
{ |
167
|
|
|
return $this->modify(static fn (string $subject): string => StrCase::toLowerCase($subject)); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @inheritDoc |
172
|
|
|
*/ |
173
|
|
|
public function toUpperCase(): static |
174
|
|
|
{ |
175
|
|
|
return $this->modify(static fn (string $subject): string => StrCase::toUpperCase($subject)); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @inheritDoc |
180
|
|
|
*/ |
181
|
|
|
public function toCapitalized(string|null $delimiter = null): static |
182
|
|
|
{ |
183
|
|
|
return $this->modify(static fn (string $subject): string => StrCase::toCapitalized($subject, $delimiter)); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @inheritDoc |
188
|
|
|
*/ |
189
|
|
|
public function toCapitalizedWords(string|null $delimiter = null): static |
190
|
|
|
{ |
191
|
|
|
return $this->modify(static fn (string $subject): string => StrCase::toCapitalizedWords($subject, $delimiter)); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @inheritDoc |
196
|
|
|
*/ |
197
|
|
|
public function toSnakeCase(): static |
198
|
|
|
{ |
199
|
|
|
return $this->modify(static fn (string $subject): string => StrCase::toSnakeCase($subject)); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @inheritDoc |
204
|
|
|
*/ |
205
|
|
|
public function toSlug(): static |
206
|
|
|
{ |
207
|
|
|
return $this->modify(static fn (string $subject): string => StrCase::toSlug($subject)); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @inheritDoc |
212
|
|
|
*/ |
213
|
|
|
public function toStudlyCase(): static |
214
|
|
|
{ |
215
|
|
|
return $this->modify(static fn (string $subject): string => StrCase::toStudlyCase($subject)); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @inheritDoc |
220
|
|
|
*/ |
221
|
|
|
public function ucFirstLetter(): static |
222
|
|
|
{ |
223
|
|
|
return $this->modify(static fn (string $subject): string => StrCase::ucFirstLetter($subject)); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* @inheritDoc |
228
|
|
|
*/ |
229
|
|
|
public function isEmail(): bool |
230
|
|
|
{ |
231
|
|
|
return Helper::isEmail($this->subject); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* @inheritDoc |
236
|
|
|
*/ |
237
|
|
|
public function isAlphabetic(): bool |
238
|
|
|
{ |
239
|
|
|
return Helper::isAlphabetic($this->subject); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* @inheritDoc |
244
|
|
|
*/ |
245
|
|
|
public function isLowercase(): bool |
246
|
|
|
{ |
247
|
|
|
return Helper::isLowercase($this->subject); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* @inheritDoc |
252
|
|
|
*/ |
253
|
|
|
public function isUppercase(): bool |
254
|
|
|
{ |
255
|
|
|
return Helper::isUppercase($this->subject); |
256
|
|
|
} |
257
|
|
|
} |
258
|
|
|
|