-
Notifications
You must be signed in to change notification settings - Fork 0
/
Text.php
508 lines (450 loc) · 13.6 KB
/
Text.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
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
<?php declare(strict_types=1);
/**
* This file is part of the Phootwork package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
* @copyright Thomas Gossmann
*/
namespace phootwork\lang;
use phootwork\lang\parts\ArrayConversionsPart;
use phootwork\lang\parts\CheckerPart;
use phootwork\lang\parts\ComparisonPart;
use phootwork\lang\parts\InternalPart;
use phootwork\lang\parts\SearchPart;
use phootwork\lang\parts\TransformationsPart;
use Stringable;
/**
* Object representation of an immutable String
*
* @author gossi
*/
class Text implements Comparable, Stringable {
use ArrayConversionsPart;
use CheckerPart;
use ComparisonPart;
use SearchPart;
use InternalPart;
use TransformationsPart;
/** @var string */
private string $string;
/** @var string */
private string $encoding;
/**
* Initializes a String object ad assigns both string and encoding properties
* the supplied values. $string is cast to a string prior to assignment, and if
* $encoding is not specified, it defaults to mb_internal_encoding(). Throws
* an InvalidArgumentException if the first argument is an array or object
* without a __toString method.
*
* @param string|Stringable $string Value to modify, after being cast to string
* @param string|null $encoding The character encoding
*
* @psalm-suppress PossiblyInvalidPropertyAssignmentValue mb_internal_encoding always return string when called as getter
*/
public function __construct(string|Stringable $string = '', ?string $encoding = null) {
$this->string = (string) $string;
$this->encoding = $encoding ?? mb_internal_encoding();
}
/**
* Static initializing a String object.
*
* @param string|Stringable $string
* @param string|null $encoding
*
* @return static
*
* @see Text::__construct()
*
* @psalm-suppress UnsafeInstantiation
*/
public static function create(string|Stringable $string, ?string $encoding = null): static {
return new static($string, $encoding);
}
/**
* Returns the used encoding
*
* @return string
*/
public function getEncoding(): string {
return $this->encoding;
}
/**
* Get string length
*
* <code>
* $str = new Text('Hello World!');<br>
* $str->length(); // 12
*
* $str = new Text('いちりんしゃ');<br>
* $str->length(); // 6
* </code>
*
* @return int Returns the length
*/
public function length(): int {
return mb_strlen($this->string, $this->encoding);
}
/**
* Appends <code>$string</code> and returns as a new <code>Text</code>
*
* @param string|Stringable $string
*
* @return Text
*/
public function append(string|Stringable $string): self {
return new self($this->string . $string, $this->encoding);
}
/**
* Prepends <code>$string</code> and returns as a new <code>Text</code>
*
* @param string|Stringable $string $string
*
* @return Text
*/
public function prepend(string|Stringable $string): self {
return new self($string . $this->string, $this->encoding);
}
/**
* Inserts a substring at the given index
*
* <code>
* $str = new Text('Hello World!');<br>
* $str->insert('to this ', 5); // Hello to this World!
* </code>
*
* @param string|Stringable $substring
* @param int $index
*
* @return Text
*/
public function insert(string|Stringable $substring, int $index): self {
if ($index <= 0) {
return $this->prepend($substring);
}
if ($index > $this->length()) {
return $this->append($substring);
}
$start = mb_substr($this->string, 0, $index, $this->encoding);
$end = mb_substr($this->string, $index, $this->length(), $this->encoding);
return new self($start . $substring . $end);
}
//
//
// SLICING AND SUBSTRING
//
//
/**
* Slices a piece of the string from a given offset with a specified length.
* If no length is given, the String is sliced to its maximum length.
*
* @see #substring
*
* @param int $offset
* @param int|null $length
*
* @return Text
*/
public function slice(int $offset, ?int $length = null): self {
$offset = $this->prepareOffset($offset);
$length = $this->prepareLength($offset, $length);
return new self(mb_substr($this->string, $offset, $length, $this->encoding), $this->encoding);
}
/**
* Slices a piece of the string from a given start to an end.
* If no length is given, the String is sliced to its maximum length.
*
* @see #slice
*
* @param int $start
* @param int|null $end
*
* @return Text
*/
public function substring(int $start, ?int $end = null): self {
$length = $this->length();
if (null === $end) {
$end = $length;
}
if ($end < 0) {
$end = $length + $end;
}
$end = min($end, $length);
$start = min($start, $end);
$end = max($start, $end);
$end = $end - $start;
return new self(mb_substr($this->string, $start, $end, $this->encoding), $this->encoding);
}
/**
* Count the number of substring occurrences.
*
* @param string|Stringable $substring The substring to count the occurrencies
* @param bool $caseSensitive Force case-sensitivity
*
* @return int
*/
public function countSubstring(string|Stringable $substring, bool $caseSensitive = true): int {
if (empty($substring)) {
throw new \InvalidArgumentException('$substring cannot be empty');
}
if ($caseSensitive) {
return mb_substr_count($this->string, (string) $substring, $this->encoding);
}
$str = mb_strtoupper($this->string, $this->encoding);
$substring = mb_strtoupper((string) $substring, $this->encoding);
return mb_substr_count($str, $substring, $this->encoding);
}
//
//
// REPLACING
//
//
/**
* Replace all occurrences of the search string with the replacement string
*
* @see #supplant
*
* @param Arrayable|Stringable|array|string $search
* The value being searched for, otherwise known as the needle. An array may be used
* to designate multiple needles.
* @param Arrayable|Stringable[]|array|string $replace
* The replacement value that replaces found search values. An array may be used to
* designate multiple replacements.
*
* @return Text
*
* @psalm-suppress MixedArgumentTypeCoercion
*/
public function replace(Arrayable|Stringable|array|string $search, Arrayable|Stringable|array|string $replace): self {
$search = $search instanceof Stringable ? (string) $search :
($search instanceof Arrayable ? $search->toArray() : $search);
$replace = $replace instanceof Stringable ? (string) $replace :
($replace instanceof Arrayable ? $replace->toArray() : $replace);
return new self(str_replace($search, $replace, $this->string), $this->encoding);
}
/**
* Replaces all occurrences of given replacement map. Keys will be replaced with its values.
*
* @param string[] $map the replacements. Keys will be replaced with its value.
*
* @return Text
*/
public function supplant(array $map): self {
return new self(str_replace(array_keys($map), array_values($map), $this->string), $this->encoding);
}
/**
* Replace text within a portion of a string.
*
* @param string|Stringable $replacement
* @param int $offset
* @param int|null $length
*
* @return Text
*/
public function splice(string|Stringable $replacement, int $offset, ?int $length = null): self {
$offset = $this->prepareOffset($offset);
$length = $this->prepareLength($offset, $length);
$start = $this->substring(0, $offset);
$end = $this->substring($offset + $length);
return new self($start . $replacement . $end);
}
//
//
// STRING OPERATIONS
//
//
/**
* Strip whitespace (or other characters) from the beginning and end of the string
*
* @param string|Stringable $characters
* Optionally, the stripped characters can also be specified using the mask parameter.
* Simply list all characters that you want to be stripped. With .. you can specify a
* range of characters.
*
* @return Text
*/
public function trim(string|Stringable $characters = " \t\n\r\v\0"): self {
return new self(trim($this->string, (string) $characters), $this->encoding);
}
/**
* Strip whitespace (or other characters) from the beginning of the string
*
* @param string|Stringable $characters
* Optionally, the stripped characters can also be specified using the mask parameter.
* Simply list all characters that you want to be stripped. With .. you can specify a
* range of characters.
*
* @return Text
*/
public function trimStart(string|Stringable $characters = " \t\n\r\v\0"): self {
return new self(ltrim($this->string, (string) $characters), $this->encoding);
}
/**
* Strip whitespace (or other characters) from the end of the string
*
* @param string|Stringable $characters
* Optionally, the stripped characters can also be specified using the mask parameter.
* Simply list all characters that you want to be stripped. With .. you can specify a
* range of characters.
*
* @return Text
*/
public function trimEnd(string|Stringable $characters = " \t\n\r\v\0"): self {
return new self(rtrim($this->string, (string) $characters), $this->encoding);
}
/**
* Adds padding to the start and end
*
* @param int $length
* @param string|Stringable $padding
*
* @return Text
*/
public function pad(int $length, string|Stringable $padding = ' '): self {
$len = $length - $this->length();
return $this->applyPadding(floor($len / 2), ceil($len / 2), $padding);
}
/**
* Adds padding to the start
*
* @param int $length
* @param string|Stringable $padding
*
* @return Text
*/
public function padStart(int $length, string|Stringable $padding = ' ') {
return $this->applyPadding($length - $this->length(), 0, $padding);
}
/**
* Adds padding to the end
*
* @param int $length
* @param string|Stringable $padding
*
* @return Text
*/
public function padEnd(int $length, string|Stringable $padding = ' '): self {
return $this->applyPadding(0, $length - $this->length(), $padding);
}
/**
* Adds the specified amount of left and right padding to the given string.
* The default character used is a space.
*
* @see https://github.com/danielstjules/Stringy/blob/master/src/Stringy.php
*
* @param int|float $left Length of left padding
* @param int|float $right Length of right padding
* @param string|Stringable $padStr String used to pad
*
* @return Text the padded string
*/
protected function applyPadding(int|float $left = 0, int|float $right = 0, string|Stringable $padStr = ' '): self {
$length = mb_strlen((string) $padStr, $this->encoding);
$strLength = $this->length();
$paddedLength = $strLength + $left + $right;
if (!$length || $paddedLength <= $strLength) {
return $this;
}
$leftPadding = mb_substr(str_repeat((string) $padStr, (int) ceil($left / $length)), 0, (int) $left, $this->encoding);
$rightPadding = mb_substr(str_repeat((string) $padStr, (int) ceil($right / $length)), 0, (int) $right, $this->encoding);
return new self($leftPadding . $this->string . $rightPadding);
}
/**
* Ensures a given substring at the start of the string
*
* @param string $substring
*
* @return Text
*/
public function ensureStart(string $substring): self {
if (!$this->startsWith($substring)) {
return $this->prepend($substring);
}
return $this;
}
/**
* Ensures a given substring at the end of the string
*
* @param string $substring
*
* @return Text
*/
public function ensureEnd(string $substring): self {
if (!$this->endsWith($substring)) {
return $this->append($substring);
}
return $this;
}
/**
* Returns a copy of the string wrapped at a given number of characters
*
* @param int $width The number of characters at which the string will be wrapped.
* @param string $break The line is broken using the optional break parameter.
* @param bool $cut
* If the cut is set to TRUE, the string is always wrapped at or before the specified
* width. So if you have a word that is larger than the given width, it is broken apart.
*
* @return Text Returns the string wrapped at the specified length.
*/
public function wrapWords(int $width = 75, string $break = "\n", bool $cut = false): self {
return new self(wordwrap($this->string, $width, $break, $cut), $this->encoding);
}
/**
* Repeat the string $times times. If $times is 0, it returns ''.
*
* @param int $multiplier
*
* @throws \InvalidArgumentException If $times is negative.
*
* @return Text
*/
public function repeat(int $multiplier): self {
return new self(str_repeat($this->string, $multiplier), $this->encoding);
}
/**
* Reverses the character order
*
* @return Text
*/
public function reverse(): self {
return new self(strrev($this->string), $this->encoding);
}
/**
* Truncates the string with a substring and ensures it doesn't exceed the given length
*
* @param int $length
* @param string $substring
*
* @return Text
*/
public function truncate(int $length, string $substring = ''): self {
if ($this->length() <= $length) {
return new self($this->string, $this->encoding);
}
$substrLen = mb_strlen($substring, $this->encoding);
if ($this->length() + $substrLen > $length) {
$length -= $substrLen;
}
return $this->substring(0, $length)->append($substring);
}
/**
* Returns the native string
*
* @return string
*/
public function toString(): string {
return $this->string;
}
protected function getString(): string {
return $this->toString();
}
//
//
// MAGIC HAPPENS HERE
//
//
public function __toString(): string {
return $this->string;
}
}