1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Innmind\Immutable; |
5
|
|
|
|
6
|
|
|
use Innmind\Immutable\Exception\RegexException; |
7
|
|
|
use Innmind\Immutable\Exception\SubstringException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @deprecated To be removed in 2.0 |
11
|
|
|
*/ |
12
|
|
|
class StringPrimitive implements PrimitiveInterface, StringableInterface |
13
|
|
|
{ |
14
|
|
|
const PAD_RIGHT = STR_PAD_RIGHT; |
15
|
|
|
const PAD_LEFT = STR_PAD_LEFT; |
16
|
|
|
const PAD_BOTH = STR_PAD_BOTH; |
17
|
|
|
const PREG_NO_FLAGS = 0; |
18
|
|
|
const PREG_SPLIT_NO_EMPTY = PREG_SPLIT_NO_EMPTY; |
19
|
|
|
const PREG_SPLIT_DELIM_CAPTURE = PREG_SPLIT_DELIM_CAPTURE; |
20
|
|
|
const PREG_SPLIT_OFFSET_CAPTURE = PREG_SPLIT_OFFSET_CAPTURE; |
21
|
|
|
const PREG_OFFSET_CAPTURE = PREG_OFFSET_CAPTURE; |
22
|
|
|
|
23
|
|
|
private $value; |
24
|
|
|
|
25
|
|
|
public function __construct(string $value) |
26
|
|
|
{ |
27
|
|
|
$this->value = $value; |
28
|
|
|
|
29
|
|
|
@trigger_error( |
|
|
|
|
30
|
|
|
'Use Str class instead', |
31
|
|
|
E_USER_DEPRECATED |
32
|
|
|
); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* {@inheritdoc} |
37
|
|
|
*/ |
38
|
|
|
public function toPrimitive() |
39
|
|
|
{ |
40
|
|
|
return $this->value; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritdoc} |
45
|
|
|
*/ |
46
|
|
|
public function __toString(): string |
47
|
|
|
{ |
48
|
|
|
return $this->value; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Split the string into a collection of ones |
53
|
|
|
* |
54
|
|
|
* @param string $delimiter |
55
|
|
|
* |
56
|
|
|
* @return TypedCollectionInterface |
57
|
|
|
*/ |
58
|
|
View Code Duplication |
public function split(string $delimiter = null): TypedCollectionInterface |
|
|
|
|
59
|
|
|
{ |
60
|
|
|
$parts = empty($delimiter) ? |
61
|
|
|
str_split($this->value) : explode($delimiter, $this->value); |
62
|
|
|
$strings = []; |
63
|
|
|
|
64
|
|
|
foreach ($parts as $part) { |
65
|
|
|
$strings[] = new self($part); |
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return new TypedCollection( |
69
|
|
|
self::class, |
70
|
|
|
$strings |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Returns a collection of the string splitted by the given chunk size |
76
|
|
|
* |
77
|
|
|
* @param int $size |
78
|
|
|
* |
79
|
|
|
* @return TypedCollectionInterface |
80
|
|
|
*/ |
81
|
|
View Code Duplication |
public function chunk(int $size = 1): TypedCollectionInterface |
|
|
|
|
82
|
|
|
{ |
83
|
|
|
$pieces = str_split($this->value, $size); |
84
|
|
|
|
85
|
|
|
foreach ($pieces as &$piece) { |
86
|
|
|
$piece = new self($piece); |
|
|
|
|
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return new TypedCollection(self::class, $pieces); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Returns the position of the first occurence of the string |
94
|
|
|
* |
95
|
|
|
* @param string $needle |
96
|
|
|
* @param int $offset |
97
|
|
|
* |
98
|
|
|
* @throws SubstringException If the string is not found |
99
|
|
|
* |
100
|
|
|
* @return int |
101
|
|
|
*/ |
102
|
|
View Code Duplication |
public function pos(string $needle, int $offset = 0): int |
|
|
|
|
103
|
|
|
{ |
104
|
|
|
$position = mb_strpos($this->value, $needle, $offset); |
105
|
|
|
|
106
|
|
|
if ($position === false) { |
107
|
|
|
throw new SubstringException(sprintf( |
108
|
|
|
'Substring "%s" not found', |
109
|
|
|
$needle |
110
|
|
|
)); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return (int) $position; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Replace all occurences of the search string with the replacement one |
118
|
|
|
* |
119
|
|
|
* @param string $search |
120
|
|
|
* @param string $replacement |
121
|
|
|
* |
122
|
|
|
* @return self |
123
|
|
|
*/ |
124
|
|
|
public function replace(string $search, string $replacement): self |
125
|
|
|
{ |
126
|
|
|
return new self(str_replace( |
|
|
|
|
127
|
|
|
(string) $search, |
128
|
|
|
(string) $replacement, |
129
|
|
|
$this->value |
130
|
|
|
)); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Returns the string following the given delimiter |
135
|
|
|
* |
136
|
|
|
* @param string $delimiter |
137
|
|
|
* |
138
|
|
|
* @throws SubstringException If the string is not found |
139
|
|
|
* |
140
|
|
|
* @return self |
141
|
|
|
*/ |
142
|
|
View Code Duplication |
public function str(string $delimiter): self |
|
|
|
|
143
|
|
|
{ |
144
|
|
|
$sub = mb_strstr($this->value, $delimiter); |
145
|
|
|
|
146
|
|
|
if ($sub === false) { |
147
|
|
|
throw new SubstringException(sprintf( |
148
|
|
|
'Substring "%s" not found', |
149
|
|
|
$delimiter |
150
|
|
|
)); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return new self($sub); |
|
|
|
|
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Return the string in upper case |
158
|
|
|
* |
159
|
|
|
* @return self |
160
|
|
|
*/ |
161
|
|
|
public function toUpper(): self |
162
|
|
|
{ |
163
|
|
|
return new self(mb_strtoupper($this->value)); |
|
|
|
|
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Return the string in lower case |
168
|
|
|
* |
169
|
|
|
* @return self |
170
|
|
|
*/ |
171
|
|
|
public function toLower(): self |
172
|
|
|
{ |
173
|
|
|
return new self(mb_strtolower($this->value)); |
|
|
|
|
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Return the string length |
178
|
|
|
* |
179
|
|
|
* @return int |
180
|
|
|
*/ |
181
|
|
|
public function length(): int |
182
|
|
|
{ |
183
|
|
|
return strlen($this->value); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Reverse the string |
188
|
|
|
* |
189
|
|
|
* @return self |
190
|
|
|
*/ |
191
|
|
|
public function reverse(): self |
192
|
|
|
{ |
193
|
|
|
return new self(strrev($this->value)); |
|
|
|
|
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Pad the string |
198
|
|
|
* |
199
|
|
|
* @param int $length |
200
|
|
|
* @param string $character |
201
|
|
|
* @param int $direction |
202
|
|
|
* |
203
|
|
|
* @return self |
204
|
|
|
*/ |
205
|
|
|
public function pad(int $length, string $character = ' ', int $direction = self::PAD_RIGHT): self |
206
|
|
|
{ |
207
|
|
|
return new self(str_pad( |
|
|
|
|
208
|
|
|
$this->value, |
209
|
|
|
$length, |
210
|
|
|
$character, |
211
|
|
|
$direction |
212
|
|
|
)); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Pad to the right |
217
|
|
|
* |
218
|
|
|
* @param int $length |
219
|
|
|
* @param string $character |
220
|
|
|
* |
221
|
|
|
* @return self |
222
|
|
|
*/ |
223
|
|
|
public function rightPad(int $length, string $character = ' '): self |
224
|
|
|
{ |
225
|
|
|
return $this->pad($length, $character, self::PAD_RIGHT); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Pad to the left |
230
|
|
|
* |
231
|
|
|
* @param int $length |
232
|
|
|
* @param string $character |
233
|
|
|
* |
234
|
|
|
* @return self |
235
|
|
|
*/ |
236
|
|
|
public function leftPad(int $length, string $character = ' '): self |
237
|
|
|
{ |
238
|
|
|
return $this->pad($length, $character, self::PAD_LEFT); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Pad both sides |
243
|
|
|
* |
244
|
|
|
* @param int $length |
245
|
|
|
* @param string $character |
246
|
|
|
* |
247
|
|
|
* @return self |
248
|
|
|
*/ |
249
|
|
|
public function uniPad(int $length, string $character = ' '): self |
250
|
|
|
{ |
251
|
|
|
return $this->pad($length, $character, self::PAD_BOTH); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Find length of initial segment not matching mask |
256
|
|
|
* |
257
|
|
|
* @param string $mask |
258
|
|
|
* @param int $start |
259
|
|
|
* @param int $length |
260
|
|
|
* |
261
|
|
|
* @return int |
262
|
|
|
*/ |
263
|
|
View Code Duplication |
public function cspn(string $mask, int $start = 0, int $length = null): int |
|
|
|
|
264
|
|
|
{ |
265
|
|
|
if ($length === null) { |
266
|
|
|
$value = strcspn($this->value, $mask, $start); |
267
|
|
|
} else { |
268
|
|
|
$value = strcspn( |
269
|
|
|
$this->value, |
270
|
|
|
$mask, |
271
|
|
|
$start, |
272
|
|
|
$length |
273
|
|
|
); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
return (int) $value; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* Repeat the string n times |
281
|
|
|
* |
282
|
|
|
* @param int $repeat |
283
|
|
|
* |
284
|
|
|
* @return self |
285
|
|
|
*/ |
286
|
|
|
public function repeat(int $repeat): self |
287
|
|
|
{ |
288
|
|
|
return new self(str_repeat($this->value, $repeat)); |
|
|
|
|
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* Shuffle the string |
293
|
|
|
* |
294
|
|
|
* @return self |
295
|
|
|
*/ |
296
|
|
|
public function shuffle(): self |
297
|
|
|
{ |
298
|
|
|
return new self(str_shuffle($this->value)); |
|
|
|
|
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* Strip slashes |
303
|
|
|
* |
304
|
|
|
* @return self |
305
|
|
|
*/ |
306
|
|
|
public function stripSlashes(): self |
307
|
|
|
{ |
308
|
|
|
return new self(stripslashes($this->value)); |
|
|
|
|
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* Strip C-like slashes |
313
|
|
|
* |
314
|
|
|
* @return self |
315
|
|
|
*/ |
316
|
|
|
public function stripCSlashes(): self |
317
|
|
|
{ |
318
|
|
|
return new self(stripcslashes($this->value)); |
|
|
|
|
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* Return the word count |
323
|
|
|
* |
324
|
|
|
* @param string $charlist |
325
|
|
|
* |
326
|
|
|
* @return int |
327
|
|
|
*/ |
328
|
|
|
public function wordCount(string $charlist = ''): int |
329
|
|
|
{ |
330
|
|
|
return (int) str_word_count( |
331
|
|
|
$this->value, |
332
|
|
|
0, |
333
|
|
|
$charlist |
334
|
|
|
); |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
/** |
338
|
|
|
* Return the collection of words |
339
|
|
|
* |
340
|
|
|
* @param string $charlist |
341
|
|
|
* |
342
|
|
|
* @return TypedCollectionInterface |
343
|
|
|
*/ |
344
|
|
|
public function words(string $charlist = ''): TypedCollectionInterface |
345
|
|
|
{ |
346
|
|
|
$words = str_word_count($this->value, 2, $charlist); |
347
|
|
|
|
348
|
|
|
foreach ($words as &$word) { |
349
|
|
|
$word = new self($word); |
|
|
|
|
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
return new TypedCollection( |
353
|
|
|
self::class, |
354
|
|
|
$words |
355
|
|
|
); |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
/** |
359
|
|
|
* Split the string using a regular expression |
360
|
|
|
* |
361
|
|
|
* @param string $regex |
362
|
|
|
* @param int $limit |
363
|
|
|
* @param int $flags |
364
|
|
|
* |
365
|
|
|
* @return TypedCollectionInterface |
366
|
|
|
*/ |
367
|
|
View Code Duplication |
public function pregSplit(string $regex, int $limit = -1, int $flags = self::PREG_NO_FLAGS): TypedCollectionInterface |
|
|
|
|
368
|
|
|
{ |
369
|
|
|
$strings = preg_split($regex, $this->value, $limit, $flags); |
370
|
|
|
|
371
|
|
|
foreach ($strings as &$string) { |
372
|
|
|
$string = new self($string); |
|
|
|
|
373
|
|
|
} |
374
|
|
|
|
375
|
|
|
return new TypedCollection( |
376
|
|
|
self::class, |
377
|
|
|
$strings |
378
|
|
|
); |
379
|
|
|
} |
380
|
|
|
|
381
|
|
|
/** |
382
|
|
|
* Check if the string match the given regular expression |
383
|
|
|
* |
384
|
|
|
* @param string $regex |
385
|
|
|
* @param int $offset |
386
|
|
|
* |
387
|
|
|
* @throws Exception If the regex failed |
388
|
|
|
* |
389
|
|
|
* @return bool |
390
|
|
|
*/ |
391
|
|
View Code Duplication |
public function match(string $regex, int $offset = 0): bool |
|
|
|
|
392
|
|
|
{ |
393
|
|
|
$matches = []; |
394
|
|
|
$value = preg_match($regex, $this->value, $matches, 0, $offset); |
395
|
|
|
|
396
|
|
|
if ($value === false) { |
397
|
|
|
throw new RegexException('', preg_last_error()); |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
return (bool) $value; |
401
|
|
|
} |
402
|
|
|
|
403
|
|
|
/** |
404
|
|
|
* Return a collection of the elements matching the regex |
405
|
|
|
* |
406
|
|
|
* @param string $regex |
407
|
|
|
* @param int $offset |
408
|
|
|
* @param int $flags |
409
|
|
|
* |
410
|
|
|
* @throws Exception If the regex failed |
411
|
|
|
* |
412
|
|
|
* @return TypedCollectionInterface |
413
|
|
|
*/ |
414
|
|
|
public function getMatches(string $regex, int $offset = 0, int $flags = self::PREG_NO_FLAGS): TypedCollectionInterface |
415
|
|
|
{ |
416
|
|
|
$matches = []; |
417
|
|
|
$value = preg_match( |
418
|
|
|
$regex, |
419
|
|
|
$this->value, |
420
|
|
|
$matches, |
421
|
|
|
$flags, |
422
|
|
|
$offset |
423
|
|
|
); |
424
|
|
|
|
425
|
|
|
foreach ($matches as &$match) { |
426
|
|
|
$match = new self($match); |
|
|
|
|
427
|
|
|
} |
428
|
|
|
|
429
|
|
|
if ($value === false) { |
430
|
|
|
throw new RegexException('', preg_last_error()); |
431
|
|
|
} |
432
|
|
|
|
433
|
|
|
return new TypedCollection(self::class, $matches); |
434
|
|
|
} |
435
|
|
|
|
436
|
|
|
/** |
437
|
|
|
* Replace part of the string by using a regular expression |
438
|
|
|
* |
439
|
|
|
* @param string $regex |
440
|
|
|
* @param string $replacement |
441
|
|
|
* @param int $limit |
442
|
|
|
* |
443
|
|
|
* @throws Exception If the regex failed |
444
|
|
|
* |
445
|
|
|
* @return self |
446
|
|
|
*/ |
447
|
|
View Code Duplication |
public function pregReplace(string $regex, string $replacement, int $limit = -1): self |
|
|
|
|
448
|
|
|
{ |
449
|
|
|
$value = preg_replace( |
450
|
|
|
$regex, |
451
|
|
|
$replacement, |
452
|
|
|
$this->value, |
453
|
|
|
$limit |
454
|
|
|
); |
455
|
|
|
|
456
|
|
|
if ($value === null) { |
457
|
|
|
throw new RegexException('', preg_last_error()); |
458
|
|
|
} |
459
|
|
|
|
460
|
|
|
return new self($value); |
|
|
|
|
461
|
|
|
} |
462
|
|
|
|
463
|
|
|
/** |
464
|
|
|
* Return part of the string |
465
|
|
|
* |
466
|
|
|
* @param int $start |
467
|
|
|
* @param int $length |
468
|
|
|
* |
469
|
|
|
* @return self |
470
|
|
|
*/ |
471
|
|
View Code Duplication |
public function substring(int $start, int $length = null): self |
|
|
|
|
472
|
|
|
{ |
473
|
|
|
if ($length === null) { |
474
|
|
|
$sub = substr($this->value, $start); |
475
|
|
|
} else { |
476
|
|
|
$sub = substr($this->value, $start, $length); |
477
|
|
|
} |
478
|
|
|
|
479
|
|
|
return new self($sub); |
|
|
|
|
480
|
|
|
} |
481
|
|
|
|
482
|
|
|
/** |
483
|
|
|
* Return a formatted string |
484
|
|
|
* |
485
|
|
|
* @return self |
486
|
|
|
*/ |
487
|
|
|
public function sprintf(): self |
488
|
|
|
{ |
489
|
|
|
$params = func_get_args(); |
490
|
|
|
array_unshift($params, $this->value); |
491
|
|
|
$formatted = call_user_func_array('sprintf', $params); |
492
|
|
|
|
493
|
|
|
return new self($formatted); |
|
|
|
|
494
|
|
|
} |
495
|
|
|
|
496
|
|
|
/** |
497
|
|
|
* Return the string with the first letter as uppercase |
498
|
|
|
* |
499
|
|
|
* @return self |
500
|
|
|
*/ |
501
|
|
|
public function ucfirst(): self |
502
|
|
|
{ |
503
|
|
|
return new self(ucfirst($this->value)); |
|
|
|
|
504
|
|
|
} |
505
|
|
|
|
506
|
|
|
/** |
507
|
|
|
* Return the string with the first letter as lowercase |
508
|
|
|
* |
509
|
|
|
* @return self |
510
|
|
|
*/ |
511
|
|
|
public function lcfirst(): self |
512
|
|
|
{ |
513
|
|
|
return new self(lcfirst($this->value)); |
|
|
|
|
514
|
|
|
} |
515
|
|
|
|
516
|
|
|
/** |
517
|
|
|
* Return a CamelCase representation of the string |
518
|
|
|
* |
519
|
|
|
* @return self |
520
|
|
|
*/ |
521
|
|
|
public function camelize(): self |
522
|
|
|
{ |
523
|
|
|
return new self( |
|
|
|
|
524
|
|
|
$this |
525
|
|
|
->pregSplit('/_| /') |
526
|
|
|
->map(function(self $part) { |
527
|
|
|
return $part->ucfirst(); |
528
|
|
|
}) |
529
|
|
|
->join('') |
530
|
|
|
); |
531
|
|
|
} |
532
|
|
|
|
533
|
|
|
/** |
534
|
|
|
* Append a string at the end of the current one |
535
|
|
|
* |
536
|
|
|
* @param string $string |
537
|
|
|
* |
538
|
|
|
* @return self |
539
|
|
|
*/ |
540
|
|
|
public function append(string $string): self |
541
|
|
|
{ |
542
|
|
|
return new self((string) $this.$string); |
|
|
|
|
543
|
|
|
} |
544
|
|
|
|
545
|
|
|
/** |
546
|
|
|
* Prepend a string at the beginning of the current one |
547
|
|
|
* |
548
|
|
|
* @param string $string |
549
|
|
|
* |
550
|
|
|
* @return self |
551
|
|
|
*/ |
552
|
|
|
public function prepend(string $string): self |
553
|
|
|
{ |
554
|
|
|
return new self($string.(string) $this); |
|
|
|
|
555
|
|
|
} |
556
|
|
|
|
557
|
|
|
/** |
558
|
|
|
* Check if the 2 strings are equal |
559
|
|
|
* |
560
|
|
|
* @param self $string |
561
|
|
|
* |
562
|
|
|
* @return bool |
563
|
|
|
*/ |
564
|
|
|
public function equals(self $string): bool |
565
|
|
|
{ |
566
|
|
|
return (string) $this === (string) $string; |
567
|
|
|
} |
568
|
|
|
|
569
|
|
|
/** |
570
|
|
|
* Trim the string |
571
|
|
|
* |
572
|
|
|
* @param string $mask |
573
|
|
|
* |
574
|
|
|
* @return self |
575
|
|
|
*/ |
576
|
|
|
public function trim(string $mask = null): self |
577
|
|
|
{ |
578
|
|
|
return new self($mask === null ? trim((string) $this) : trim((string) $this, $mask)); |
|
|
|
|
579
|
|
|
} |
580
|
|
|
|
581
|
|
|
/** |
582
|
|
|
* Trim the right side of the string |
583
|
|
|
* |
584
|
|
|
* @param string $mask |
585
|
|
|
* |
586
|
|
|
* @return self |
587
|
|
|
*/ |
588
|
|
|
public function rightTrim(string $mask = null): self |
589
|
|
|
{ |
590
|
|
|
return new self($mask === null ? rtrim((string) $this) : rtrim((string) $this, $mask)); |
|
|
|
|
591
|
|
|
} |
592
|
|
|
|
593
|
|
|
/** |
594
|
|
|
* Trim the left side of the string |
595
|
|
|
* |
596
|
|
|
* @param string $mask |
597
|
|
|
* |
598
|
|
|
* @return self |
599
|
|
|
*/ |
600
|
|
|
public function leftTrim(string $mask = null): self |
601
|
|
|
{ |
602
|
|
|
return new self($mask === null ? ltrim((string) $this) : ltrim((string) $this, $mask)); |
|
|
|
|
603
|
|
|
} |
604
|
|
|
} |
605
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: