Passed
Pull Request — develop (#56)
by Glynn
03:58
created

splitByLength()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 8
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 1
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
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;
0 ignored issues
show
Bug introduced by
The type PinkCrab\FunctionConstructors\Comparisons was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
35
use PinkCrab\FunctionConstructors\GeneralFunctions as F;
0 ignored issues
show
Bug introduced by
The type PinkCrab\FunctionConstructors\GeneralFunctions was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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
0 ignored issues
show
Bug Best Practice introduced by
The expression return $length ? substr_...ing, $replace, $offset) could return the type array which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
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 decimalNumber($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 int $limit The number of chars to split by.
323
 * @return Closure(string):array<string> The parts.
324
 */
325
function split(string $separator, int $limit = PHP_INT_MAX): Closure
326
{
327
    /**
328
     * @param string $string The string to be split
329
     * @return array<int, string>
330
     */
331
    return function (string $string) use ($separator, $limit): array {
332
        return explode($separator, $string, $limit); // @phpstan-ignore-line, inconsistent errors with differing php versions.
333
    };
334
}
335
336
/**
337
 * Returns a callable for splitting a string by a set amount.
338
 *
339
 * @param int $length The length to split the string up with.
340
 * @return Closure(string):array<string> The parts.
341
 */
342
function splitByLength(int $length): Closure
343
{
344
    /**
345
     * @param string $string The string to be split
346
     * @return array<int, string>
347
     */
348
    return function (string $string) use ($length): array {
349
        return \str_split($string, max(1, $length)) ?: array(); // @phpstan-ignore-line, inconsistent errors with differing php versions.
0 ignored issues
show
Bug Best Practice introduced by
The expression return str_split($string...1, $length)) ?: array() could return the type true which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
350
    };
351
}
352
353
354
/**
355
 * Returns a callback for splitting a string into chunks.
356
 *
357
 * @param int $length The length of each chunk.
358
 * @param string $end The string to use at the end.
359
 * @return Closure(string):string
360
 */
361
function chunk(int $length, string $end = "\r\n"): Closure
362
{
363
    /**
364
     * @param string $string The string to be chunked
365
     * @return string
366
     */
367
    return function (string $string) use ($length, $end): string {
368
        return \chunk_split($string, max(1, $length), $end);
369
    };
370
}
371
372
/**
373
 * Creates a callable for wrapping a string to a defined value.
374
 *
375
 * @param int $width Max width for each "line"
376
 * @param string $break The string to use to denote the end of line.
377
 * @param bool $cut If set to true, words are cut at $width, else overflow.
378
 * @return Closure(string):string
379
 */
380
function wordWrap(int $width, string $break = "\n", bool $cut = false): Closure
381
{
382
    /**
383
     * @param string $string The string to be wrapped
384
     * @return string
385
     */
386
    return function (string $string) use ($width, $break, $cut): string {
387
        return \wordwrap($string, $width, $break, $cut);
388
    };
389
}
390
391
/**
392
 * Returns a callback for counting the number of occurrences of each char in a string.
393
 *
394
 * @link https://www.php.net/manual/en/function.count-chars.php
395
 * @param int $mode See the PHP docs for details.
396
 * @return Closure(string):(int[]|string)
397
 */
398
function countChars(int $mode = 1): Closure
399
{
400
    // Throw an exception if the mode is not supported.
401
    if (! in_array($mode, array( 0, 1, 2, 3, 4 ), true)) {
402
        throw new \InvalidArgumentException('Invalid mode');
403
    }
404
405
    /**
406
     * @param string $string The string to have its char counted.
407
     * @return int[]|string
408
     */
409
    return function (string $string) use ($mode) {
410
        return \count_chars($string, $mode);
411
    };
412
}
413
414
/**
415
 * Returns a callable that counts the occurrences of a given substring in a string
416
 *
417
 * @param string $needle The substring to find
418
 * @param int $offset Place to start, defaults to 0 (start)
419
 * @param int|null $length Max length after offset to search.
420
 * @return Closure(string):int
421
 */
422
function countSubString(string $needle, int $offset = 0, ?int $length = null): Closure
423
{
424
    /**
425
     * @param string $haystack
426
     * @return int
427
     */
428
    return function (string $haystack) use ($needle, $offset, $length): int {
429
        return $length
430
            ? \substr_count($haystack, $needle, $offset, $length)
431
            : \substr_count($haystack, $needle, $offset);
432
    };
433
}
434
435
/**
436
 * Returns a callable for doing repeated trim.
437
 *
438
 * @param string $mask
439
 * @return Closure(string):string
440
 */
441
function trim(string $mask = "\t\n\r\0\x0B"): Closure
442
{
443
    /**
444
     * @param string $string The string to be trimmed
445
     * @return string
446
     */
447
    return function (string $string) use ($mask): string {
448
        return \trim($string, $mask);
449
    };
450
}
451
452
/**
453
 * Returns a callable for doing repeated ltrim.
454
 *
455
 * @param string $mask
456
 * @return Closure(string):string
457
 */
458
function lTrim(string $mask = "\t\n\r\0\x0B"): Closure
459
{
460
    /**
461
     * @param string $string The string to be trimmed
462
     * @return string
463
     */
464
    return function (string $string) use ($mask): string {
465
        return \ltrim($string, $mask);
466
    };
467
}
468
469
/**
470
 * Returns a callable for doing repeated rtrim.
471
 *
472
 * @param string $mask
473
 * @return Closure(string):string
474
 */
475
function rTrim(string $mask = "\t\n\r\0\x0B"): Closure
476
{
477
    /**
478
     * @param string $string The string to be trimmed
479
     * @return string
480
     */
481
    return function (string $string) use ($mask): string {
482
        return \rtrim($string, $mask);
483
    };
484
}
485
486
/**
487
 * Returns a callable for finding the similarities between 2 string.
488
 * This sets the defined value as the base (similar_text as first)
489
 *
490
 * @param string $base The string to act as the base.
491
 * @param bool $asPc If set to true will return the percentage match, rather than char count.
492
 * @return Closure(string):Number
493
 */
494
function similarAsBase(string $base, bool $asPc = false): Closure
495
{
496
    /**
497
     * @param string $comparisonString The string to compare against base.
498
     * @return Number
499
     */
500
    return function (string $comparisonString) use ($base, $asPc) {
501
        $pc       = 0.00;
502
        $matching = similar_text($base, $comparisonString, $pc);
503
        return $asPc ? $pc : $matching;
504
    };
505
}
506
507
/**
508
 * Returns a callable for finding the similarities between 2 string.
509
 * This sets the defined value as the comparisonString (similar_text as second)
510
 *
511
 * @param string $comparisonString The string to compare against base.
512
 * @param bool $asPc If set to true will return the percentage match, rather than char count.
513
 * @return Closure(string):Number
514
 */
515
function similarAsComparison(string $comparisonString, bool $asPc = false): Closure
516
{
517
    /**
518
     * @param string $comparisonString The string to act as the base.
519
     * @return Number
520
     */
521
    return function (string $base) use ($comparisonString, $asPc) {
522
        $pc       = 0.00;
523
        $matching = similar_text($base, $comparisonString, $pc);
524
        return $asPc ? $pc : $matching;
525
    };
526
}
527
528
/**
529
 * Returns a callable for padding out a string.
530
 *
531
 * @param int $length Max length to make string.
532
 * @param string $padContent The value to padd the string with (defaults to ' ')
533
 * @param int $type How to pad, please use these constants. STR_PAD_RIGHT|STR_PAD_LEFT|STR_PAD_BOTH
534
 * @return Closure(string):string
535
 */
536
function pad(int $length, string $padContent = ' ', int $type = STR_PAD_RIGHT): Closure
537
{
538
    /**
539
     * @param string $string The string to pad out.
540
     * @return string
541
     */
542
    return function (string $string) use ($length, $padContent, $type): string {
543
        return \str_pad($string, $length, $padContent, $type);
544
    };
545
}
546
547
/**
548
 * Returns a callable for repeating a string by a defined number of times.
549
 *
550
 * @param int $count Number of times to repeat string.
551
 * @return Closure(string):string
552
 */
553
function repeat(int $count): Closure
554
{
555
    /**
556
     * @param string $string The string to repeat
557
     * @return string
558
     */
559
    return function (string $string) use ($count): string {
560
        return \str_repeat($string, $count);
561
    };
562
}
563
564
/**
565
 * Returns a callback for creating a word counter, with set format and char list.
566
 *
567
 * @param int $format can use WORD_COUNT_NUMBER_OF_WORDS | WORD_COUNT_ARRAY | WORD_COUNT_ASSOCIATIVE_ARRAY
568
 * @param string|null $charList The char list of option values considered words.
569
 * @return Closure(string):(int|string[])
570
 */
571
function wordCount(int $format = WORD_COUNT_NUMBER_OF_WORDS, ?string $charList = null): Closure
572
{
573
    /**
574
     * @param string $string The string to pad out.
575
     * @return int|string[]
576
     */
577
    return function (string $string) use ($format, $charList) {
578
        return $charList
579
            ? (\str_word_count($string, $format, $charList) ?: 0)
580
            : (\str_word_count($string, $format) ?: 0);
581
    };
582
}
583
584
/**
585
 * Creates a function for stripping tags with a defined set of allowed tags.
586
 *
587
 * @param string|null $allowedTags The allowed tags, pass null or leave blank for none.
588
 * @return Closure(string):string
589
 */
590
function stripTags(?string $allowedTags = null): Closure
591
{
592
    /**
593
     * @param string $string The string to strip tags from.
594
     * @return string
595
     */
596
    return function (string $string) use ($allowedTags): string {
597
        return $allowedTags
598
            ? \strip_tags($string, $allowedTags)
599
            : \strip_tags($string);
600
    };
601
}
602
603
/**
604
 * Returns a callable for finding the first postition of a defined value in any string.
605
 *
606
 * @param string $needle The value to look for.
607
 * @param int  $offset The offset to start
608
 * @param int $flags STRINGS_CASE_SENSITIVE | STRINGS_CASE_INSENSITIVE
609
 * @return Closure(string):?int
610
 */
611
function firstPosition(string $needle, int $offset = 0, int $flags = STRINGS_CASE_SENSITIVE): Closure
612
{
613
    $caseSensitive = ! (bool) ($flags & STRINGS_CASE_INSENSITIVE); // Assumes true unless INSESNITVE passed
614
615
    /**
616
     * @param string $haystack The haystack to look through.
617
     * @return int|null
618
     */
619
    return function (string $haystack) use ($needle, $offset, $caseSensitive): ?int {
620
        $pos = $caseSensitive
621
            ? strpos($haystack, $needle, $offset)
622
            : stripos($haystack, $needle, $offset);
623
        return is_int($pos) ? (int) $pos : null;
0 ignored issues
show
introduced by
The condition is_int($pos) is always true.
Loading history...
624
    };
625
}
626
627
/**
628
 * Returns a callable for finding the first position of a defined value in any string.
629
 *
630
 * @param string $needle The value to look for.
631
 * @param int  $offset The offset to start
632
 * @param int $flags STRINGS_CASE_SENSITIVE | STRINGS_CASE_INSENSITIVE
633
 * @return Closure(string):?int
634
 */
635
function lastPosition(string $needle, int $offset = 0, int $flags = STRINGS_CASE_SENSITIVE): Closure
636
{
637
    $caseSensitive = ! (bool) ($flags & STRINGS_CASE_INSENSITIVE); // Assumes true unless INSESNITVE passed
638
639
    /**
640
     * @param string $haystack The haystack to look through.
641
     * @return int|null
642
     */
643
    return function (string $haystack) use ($needle, $offset, $caseSensitive): ?int {
644
        $pos = $caseSensitive
645
            ? strrpos($haystack, $needle, $offset)
646
            : strripos($haystack, $needle, $offset);
647
        return is_int($pos) ? (int) $pos : null;
0 ignored issues
show
introduced by
The condition is_int($pos) is always true.
Loading history...
648
    };
649
}
650
651
/**
652
 * Returns a callable for looking for the first occurrence of a substring.
653
 * When found can return all after or before the needle (sub string)
654
 * Can be done as case sensitive (strstr()) or insensitive (stristr())
655
 *
656
 * @param string $needle The substring to look for.
657
 * @param int $flags Possible STRINGS_CASE_INSENSITIVE | STRINGS_CASE_SENSITIVE | STRINGS_AFTER_NEEDLE | STRINGS_BEFORE_NEEDLE
658
 * @return Closure(string):string
659
 */
660
function firstSubString(string $needle, int $flags = STRINGS_CASE_SENSITIVE | STRINGS_AFTER_NEEDLE): Closure
661
{
662
663
    // Decode flags, only look for none defaults.
664
    $beforeNeedle  = (bool) ($flags & STRINGS_BEFORE_NEEDLE);
665
    $caseSensitive = ! (bool) ($flags & STRINGS_CASE_INSENSITIVE); // Assumes true unless INSESNITVE passed
666
667
    /**
668
     * @param string $haystack The haystack to look through.
669
     * @return string
670
     */
671
    return function (string $haystack) use ($needle, $beforeNeedle, $caseSensitive): string {
672
        $result = $caseSensitive
673
            ? strstr($haystack, $needle, $beforeNeedle)
674
            : stristr($haystack, $needle, $beforeNeedle);
675
        return is_string($result) ? $result : '';
0 ignored issues
show
introduced by
The condition is_string($result) is always true.
Loading history...
676
    };
677
}
678
679
/**
680
 * Returns a callable for creating a function which finds the first occurrence of
681
 * any character (from a list) in a defined string.
682
 *
683
 * @param string $chars All chars to check with.
684
 * @return Closure(string):string
685
 */
686
function firstChar(string $chars): Closure
687
{
688
    /**
689
     * @param string $haystack
690
     * @return string
691
     */
692
    return function (string $haystack) use ($chars): string {
693
        $result = strpbrk($haystack, $chars);
694
        return is_string($result) ? $result : '';
0 ignored issues
show
introduced by
The condition is_string($result) is always true.
Loading history...
695
    };
696
}
697
698
/**
699
 * Returns a function that finds the last char in a string and returns the following text.
700
 * Matches the first char passed, if more than 1 char passed, the rest are ignored.
701
 *
702
 * @param string $char
703
 * @return Closure(string):string
704
 */
705
function lastChar(string $char): Closure
706
{
707
    /**
708
     * @param string $haystack
709
     * @return string
710
     */
711
    return function (string $haystack) use ($char): string {
712
        $result = strrchr($haystack, $char);
713
        return is_string($result) ? $result : '';
0 ignored issues
show
introduced by
The condition is_string($result) is always true.
Loading history...
714
    };
715
}
716
717
/**
718
 * Returns a callable which translates substrings from a defined dictionary.
719
 * Dictionary should be ['from' => 'to' ]
720
 *
721
 * @param array<string, mixed> $dictionary
722
 * @return Closure(string):string
723
 */
724
function translateWith(array $dictionary): Closure
725
{
726
    /**
727
     * @param string $haystack
728
     * @return string
729
     */
730
    return function (string $haystack) use ($dictionary): string {
731
        $result = strtr($haystack, $dictionary);
732
        return $result;
733
    };
734
}
735
736
/**
737
 * Creates a callable for a string safe function compose.
738
 *
739
 * @uses F\composeTypeSafe
740
 * @param callable(mixed):string ...$callable
741
 * @return Closure(mixed):string
742
 */
743
function composeSafeStringFunc(callable ...$callable): Closure
744
{
745
    return F\composeTypeSafe('is_string', ...$callable);
0 ignored issues
show
Bug introduced by
The function composeTypeSafe was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

745
    return /** @scrutinizer ignore-call */ F\composeTypeSafe('is_string', ...$callable);
Loading history...
746
}
747
748
/**
749
 * Creates a callable for compiling a string.
750
 *
751
 * @param string $initial
752
 * @return Closure(string|null):(Closure|string)
753
 */
754
function stringCompiler(string $initial = ''): Closure
755
{
756
    /**
757
     * @param string|null $value
758
     * @return Closure|string
759
     */
760
    return function (?string $value = null) use ($initial) {
761
        if ($value) {
762
            $initial .= $value;
763
        }
764
        return $value ? stringCompiler($initial) : $initial;
765
    };
766
}
767
768
/**
769
 * Checks if the passe value is a string and if its length is 0.
770
 *
771
 * @param mixed $value
772
 * @return bool
773
 */
774
function isBlank($value): bool
775
{
776
    return is_string($value) && \mb_strlen($value) === 0;
777
}
778
779
780
/************************************************************************/
781
/************************************************************************/
782
/**                        Deprecated Functions                        **/
783
/************************************************************************/
784
/************************************************************************/
785
786
787
/**
788
 * See decimalNumber()
789
 *
790
 * @deprecated Use decimalNumber() instead.
791
 * @param int $precision
792
 * @param string $point
793
 * @param string $thousands
794
 * @return Closure
795
 */
796
function decimialNumber($precision = 2, $point = '.', $thousands = ''): Closure
797
{
798
    return decimalNumber($precision, $point, $thousands);
799
}
800
801
/**
802
 * See similarAsComparison()
803
 *
804
 * @deprecated Use similarAsComparison() instead.
805
 * @param string $comparisonString
806
 * @param bool $asPc
807
 * @return Closure
808
 */
809
function similarAsComparisson(string $comparisonString, bool $asPc = false): Closure
810
{
811
    return similarAsComparison($comparisonString, $asPc);
812
}
813
814
/**
815
 * See firstPosition()
816
 *
817
 * @deprecated
818
 * @param string $needle
819
 * @param int $offset
820
 * @param int $flags
821
 * @return Closure
822
 */
823
function firstPosistion(string $needle, int $offset = 0, int $flags = STRINGS_CASE_SENSITIVE): Closure
824
{
825
    return firstPosition($needle, $offset, $flags);
826
}
827
828
/**
829
 * See lastPosition()
830
 *
831
 * @deprecated Use lastPosition() instead.
832
 * @param string $needle The value to look for.
833
 * @param int  $offset The offset to start
834
 * @param int $flags STRINGS_CASE_SENSITIVE | STRINGS_CASE_INSENSITIVE
835
 * @return Closure
836
 */
837
function lastPosistion(string $needle, int $offset = 0, int $flags = STRINGS_CASE_SENSITIVE): Closure
838
{
839
    return lastPosition($needle, $offset, $flags);
840
}
841