-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStringable.php
More file actions
265 lines (216 loc) · 6.81 KB
/
Stringable.php
File metadata and controls
265 lines (216 loc) · 6.81 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
<?php
declare(strict_types=1);
/**
* You may not change or alter any portion of this comment or credits
* of supporting developers from this source code or any supporting source code
* which is considered copyrighted (c) material of the original comment or credit authors.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
/**
* @copyright 2000-2026 XOOPS Project (https://xoops.org/)
* @license GNU GPL 2.0 or later (https://www.gnu.org/licenses/gpl-2.0.html)
* @author XOOPS Development Team
*/
namespace Xoops\Helpers\Utility;
use Stringable as NativeStringable;
/**
* Fluent, chainable string manipulation wrapper.
*
* Wraps Str utility methods for chainable operations.
* Immutable — each operation returns a new Stringable instance.
*
* Usage:
* $slug = Stringable::of(' Hello World! ')
* ->trim()
* ->slug('-')
* ->toString(); // "hello-world"
*
* $masked = Stringable::of('user@example.com')
* ->mask('*', 2, 5)
* ->toString(); // "us*****xample.com"
*/
final class Stringable implements NativeStringable
{
private function __construct(
private readonly string $value,
) {}
/**
* Create a new Stringable from a string.
*/
public static function of(string $value): self
{
return new self($value);
}
// ── Transformations ────────────────────────────────────
public function trim(string $characters = " \t\n\r\0\x0B"): self
{
return new self(trim($this->value, $characters));
}
public function ltrim(string $characters = " \t\n\r\0\x0B"): self
{
return new self(ltrim($this->value, $characters));
}
public function rtrim(string $characters = " \t\n\r\0\x0B"): self
{
return new self(rtrim($this->value, $characters));
}
public function lower(): self
{
return new self(mb_strtolower($this->value, 'UTF-8'));
}
public function upper(): self
{
return new self(mb_strtoupper($this->value, 'UTF-8'));
}
public function ucfirst(): self
{
return new self(mb_strtoupper(mb_substr($this->value, 0, 1, 'UTF-8'), 'UTF-8') . mb_substr($this->value, 1, null, 'UTF-8'));
}
public function camel(): self
{
return new self(Str::camel($this->value));
}
public function snake(string $delimiter = '_'): self
{
return new self(Str::snake($this->value, $delimiter));
}
public function studly(): self
{
return new self(Str::studly($this->value));
}
public function kebab(): self
{
return new self(Str::kebab($this->value));
}
public function slug(string $separator = '-'): self
{
return new self(Str::slug($this->value, $separator));
}
public function limit(int $limit = 100, string $end = '...'): self
{
return new self(Str::limit($this->value, $limit, $end));
}
public function replace(string $search, string $replace): self
{
return new self(str_replace($search, $replace, $this->value));
}
public function replaceFirst(string $search, string $replace): self
{
return new self(Str::replaceFirst($search, $replace, $this->value));
}
public function replaceLast(string $search, string $replace): self
{
return new self(Str::replaceLast($search, $replace, $this->value));
}
public function between(string $from, string $to): self
{
return new self(Str::between($this->value, $from, $to));
}
public function mask(string $character, int $index, ?int $length = null): self
{
return new self(Str::mask($this->value, $character, $index, $length));
}
public function ascii(): self
{
return new self(Str::ascii($this->value));
}
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 substr(int $start, ?int $length = null): self
{
return new self(mb_substr($this->value, $start, $length, 'UTF-8'));
}
// ── Inspections ────────────────────────────────────────
/**
* @param string|array<int, string> $needles
*/
public function contains(string|array $needles, bool $ignoreCase = false): bool
{
return Str::contains($this->value, $needles, $ignoreCase);
}
/**
* @param string|array<int, string> $needles
*/
public function startsWith(string|array $needles): bool
{
return Str::startsWith($this->value, $needles);
}
/**
* @param string|array<int, string> $needles
*/
public function endsWith(string|array $needles): bool
{
return Str::endsWith($this->value, $needles);
}
public function length(): int
{
return Str::length($this->value);
}
public function isEmpty(): bool
{
return $this->value === '';
}
public function isNotEmpty(): bool
{
return $this->value !== '';
}
public function isEmail(): bool
{
return Str::isEmail($this->value);
}
public function isUrl(): bool
{
return Str::isUrl($this->value);
}
public function isJson(): bool
{
return Str::isJson($this->value);
}
// ── Conditional ────────────────────────────────────────
/**
* Apply a callback if the condition is truthy.
*/
public function when(mixed $condition, callable $callback, ?callable $default = null): self
{
if (Value::filled($condition)) {
return $callback($this);
}
if ($default !== null) {
return $default($this);
}
return $this;
}
/**
* Pipe this Stringable through a callback.
*/
public function pipe(callable $callback): self
{
return new self((string) $callback($this->value));
}
/**
* Apply a callback for side effects, return self unchanged.
*/
public function tap(callable $callback): self
{
$callback($this->value);
return $this;
}
// ── Output ─────────────────────────────────────────────
public function toString(): string
{
return $this->value;
}
public function __toString(): string
{
return $this->value;
}
}