1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Composable strings functions. |
5
|
|
|
* |
6
|
|
|
* This file is part of PinkCrab Function Constructors. |
7
|
|
|
* |
8
|
|
|
* PinkCrab Function Constructors is free software: you can redistribute it and/or modify it under the terms of the |
9
|
|
|
* GNU General Public License as published by the Free Software Foundation, either version 2 |
10
|
|
|
* of the License, or (at your option) any later version. |
11
|
|
|
* |
12
|
|
|
* PinkCrab Function Constructors is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without |
13
|
|
|
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
14
|
|
|
* See the GNU General Public License for more details. |
15
|
|
|
* |
16
|
|
|
* You should have received a copy of the GNU General Public License along with PinkCrab Function Constructors. |
17
|
|
|
* If not, see <https://www.gnu.org/licenses/>. |
18
|
|
|
* |
19
|
|
|
* @author Glynn Quelch <[email protected]> |
20
|
|
|
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html |
21
|
|
|
* @package PinkCrab\FunctionConstructors |
22
|
|
|
* @since 0.0.1 |
23
|
|
|
* |
24
|
|
|
* @template Number of int|float |
25
|
|
|
* @phpstan-template Number of int|float |
26
|
|
|
* @psalm-template Number of int|float |
27
|
|
|
*/ |
28
|
|
|
|
29
|
|
|
declare(strict_types=1); |
30
|
|
|
|
31
|
|
|
namespace PinkCrab\FunctionConstructors\Strings; |
32
|
|
|
|
33
|
|
|
use Closure; |
34
|
|
|
use PinkCrab\FunctionConstructors\Comparisons as C; |
|
|
|
|
35
|
|
|
use PinkCrab\FunctionConstructors\GeneralFunctions as F; |
|
|
|
|
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Creates a callable for wrapping a string. |
39
|
|
|
* By defaults uses opening as closing, if no closing defined. |
40
|
|
|
* |
41
|
|
|
* @param string $opening |
42
|
|
|
* @param string|null $closing |
43
|
|
|
* @return Closure(string):string |
44
|
|
|
*/ |
45
|
|
|
function wrap(string $opening, ?string $closing = null): Closure |
46
|
|
|
{ |
47
|
|
|
/** |
48
|
|
|
* @param string $string |
49
|
|
|
* @return string |
50
|
|
|
*/ |
51
|
|
|
return function (string $string) use ($opening, $closing): string { |
52
|
|
|
return \sprintf('%s%s%s', $opening, $string, $closing ?? $opening); |
53
|
|
|
}; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Creates a callable for slicing a string |
58
|
|
|
* |
59
|
|
|
* Uses substr() |
60
|
|
|
* |
61
|
|
|
* @param int $start start position |
62
|
|
|
* @param int|null $finish end position |
63
|
|
|
* @return Closure(string):string |
64
|
|
|
*/ |
65
|
|
|
function slice(int $start, ?int $finish = null): Closure |
66
|
|
|
{ |
67
|
|
|
/** |
68
|
|
|
* @param string $string |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
|
|
return function (string $string) use ($start, $finish): string { |
72
|
|
|
return ! $finish |
73
|
|
|
? substr($string, $start) |
74
|
|
|
: substr($string, $start, $finish); |
75
|
|
|
}; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Creates a callable for prepending to a string. |
80
|
|
|
* |
81
|
|
|
* @param string $prepend |
82
|
|
|
* @return Closure(string):string |
83
|
|
|
*/ |
84
|
|
|
function prepend(string $prepend): Closure |
85
|
|
|
{ |
86
|
|
|
/** |
87
|
|
|
* @param string $string |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
|
|
return function (string $string) use ($prepend): string { |
91
|
|
|
return \sprintf('%s%s', $prepend, $string); |
92
|
|
|
}; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Creates a callable for appending to a string. |
97
|
|
|
* |
98
|
|
|
* @param string $append |
99
|
|
|
* @return Closure(string):string |
100
|
|
|
*/ |
101
|
|
|
function append(string $append): Closure |
102
|
|
|
{ |
103
|
|
|
/** |
104
|
|
|
* @param string $string |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
|
|
return function (string $string) use ($append): string { |
108
|
|
|
return \sprintf('%s%s', $string, $append); |
109
|
|
|
}; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Returns a callable for formatting a string with a defined set of rules |
114
|
|
|
* |
115
|
|
|
* @param string $template |
116
|
|
|
* @return Closure(array<string, mixed>):string |
117
|
|
|
*/ |
118
|
|
|
function vSprintf(string $template): Closure |
119
|
|
|
{ |
120
|
|
|
/** |
121
|
|
|
* @param array<string, mixed> $values |
122
|
|
|
* @return string Will return original string if false. |
123
|
|
|
*/ |
124
|
|
|
return function (array $values = array()) use ($template): string { |
125
|
|
|
return \vsprintf($template, $values); |
126
|
|
|
}; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Creates a double curried find to replace. |
131
|
|
|
* |
132
|
|
|
* @param string $find Value to look for |
133
|
|
|
* @return Closure(string):Closure |
134
|
|
|
*/ |
135
|
|
|
function findToReplace(string $find): Closure |
136
|
|
|
{ |
137
|
|
|
/** |
138
|
|
|
* @param string $replace value to replace with |
139
|
|
|
* @return Closure(string):string |
140
|
|
|
*/ |
141
|
|
|
return function (string $replace) use ($find): Closure { |
142
|
|
|
/** |
143
|
|
|
* @param string $subject String to carry out find and replace. |
144
|
|
|
* @return string |
145
|
|
|
*/ |
146
|
|
|
return function ($subject) use ($find, $replace): string { |
147
|
|
|
return \str_replace($find, $replace, $subject); |
148
|
|
|
}; |
149
|
|
|
}; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Creates a Closure to find and replace within a string. |
154
|
|
|
* |
155
|
|
|
* @param string $find |
156
|
|
|
* @param string $replace |
157
|
|
|
* @return Closure(string):string |
158
|
|
|
*/ |
159
|
|
|
function replaceWith(string $find, string $replace): Closure |
160
|
|
|
{ |
161
|
|
|
/** |
162
|
|
|
* @param string $source |
163
|
|
|
* @return string |
164
|
|
|
*/ |
165
|
|
|
return function ($source) use ($find, $replace): string { |
166
|
|
|
return \str_replace($find, $replace, $source); |
167
|
|
|
}; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Returns a callable that can replace a sub string with a pre defined value. |
172
|
|
|
* |
173
|
|
|
* @param string $replace The value to replace in passed string |
174
|
|
|
* @param int $offset The offset to start, negative numbers count back from end. |
175
|
|
|
* @param int|null $length Number of chars to stop replacing at end of replacement. |
176
|
|
|
* @return Closure(string):string |
177
|
|
|
*/ |
178
|
|
|
function replaceSubString(string $replace, int $offset = 0, ?int $length = null): Closure |
179
|
|
|
{ |
180
|
|
|
/** |
181
|
|
|
* @param string $string |
182
|
|
|
* @return string |
183
|
|
|
*/ |
184
|
|
|
return function (string $string) use ($replace, $offset, $length): string { |
185
|
|
|
return $length |
|
|
|
|
186
|
|
|
? \substr_replace($string, $replace, $offset, $length) |
187
|
|
|
: \substr_replace($string, $replace, $offset); |
188
|
|
|
}; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Creates a callable for checking if a string starts with |
193
|
|
|
* |
194
|
|
|
* @param string $find The value to look for. |
195
|
|
|
* @return Closure(string):bool |
196
|
|
|
*/ |
197
|
|
|
function startsWith(string $find): Closure |
198
|
|
|
{ |
199
|
|
|
/** |
200
|
|
|
* @param string $source |
201
|
|
|
* @return bool |
202
|
|
|
*/ |
203
|
|
|
return function (string $source) use ($find): bool { |
204
|
|
|
return (\substr($source, 0, \strlen($find)) === $find); |
205
|
|
|
}; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Creates a callable for checkin if a string ends with |
210
|
|
|
* |
211
|
|
|
* @param string $find The value to look for. |
212
|
|
|
* @return Closure(string):bool |
213
|
|
|
*/ |
214
|
|
|
function endsWith(string $find): Closure |
215
|
|
|
{ |
216
|
|
|
/** |
217
|
|
|
* @param string $source |
218
|
|
|
* @return bool |
219
|
|
|
*/ |
220
|
|
|
return function (string $source) use ($find): bool { |
221
|
|
|
if (\strlen($find) === 0) { |
222
|
|
|
return true; |
223
|
|
|
} |
224
|
|
|
return (\substr($source, -\strlen($find)) === $find); |
225
|
|
|
}; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Creates a callable for checking if a string contains. using stringContains |
230
|
|
|
* |
231
|
|
|
* @param string $needle The value to look for. |
232
|
|
|
* @return Closure(string):bool |
233
|
|
|
*/ |
234
|
|
|
function contains(string $needle): Closure |
235
|
|
|
{ |
236
|
|
|
/** |
237
|
|
|
* @param string $haystack String to look in. |
238
|
|
|
* @return bool |
239
|
|
|
*/ |
240
|
|
|
return function (string $haystack) use ($needle): bool { |
241
|
|
|
return \stringContains($haystack, $needle); |
242
|
|
|
}; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Creates a callable for checking if a string contains using preg_match. |
247
|
|
|
* |
248
|
|
|
* @param string $pattern |
249
|
|
|
* @return Closure(string):bool |
250
|
|
|
*/ |
251
|
|
|
function containsPattern(string $pattern): Closure |
252
|
|
|
{ |
253
|
|
|
/** |
254
|
|
|
* @param string $source String to look in. |
255
|
|
|
* @return bool |
256
|
|
|
*/ |
257
|
|
|
return function (string $source) use ($pattern): bool { |
258
|
|
|
return (bool) \preg_match($pattern, $source); |
259
|
|
|
}; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* Splits a string with a pattern |
264
|
|
|
* |
265
|
|
|
* @param string $pattern |
266
|
|
|
* @return Closure(string):?string[] |
267
|
|
|
*/ |
268
|
|
|
function splitPattern(string $pattern): Closure |
269
|
|
|
{ |
270
|
|
|
/** |
271
|
|
|
* @param string $name |
272
|
|
|
* @return string[] |
273
|
|
|
*/ |
274
|
|
|
return function (string $string) use ($pattern): ?array { |
275
|
|
|
return \preg_split($pattern, $string) ?: null; |
276
|
|
|
}; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* Converts a number (loose type) to a string representation of a float. |
281
|
|
|
* |
282
|
|
|
* @param string|int|float $precision Number of decimal places |
283
|
|
|
* @param string $point The decimal separator |
284
|
|
|
* @param string $thousands The thousand separator. |
285
|
|
|
* @return Closure(string|int|float):string |
286
|
|
|
*/ |
287
|
|
|
function digit($precision = 2, $point = '.', $thousands = ''): Closure |
288
|
|
|
{ |
289
|
|
|
/** |
290
|
|
|
* @param string|int|float $number |
291
|
|
|
* @return string |
292
|
|
|
*/ |
293
|
|
|
return function ($number) use ($precision, $point, $thousands): string { |
294
|
|
|
return \is_numeric($number) |
295
|
|
|
? \number_format((float) $number, (int) $precision, $point, $thousands) |
296
|
|
|
: ''; |
297
|
|
|
}; |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
|
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* Returns a callable for adding C slashes to a string based on a defined char list. |
304
|
|
|
* |
305
|
|
|
* @param string $charList The Char list to add slashes too. |
306
|
|
|
* @return Closure(string):string |
307
|
|
|
*/ |
308
|
|
|
function addSlashes(string $charList): Closure |
309
|
|
|
{ |
310
|
|
|
/** |
311
|
|
|
* @param string $string The string to have char, slash escaped. |
312
|
|
|
* @return string |
313
|
|
|
*/ |
314
|
|
|
return function (string $string) use ($charList): string { |
315
|
|
|
return \addcslashes($string, $charList); |
316
|
|
|
}; |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* Returns a callable for splitting a string by a set amount. |
321
|
|
|
* |
322
|
|
|
* @param non-empty-string $separator The char to split by. |
323
|
|
|
* @param int $limit The number of groups to split into. |
324
|
|
|
* @return Closure(string):array<string> The parts. |
325
|
|
|
*/ |
326
|
|
|
function split(string $separator, int $limit = PHP_INT_MAX): Closure |
327
|
|
|
{ |
328
|
|
|
/** |
329
|
|
|
* @param string $string The string to be split |
330
|
|
|
* @return array<int, string> |
331
|
|
|
*/ |
332
|
|
|
return function (string $string) use ($separator, $limit): array { |
333
|
|
|
$chunks = explode($separator, $string, $limit); |
334
|
|
|
return is_array($chunks) ? $chunks : array(); // @phpstan-ignore-line, inconsistent errors with differing php versions. |
335
|
|
|
}; |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
/** |
339
|
|
|
* Returns a callable for splitting a string by a set amount. |
340
|
|
|
* |
341
|
|
|
* @param int $length The length to split the string up with. |
342
|
|
|
* @return Closure(string):array<string> The parts. |
343
|
|
|
*/ |
344
|
|
|
function splitByLength(int $length): Closure |
345
|
|
|
{ |
346
|
|
|
/** |
347
|
|
|
* @param string $string The string to be split |
348
|
|
|
* @return array<int, string> |
349
|
|
|
*/ |
350
|
|
|
return function (string $string) use ($length): array { |
351
|
|
|
return \str_split($string, max(1, $length)) ?: array(); // @phpstan-ignore-line, inconsistent errors with differing php versions. |
|
|
|
|
352
|
|
|
}; |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
|
356
|
|
|
/** |
357
|
|
|
* Returns a callback for splitting a string into chunks. |
358
|
|
|
* |
359
|
|
|
* @param int $length The length of each chunk. |
360
|
|
|
* @param string $end The string to use at the end. |
361
|
|
|
* @return Closure(string):string |
362
|
|
|
*/ |
363
|
|
|
function chunk(int $length, string $end = "\r\n"): Closure |
364
|
|
|
{ |
365
|
|
|
/** |
366
|
|
|
* @param string $string The string to be chunked |
367
|
|
|
* @return string |
368
|
|
|
*/ |
369
|
|
|
return function (string $string) use ($length, $end): string { |
370
|
|
|
return \chunk_split($string, max(1, $length), $end); |
371
|
|
|
}; |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
/** |
375
|
|
|
* Creates a callable for wrapping a string to a defined value. |
376
|
|
|
* |
377
|
|
|
* @param int $width Max width for each "line" |
378
|
|
|
* @param string $break The string to use to denote the end of line. |
379
|
|
|
* @param bool $cut If set to true, words are cut at $width, else overflow. |
380
|
|
|
* @return Closure(string):string |
381
|
|
|
*/ |
382
|
|
|
function wordWrap(int $width, string $break = "\n", bool $cut = false): Closure |
383
|
|
|
{ |
384
|
|
|
/** |
385
|
|
|
* @param string $string The string to be wrapped |
386
|
|
|
* @return string |
387
|
|
|
*/ |
388
|
|
|
return function (string $string) use ($width, $break, $cut): string { |
389
|
|
|
return \wordwrap($string, $width, $break, $cut); |
390
|
|
|
}; |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
/** |
394
|
|
|
* Returns a callback for counting the number of occurrences of each char in a string. |
395
|
|
|
* |
396
|
|
|
* @link https://www.php.net/manual/en/function.count-chars.php |
397
|
|
|
* @param int $mode See the PHP docs for details. |
398
|
|
|
* @return Closure(string):(int[]|string) |
399
|
|
|
*/ |
400
|
|
|
function countChars(int $mode = 1): Closure |
401
|
|
|
{ |
402
|
|
|
// Throw an exception if the mode is not supported. |
403
|
|
|
if (! in_array($mode, array( 0, 1, 2, 3, 4 ), true)) { |
404
|
|
|
throw new \InvalidArgumentException('Invalid mode'); |
405
|
|
|
} |
406
|
|
|
|
407
|
|
|
/** |
408
|
|
|
* @param string $string The string to have its char counted. |
409
|
|
|
* @return int[]|string |
410
|
|
|
*/ |
411
|
|
|
return function (string $string) use ($mode) { |
412
|
|
|
return \count_chars($string, $mode); |
413
|
|
|
}; |
414
|
|
|
} |
415
|
|
|
|
416
|
|
|
/** |
417
|
|
|
* Returns a callable that counts the occurrences of a given substring in a string |
418
|
|
|
* |
419
|
|
|
* @param string $needle The substring to find |
420
|
|
|
* @param int $offset Place to start, defaults to 0 (start) |
421
|
|
|
* @param int|null $length Max length after offset to search. |
422
|
|
|
* @return Closure(string):int |
423
|
|
|
*/ |
424
|
|
|
function countSubString(string $needle, int $offset = 0, ?int $length = null): Closure |
425
|
|
|
{ |
426
|
|
|
/** |
427
|
|
|
* @param string $haystack |
428
|
|
|
* @return int |
429
|
|
|
*/ |
430
|
|
|
return function (string $haystack) use ($needle, $offset, $length): int { |
431
|
|
|
return $length |
432
|
|
|
? \substr_count($haystack, $needle, $offset, $length) |
433
|
|
|
: \substr_count($haystack, $needle, $offset); |
434
|
|
|
}; |
435
|
|
|
} |
436
|
|
|
|
437
|
|
|
/** |
438
|
|
|
* Returns a callable for doing repeated trim. |
439
|
|
|
* |
440
|
|
|
* @param string $mask |
441
|
|
|
* @return Closure(string):string |
442
|
|
|
*/ |
443
|
|
|
function trim(string $mask = "\t\n\r\0\x0B"): Closure |
444
|
|
|
{ |
445
|
|
|
/** |
446
|
|
|
* @param string $string The string to be trimmed |
447
|
|
|
* @return string |
448
|
|
|
*/ |
449
|
|
|
return function (string $string) use ($mask): string { |
450
|
|
|
return \trim($string, $mask); |
451
|
|
|
}; |
452
|
|
|
} |
453
|
|
|
|
454
|
|
|
/** |
455
|
|
|
* Returns a callable for doing repeated ltrim. |
456
|
|
|
* |
457
|
|
|
* @param string $mask |
458
|
|
|
* @return Closure(string):string |
459
|
|
|
*/ |
460
|
|
|
function lTrim(string $mask = "\t\n\r\0\x0B"): Closure |
461
|
|
|
{ |
462
|
|
|
/** |
463
|
|
|
* @param string $string The string to be trimmed |
464
|
|
|
* @return string |
465
|
|
|
*/ |
466
|
|
|
return function (string $string) use ($mask): string { |
467
|
|
|
return \ltrim($string, $mask); |
468
|
|
|
}; |
469
|
|
|
} |
470
|
|
|
|
471
|
|
|
/** |
472
|
|
|
* Returns a callable for doing repeated rtrim. |
473
|
|
|
* |
474
|
|
|
* @param string $mask |
475
|
|
|
* @return Closure(string):string |
476
|
|
|
*/ |
477
|
|
|
function rTrim(string $mask = "\t\n\r\0\x0B"): Closure |
478
|
|
|
{ |
479
|
|
|
/** |
480
|
|
|
* @param string $string The string to be trimmed |
481
|
|
|
* @return string |
482
|
|
|
*/ |
483
|
|
|
return function (string $string) use ($mask): string { |
484
|
|
|
return \rtrim($string, $mask); |
485
|
|
|
}; |
486
|
|
|
} |
487
|
|
|
|
488
|
|
|
/** |
489
|
|
|
* Returns a callable for finding the similarities between 2 string. |
490
|
|
|
* This sets the defined value as the base (similar_text as first) |
491
|
|
|
* |
492
|
|
|
* @param string $base The string to act as the base. |
493
|
|
|
* @param bool $asPc If set to true will return the percentage match, rather than char count. |
494
|
|
|
* @return Closure(string):Number |
495
|
|
|
*/ |
496
|
|
|
function similar(string $base, bool $asPc = false): Closure |
497
|
|
|
{ |
498
|
|
|
/** |
499
|
|
|
* @param string $comparisonString The string to compare against base. |
500
|
|
|
* @return Number |
501
|
|
|
*/ |
502
|
|
|
return function (string $comparisonString) use ($base, $asPc) { |
503
|
|
|
$pc = 0.00; |
504
|
|
|
$matching = similar_text($base, $comparisonString, $pc); |
505
|
|
|
return $asPc ? $pc : $matching; |
506
|
|
|
}; |
507
|
|
|
} |
508
|
|
|
|
509
|
|
|
/** |
510
|
|
|
* Returns a callable for finding the similarities between 2 string. |
511
|
|
|
* This sets the defined value as the base (similar_text as first) |
512
|
|
|
* |
513
|
|
|
* @deprecated Use similar() instead. |
514
|
|
|
* @param string $base The string to act as the base. |
515
|
|
|
* @param bool $asPc If set to true will return the percentage match, rather than char count. |
516
|
|
|
* @return Closure(string):Number |
517
|
|
|
*/ |
518
|
|
|
function similarAsBase(string $base, bool $asPc = false): Closure |
519
|
|
|
{ |
520
|
|
|
trigger_error('Deprecated function called. Please use similar. This function will be removed in later versions.', E_USER_DEPRECATED); |
521
|
|
|
return similar($base, $asPc); |
522
|
|
|
} |
523
|
|
|
|
524
|
|
|
/** |
525
|
|
|
* Returns a callable for finding the similarities between 2 string. |
526
|
|
|
* This sets the defined value as the comparisonString (similar_text as second) |
527
|
|
|
* |
528
|
|
|
* @deprecated Use similar() instead. |
529
|
|
|
* @param string $comparisonString The string to compare against base. |
530
|
|
|
* @param bool $asPc If set to true will return the percentage match, rather than char count. |
531
|
|
|
* @return Closure(string):Number |
532
|
|
|
*/ |
533
|
|
|
function similarAsComparison(string $comparisonString, bool $asPc = false): Closure |
534
|
|
|
{ |
535
|
|
|
trigger_error('Deprecated function called. Please use similar. This function will be removed in later versions.', E_USER_DEPRECATED); |
536
|
|
|
return similar($comparisonString, $asPc); |
537
|
|
|
} |
538
|
|
|
|
539
|
|
|
/** |
540
|
|
|
* Returns a callable for padding out a string. |
541
|
|
|
* |
542
|
|
|
* @param int $length Max length to make string. |
543
|
|
|
* @param string $padContent The value to padd the string with (defaults to ' ') |
544
|
|
|
* @param int $type How to pad, please use these constants. STR_PAD_RIGHT|STR_PAD_LEFT|STR_PAD_BOTH |
545
|
|
|
* @return Closure(string):string |
546
|
|
|
*/ |
547
|
|
|
function pad(int $length, string $padContent = ' ', int $type = STR_PAD_RIGHT): Closure |
548
|
|
|
{ |
549
|
|
|
/** |
550
|
|
|
* @param string $string The string to pad out. |
551
|
|
|
* @return string |
552
|
|
|
*/ |
553
|
|
|
return function (string $string) use ($length, $padContent, $type): string { |
554
|
|
|
return \str_pad($string, $length, $padContent, $type); |
555
|
|
|
}; |
556
|
|
|
} |
557
|
|
|
|
558
|
|
|
/** |
559
|
|
|
* Returns a callable for repeating a string by a defined number of times. |
560
|
|
|
* |
561
|
|
|
* @param int $count Number of times to repeat string. |
562
|
|
|
* @return Closure(string):string |
563
|
|
|
*/ |
564
|
|
|
function repeat(int $count): Closure |
565
|
|
|
{ |
566
|
|
|
/** |
567
|
|
|
* @param string $string The string to repeat |
568
|
|
|
* @return string |
569
|
|
|
*/ |
570
|
|
|
return function (string $string) use ($count): string { |
571
|
|
|
return \str_repeat($string, $count); |
572
|
|
|
}; |
573
|
|
|
} |
574
|
|
|
|
575
|
|
|
/** |
576
|
|
|
* Returns a callback for creating a word counter, with set format and char list. |
577
|
|
|
* |
578
|
|
|
* @param int $format can use WORD_COUNT_NUMBER_OF_WORDS | WORD_COUNT_ARRAY | WORD_COUNT_ASSOCIATIVE_ARRAY |
579
|
|
|
* @param string|null $charList The char list of option values considered words. |
580
|
|
|
* @return Closure(string):(int|string[]) |
581
|
|
|
*/ |
582
|
|
|
function wordCount(int $format = WORD_COUNT_NUMBER_OF_WORDS, ?string $charList = null): Closure |
583
|
|
|
{ |
584
|
|
|
/** |
585
|
|
|
* @param string $string The string to pad out. |
586
|
|
|
* @return int|string[] |
587
|
|
|
*/ |
588
|
|
|
return function (string $string) use ($format, $charList) { |
589
|
|
|
return $charList |
590
|
|
|
? (\str_word_count($string, $format, $charList) ?: 0) |
591
|
|
|
: (\str_word_count($string, $format) ?: 0); |
592
|
|
|
}; |
593
|
|
|
} |
594
|
|
|
|
595
|
|
|
/** |
596
|
|
|
* Creates a function for stripping tags with a defined set of allowed tags. |
597
|
|
|
* |
598
|
|
|
* @param string|null $allowedTags The allowed tags, pass null or leave blank for none. |
599
|
|
|
* @return Closure(string):string |
600
|
|
|
*/ |
601
|
|
|
function stripTags(?string $allowedTags = null): Closure |
602
|
|
|
{ |
603
|
|
|
/** |
604
|
|
|
* @param string $string The string to strip tags from. |
605
|
|
|
* @return string |
606
|
|
|
*/ |
607
|
|
|
return function (string $string) use ($allowedTags): string { |
608
|
|
|
return $allowedTags |
609
|
|
|
? \strip_tags($string, $allowedTags) |
610
|
|
|
: \strip_tags($string); |
611
|
|
|
}; |
612
|
|
|
} |
613
|
|
|
|
614
|
|
|
/** |
615
|
|
|
* Returns a callable for finding the first postition of a defined value in any string. |
616
|
|
|
* |
617
|
|
|
* @param string $needle The value to look for. |
618
|
|
|
* @param int $offset The offset to start |
619
|
|
|
* @param int $flags STRINGS_CASE_SENSITIVE | STRINGS_CASE_INSENSITIVE |
620
|
|
|
* @return Closure(string):?int |
621
|
|
|
*/ |
622
|
|
|
function firstPosition(string $needle, int $offset = 0, int $flags = STRINGS_CASE_SENSITIVE): Closure |
623
|
|
|
{ |
624
|
|
|
$caseSensitive = ! (bool) ($flags & STRINGS_CASE_INSENSITIVE); // Assumes true unless INSESNITVE passed |
625
|
|
|
|
626
|
|
|
/** |
627
|
|
|
* @param string $haystack The haystack to look through. |
628
|
|
|
* @return int|null |
629
|
|
|
*/ |
630
|
|
|
return function (string $haystack) use ($needle, $offset, $caseSensitive): ?int { |
631
|
|
|
$pos = $caseSensitive |
632
|
|
|
? strpos($haystack, $needle, $offset) |
633
|
|
|
: stripos($haystack, $needle, $offset); |
634
|
|
|
return is_int($pos) ? (int) $pos : null; |
|
|
|
|
635
|
|
|
}; |
636
|
|
|
} |
637
|
|
|
|
638
|
|
|
/** |
639
|
|
|
* Returns a callable for finding the first position of a defined value in any string. |
640
|
|
|
* |
641
|
|
|
* @param string $needle The value to look for. |
642
|
|
|
* @param int $offset The offset to start |
643
|
|
|
* @param int $flags STRINGS_CASE_SENSITIVE | STRINGS_CASE_INSENSITIVE |
644
|
|
|
* @return Closure(string):?int |
645
|
|
|
*/ |
646
|
|
|
function lastPosition(string $needle, int $offset = 0, int $flags = STRINGS_CASE_SENSITIVE): Closure |
647
|
|
|
{ |
648
|
|
|
$caseSensitive = ! (bool) ($flags & STRINGS_CASE_INSENSITIVE); // Assumes true unless INSESNITVE passed |
649
|
|
|
|
650
|
|
|
/** |
651
|
|
|
* @param string $haystack The haystack to look through. |
652
|
|
|
* @return int|null |
653
|
|
|
*/ |
654
|
|
|
return function (string $haystack) use ($needle, $offset, $caseSensitive): ?int { |
655
|
|
|
$pos = $caseSensitive |
656
|
|
|
? strrpos($haystack, $needle, $offset) |
657
|
|
|
: strripos($haystack, $needle, $offset); |
658
|
|
|
return is_int($pos) ? (int) $pos : null; |
|
|
|
|
659
|
|
|
}; |
660
|
|
|
} |
661
|
|
|
|
662
|
|
|
/** |
663
|
|
|
* Returns a callable for looking for the first occurrence of a substring. |
664
|
|
|
* When found can return all after or before the needle (sub string) |
665
|
|
|
* Can be done as case sensitive (strstr()) or insensitive (stristr()) |
666
|
|
|
* |
667
|
|
|
* @param string $needle The substring to look for. |
668
|
|
|
* @param int $flags Possible STRINGS_CASE_INSENSITIVE | STRINGS_CASE_SENSITIVE | STRINGS_AFTER_NEEDLE | STRINGS_BEFORE_NEEDLE |
669
|
|
|
* @return Closure(string):string |
670
|
|
|
*/ |
671
|
|
|
function firstSubString(string $needle, int $flags = STRINGS_CASE_SENSITIVE | STRINGS_AFTER_NEEDLE): Closure |
672
|
|
|
{ |
673
|
|
|
// Decode flags, only look for none defaults. |
674
|
|
|
$beforeNeedle = (bool) ($flags & STRINGS_BEFORE_NEEDLE); |
675
|
|
|
$caseSensitive = ! (bool) ($flags & STRINGS_CASE_INSENSITIVE); // Assumes true unless INSESNITVE passed |
676
|
|
|
|
677
|
|
|
/** |
678
|
|
|
* @param string $haystack The haystack to look through. |
679
|
|
|
* @return string |
680
|
|
|
*/ |
681
|
|
|
return function (string $haystack) use ($needle, $beforeNeedle, $caseSensitive): string { |
682
|
|
|
$result = $caseSensitive |
683
|
|
|
? strstr($haystack, $needle, $beforeNeedle) |
684
|
|
|
: stristr($haystack, $needle, $beforeNeedle); |
685
|
|
|
return is_string($result) ? $result : ''; |
|
|
|
|
686
|
|
|
}; |
687
|
|
|
} |
688
|
|
|
|
689
|
|
|
/** |
690
|
|
|
* Returns a callable for creating a function which finds the first occurrence of |
691
|
|
|
* any character (from a list) in a defined string. |
692
|
|
|
* |
693
|
|
|
* @param string $chars All chars to check with. |
694
|
|
|
* @return Closure(string):string |
695
|
|
|
*/ |
696
|
|
|
function firstChar(string $chars): Closure |
697
|
|
|
{ |
698
|
|
|
/** |
699
|
|
|
* @param string $haystack |
700
|
|
|
* @return string |
701
|
|
|
*/ |
702
|
|
|
return function (string $haystack) use ($chars): string { |
703
|
|
|
$result = strpbrk($haystack, $chars); |
704
|
|
|
return is_string($result) ? $result : ''; |
|
|
|
|
705
|
|
|
}; |
706
|
|
|
} |
707
|
|
|
|
708
|
|
|
/** |
709
|
|
|
* Returns a function that finds the last char in a string and returns the following text. |
710
|
|
|
* Matches the first char passed, if more than 1 char passed, the rest are ignored. |
711
|
|
|
* |
712
|
|
|
* @param string $char |
713
|
|
|
* @return Closure(string):string |
714
|
|
|
*/ |
715
|
|
|
function lastChar(string $char): Closure |
716
|
|
|
{ |
717
|
|
|
/** |
718
|
|
|
* @param string $haystack |
719
|
|
|
* @return string |
720
|
|
|
*/ |
721
|
|
|
return function (string $haystack) use ($char): string { |
722
|
|
|
$result = strrchr($haystack, $char); |
723
|
|
|
return is_string($result) ? $result : ''; |
|
|
|
|
724
|
|
|
}; |
725
|
|
|
} |
726
|
|
|
|
727
|
|
|
/** |
728
|
|
|
* Returns a callable which translates substrings from a defined dictionary. |
729
|
|
|
* Dictionary should be ['from' => 'to' ] |
730
|
|
|
* |
731
|
|
|
* @param array<string, mixed> $dictionary |
732
|
|
|
* @return Closure(string):string |
733
|
|
|
*/ |
734
|
|
|
function translateWith(array $dictionary): Closure |
735
|
|
|
{ |
736
|
|
|
/** |
737
|
|
|
* @param string $haystack |
738
|
|
|
* @return string |
739
|
|
|
*/ |
740
|
|
|
return function (string $haystack) use ($dictionary): string { |
741
|
|
|
$result = strtr($haystack, $dictionary); |
742
|
|
|
return $result; |
743
|
|
|
}; |
744
|
|
|
} |
745
|
|
|
|
746
|
|
|
/** |
747
|
|
|
* Creates a callable for a string safe function compose. |
748
|
|
|
* |
749
|
|
|
* @uses F\composeTypeSafe |
750
|
|
|
* @param callable(mixed):string ...$callable |
751
|
|
|
* @return Closure(mixed):string |
752
|
|
|
*/ |
753
|
|
|
function composeSafeStringFunc(callable ...$callable): Closure |
754
|
|
|
{ |
755
|
|
|
return F\composeTypeSafe('is_string', ...$callable); |
|
|
|
|
756
|
|
|
} |
757
|
|
|
|
758
|
|
|
/** |
759
|
|
|
* Creates a callable for compiling a string. |
760
|
|
|
* |
761
|
|
|
* @param string $initial |
762
|
|
|
* @return Closure(string|null):(Closure|string) |
763
|
|
|
*/ |
764
|
|
|
function stringCompiler(string $initial = ''): Closure |
765
|
|
|
{ |
766
|
|
|
/** |
767
|
|
|
* @param string|null $value |
768
|
|
|
* @return Closure|string |
769
|
|
|
*/ |
770
|
|
|
return function (?string $value = null) use ($initial) { |
771
|
|
|
if ($value) { |
772
|
|
|
$initial .= $value; |
773
|
|
|
} |
774
|
|
|
return $value ? stringCompiler($initial) : $initial; |
775
|
|
|
}; |
776
|
|
|
} |
777
|
|
|
|
778
|
|
|
/** |
779
|
|
|
* Checks if the passe value is a string and if its length is 0. |
780
|
|
|
* |
781
|
|
|
* @param mixed $value |
782
|
|
|
* @return bool |
783
|
|
|
*/ |
784
|
|
|
function isBlank($value): bool |
785
|
|
|
{ |
786
|
|
|
return is_string($value) && \mb_strlen($value) === 0; |
787
|
|
|
} |
788
|
|
|
|
789
|
|
|
|
790
|
|
|
/************************************************************************/ |
791
|
|
|
/************************************************************************/ |
792
|
|
|
/** Deprecated Functions **/ |
793
|
|
|
/************************************************************************/ |
794
|
|
|
/************************************************************************/ |
795
|
|
|
|
796
|
|
|
|
797
|
|
|
/** |
798
|
|
|
* See decimalNumber() |
799
|
|
|
* This function will be removed in later versions. |
800
|
|
|
* |
801
|
|
|
* @deprecated Use decimalNumber() instead. |
802
|
|
|
* |
803
|
|
|
* @param int $precision |
804
|
|
|
* @param string $point |
805
|
|
|
* @param string $thousands |
806
|
|
|
* @return Closure |
807
|
|
|
*/ |
808
|
|
|
function decimialNumber($precision = 2, $point = '.', $thousands = ''): Closure |
809
|
|
|
{ |
810
|
|
|
trigger_error('Deprecated function called. Please use digit. This function will be removed in later versions.', E_USER_DEPRECATED); |
811
|
|
|
return digit($precision, $point, $thousands); |
812
|
|
|
} |
813
|
|
|
|
814
|
|
|
/** |
815
|
|
|
* Converts a number (loose type) to a string representation of a float. |
816
|
|
|
* |
817
|
|
|
* @param string|int|float $precision Number of decimal places |
818
|
|
|
* @param string $point The decimal separator |
819
|
|
|
* @param string $thousands The thousand separator. |
820
|
|
|
* @return Closure(string|int|float):string |
821
|
|
|
*/ |
822
|
|
|
function decimalNumber($precision = 2, $point = '.', $thousands = ''): Closure |
823
|
|
|
{ |
824
|
|
|
trigger_error('Deprecated function called. Please use digit. This function will be removed in later versions.', E_USER_DEPRECATED); |
825
|
|
|
return digit($precision, $point, $thousands); |
826
|
|
|
} |
827
|
|
|
|
828
|
|
|
/** |
829
|
|
|
* See similarAsComparison() |
830
|
|
|
* |
831
|
|
|
* @deprecated Use similarAsComparison() instead. |
832
|
|
|
* @param string $comparisonString |
833
|
|
|
* @param bool $asPc |
834
|
|
|
* @return Closure |
835
|
|
|
*/ |
836
|
|
|
function similarAsComparisson(string $comparisonString, bool $asPc = false): Closure |
837
|
|
|
{ |
838
|
|
|
trigger_error('Deprecated function called. Please use similarAsComparison. This function will be removed in later versions.', E_USER_DEPRECATED); |
839
|
|
|
return similarAsComparison($comparisonString, $asPc); |
|
|
|
|
840
|
|
|
} |
841
|
|
|
|
842
|
|
|
/** |
843
|
|
|
* See firstPosition() |
844
|
|
|
* |
845
|
|
|
* @deprecated |
846
|
|
|
* @param string $needle |
847
|
|
|
* @param int $offset |
848
|
|
|
* @param int $flags |
849
|
|
|
* @return Closure |
850
|
|
|
*/ |
851
|
|
|
function firstPosistion(string $needle, int $offset = 0, int $flags = STRINGS_CASE_SENSITIVE): Closure |
852
|
|
|
{ |
853
|
|
|
trigger_error('Deprecated function called. Please use firstPosition. This function will be removed in later versions.', E_USER_DEPRECATED); |
854
|
|
|
return firstPosition($needle, $offset, $flags); |
855
|
|
|
} |
856
|
|
|
|
857
|
|
|
/** |
858
|
|
|
* See lastPosition() |
859
|
|
|
* |
860
|
|
|
* @deprecated Use lastPosition() instead. |
861
|
|
|
* @param string $needle The value to look for. |
862
|
|
|
* @param int $offset The offset to start |
863
|
|
|
* @param int $flags STRINGS_CASE_SENSITIVE | STRINGS_CASE_INSENSITIVE |
864
|
|
|
* @return Closure |
865
|
|
|
*/ |
866
|
|
|
function lastPosistion(string $needle, int $offset = 0, int $flags = STRINGS_CASE_SENSITIVE): Closure |
867
|
|
|
{ |
868
|
|
|
trigger_error('Deprecated function called. Please use lastPosition. This function will be removed in later versions.', E_USER_DEPRECATED); |
869
|
|
|
return lastPosition($needle, $offset, $flags); |
870
|
|
|
} |
871
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths