Passed
Push — develop ( 0f5267...f8b62a )
by Glynn
04:13 queued 01:43
created

asUrl()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 11
nc 1
nop 2
dl 0
loc 18
rs 9.9
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A prepend() 0 8 1
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 array<string, mixed> $args
116
 * @return Closure(string):string
117
 */
118
function vSprintf(array $args = array()): Closure
119
{
120
    /**
121
     * @param string $string
122
     * @return string Will return original string if false.
123
     */
124
    return function (string $string) use ($args): string {
125
        $result = \vsprintf($string, $args);
126
        return ! C\isFalse($result) ? $result : $string;
0 ignored issues
show
Unused Code introduced by
The call to isFalse() has too many arguments starting with $result. ( Ignorable by Annotation )

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

126
        return ! /** @scrutinizer ignore-call */ C\isFalse($result) ? $result : $string;

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
127
    };
128
}
129
130
/**
131
 * Creates a double curried find to replace.
132
 *
133
 * @param string $find Value to look for
134
 * @return Closure(string):Closure
135
 */
136
function findToReplace(string $find): Closure
137
{
138
    /**
139
     * @param string $replace value to replace with
140
     * @return Closure(string):string
141
     */
142
    return function (string $replace) use ($find): Closure {
143
        /**
144
         * @param string $subject String to carry out find and replace.
145
         * @return string
146
         */
147
        return function ($subject) use ($find, $replace): string {
148
            return \str_replace($find, $replace, $subject);
149
        };
150
    };
151
}
152
153
/**
154
 * Creates a Closure to find and replace within a string.
155
 *
156
 * @param string  $find
157
 * @param string  $replace
158
 * @return Closure(string):string
159
 */
160
function replaceWith(string $find, string $replace): Closure
161
{
162
    /**
163
     * @param string $source
164
     * @return string
165
     */
166
    return function ($source) use ($find, $replace): string {
167
        return \str_replace($find, $replace, $source);
168
    };
169
}
170
171
/**
172
 * Returns a callable that can replace a sub string with a pre defined value.
173
 *
174
 * @param string $replace The value to replace in passed string
175
 * @param int $offset The offset to start, negative numbers count back from end.
176
 * @param int|null $length Number of chars to stop replacing at end of replacement.
177
 * @return Closure(string):string
178
 */
179
function replaceSubString(string $replace, int $offset = 0, ?int $length = null): Closure
180
{
181
    /**
182
     * @param string $string
183
     * @return string
184
     */
185
    return function (string $string) use ($replace, $offset, $length): string {
186
        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...
187
            ? \substr_replace($string, $replace, $offset, $length)
188
            : \substr_replace($string, $replace, $offset);
189
    };
190
}
191
192
/**
193
 * Creates a callable for checking if a string starts with
194
 *
195
 * @param string $find The value to look for.
196
 * @return Closure(string):bool
197
 */
198
function startsWith(string $find): Closure
199
{
200
    /**
201
     * @param string $source
202
     * @return bool
203
     */
204
    return function (string $source) use ($find): bool {
205
        return (\substr($source, 0, \strlen($find)) === $find);
206
    };
207
}
208
209
/**
210
 * Creates a callable for checkin if a string ends with
211
 *
212
 * @param string $find The value to look for.
213
 * @return Closure(string):bool
214
 */
215
function endsWith(string $find): Closure
216
{
217
    /**
218
     * @param string $source
219
     * @return bool
220
     */
221
    return function (string $source) use ($find): bool {
222
        if (\strlen($find) === 0) {
223
            return true;
224
        }
225
        return (\substr($source, -\strlen($find)) === $find);
226
    };
227
}
228
229
/**
230
 * Creates a callable for checking if a string contains. using stringContains
231
 *
232
 * @param string $needle The value to look for.
233
 * @return Closure(string):bool
234
 */
235
function contains(string $needle): Closure
236
{
237
    /**
238
     * @param string $haystack String to look in.
239
     * @return bool
240
     */
241
    return function (string $haystack) use ($needle): bool {
242
        return \stringContains($haystack, $needle);
243
    };
244
}
245
246
/**
247
 * Creates a callable for checking if a string contains using preg_match.
248
 *
249
 * @param string $pattern
250
 * @return Closure(string):bool
251
 */
252
function containsPattern(string $pattern): Closure
253
{
254
    /**
255
     * @param string $source String to look in.
256
     * @return bool
257
     */
258
    return function (string $source) use ($pattern): bool {
259
        return (bool) \preg_match($pattern, $source);
260
    };
261
}
262
263
/**
264
 * Splits a string with a pattern
265
 *
266
 * @param string $pattern
267
 * @return Closure(string):?string[]
268
 */
269
function splitPattern(string $pattern): Closure
270
{
271
    /**
272
     * @param string $name
273
     * @return string[]
274
     */
275
    return function (string $string) use ($pattern): ?array {
276
        return \preg_split($pattern, $string) ?: null;
277
    };
278
}
279
280
/**
281
 * Converts a number (loose type) to a string representation of a float.
282
 *
283
 * @param string|int|float $precision Number of decimal places
284
 * @param string $point The decimal separator
285
 * @param string $thousands The thousand separator.
286
 * @return Closure(string|int|float):string
287
 */
288
function decimalNumber($precision = 2, $point = '.', $thousands = ''): Closure
289
{
290
    /**
291
     * @param string|int|float $number
292
     * @return string
293
     */
294
    return function ($number) use ($precision, $point, $thousands): string {
295
        return \is_numeric($number)
296
            ? \number_format((float) $number, (int) $precision, $point, $thousands)
297
            : '';
298
    };
299
}
300
301
302
303
/**
304
 * Returns a callable for adding C slashes to a string based on a defined char list.
305
 *
306
 * @param string $charList The Char list to add slashes too.
307
 * @return Closure(string):string
308
 */
309
function addSlashes(string $charList): Closure
310
{
311
    /**
312
     * @param string $string The string to have char, slash escaped.
313
     * @return string
314
     */
315
    return function (string $string) use ($charList): string {
316
        return \addcslashes($string, $charList);
317
    };
318
}
319
320
/**
321
 * Returns a callable for splitting a string by a set amount.
322
 *
323
 * @param int $length The length to split the string up with.
324
 * @return Closure(string):array<string> The parts.
325
 */
326
function split(int $length): Closure
327
{
328
    /**
329
     * @param string $string The string to be split
330
     * @return array<int, string>
331
     */
332
    return function (string $string) use ($length): array {
333
        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...
334
    };
335
}
336
337
/**
338
 * Returns a callback for splitting a string into chunks.
339
 *
340
 * @param int $length The length of each chunk.
341
 * @param string $end The string to use at the end.
342
 * @return Closure(string):string
343
 */
344
function chunk(int $length, string $end = "\r\n"): Closure
345
{
346
    /**
347
     * @param string $string The string to be chunked
348
     * @return string
349
     */
350
    return function (string $string) use ($length, $end): string {
351
        return \chunk_split($string, max(1, $length), $end);
352
    };
353
}
354
355
/**
356
 * Creates a callable for wrapping a string to a defined value.
357
 *
358
 * @param int $width Max width for each "line"
359
 * @param string $break The string to use to denote the end of line.
360
 * @param bool $cut If set to true, words are cut at $width, else overflow.
361
 * @return Closure(string):string
362
 */
363
function wordWrap(int $width, string $break = "\n", bool $cut = false): Closure
364
{
365
    /**
366
     * @param string $string The string to be wrapped
367
     * @return string
368
     */
369
    return function (string $string) use ($width, $break, $cut): string {
370
        return \wordwrap($string, $width, $break, $cut);
371
    };
372
}
373
374
/**
375
 * Returns a callback for counting the number of occurrences of each char in a string.
376
 *
377
 * @link https://www.php.net/manual/en/function.count-chars.php
378
 * @param int $mode See the PHP docs for details.
379
 * @return Closure(string):(int[]|string)
380
 */
381
function countChars(int $mode = 1): Closure
382
{
383
    // Throw an exception if the mode is not supported.
384
    if (! in_array($mode, array( 0, 1, 2, 3, 4 ), true)) {
385
        throw new \InvalidArgumentException('Invalid mode');
386
    }
387
388
    /**
389
     * @param string $string The string to have its char counted.
390
     * @return int[]|string
391
     */
392
    return function (string $string) use ($mode) {
393
        return \count_chars($string, $mode);
394
    };
395
}
396
397
/**
398
 * Returns a callable that counts the occurrences of a given substring in a string
399
 *
400
 * @param string $needle The substring to find
401
 * @param int $offset Place to start, defaults to 0 (start)
402
 * @param int|null $length Max length after offset to search.
403
 * @return Closure(string):int
404
 */
405
function countSubString(string $needle, int $offset = 0, ?int $length = null): Closure
406
{
407
    /**
408
     * @param string $haystack
409
     * @return int
410
     */
411
    return function (string $haystack) use ($needle, $offset, $length): int {
412
        return $length
413
            ? \substr_count($haystack, $needle, $offset, $length)
414
            : \substr_count($haystack, $needle, $offset);
415
    };
416
}
417
418
/**
419
 * Returns a callable for doing repeated trim.
420
 *
421
 * @param string $mask
422
 * @return Closure(string):string
423
 */
424
function trim(string $mask = "\t\n\r\0\x0B"): Closure
425
{
426
    /**
427
     * @param string $string The string to be trimmed
428
     * @return string
429
     */
430
    return function (string $string) use ($mask): string {
431
        return \trim($string, $mask);
432
    };
433
}
434
435
/**
436
 * Returns a callable for doing repeated ltrim.
437
 *
438
 * @param string $mask
439
 * @return Closure(string):string
440
 */
441
function lTrim(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 \ltrim($string, $mask);
449
    };
450
}
451
452
/**
453
 * Returns a callable for doing repeated rtrim.
454
 *
455
 * @param string $mask
456
 * @return Closure(string):string
457
 */
458
function rTrim(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 \rtrim($string, $mask);
466
    };
467
}
468
469
/**
470
 * Returns a callable for finding the similarities between 2 string.
471
 * This sets the defined value as the base (similar_text as first)
472
 *
473
 * @param string $base The string to act as the base.
474
 * @param bool $asPc If set to true will return the percentage match, rather than char count.
475
 * @return Closure(string):Number
476
 */
477
function similarAsBase(string $base, bool $asPc = false): Closure
478
{
479
    /**
480
     * @param string $comparisonString The string to compare against base.
481
     * @return Number
482
     */
483
    return function (string $comparisonString) use ($base, $asPc) {
484
        $pc       = 0.00;
485
        $matching = similar_text($base, $comparisonString, $pc);
486
        return $asPc ? $pc : $matching;
487
    };
488
}
489
490
/**
491
 * Returns a callable for finding the similarities between 2 string.
492
 * This sets the defined value as the comparisonString (similar_text as second)
493
 *
494
 * @param string $comparisonString The string to compare against base.
495
 * @param bool $asPc If set to true will return the percentage match, rather than char count.
496
 * @return Closure(string):Number
497
 */
498
function similarAsComparison(string $comparisonString, bool $asPc = false): Closure
499
{
500
    /**
501
     * @param string $comparisonString The string to act as the base.
502
     * @return Number
503
     */
504
    return function (string $base) use ($comparisonString, $asPc) {
505
        $pc       = 0.00;
506
        $matching = similar_text($base, $comparisonString, $pc);
507
        return $asPc ? $pc : $matching;
508
    };
509
}
510
511
/**
512
 * Returns a callable for padding out a string.
513
 *
514
 * @param int $length Max length to make string.
515
 * @param string $padContent The value to padd the string with (defaults to ' ')
516
 * @param int $type How to pad, please use these constants. STR_PAD_RIGHT|STR_PAD_LEFT|STR_PAD_BOTH
517
 * @return Closure(string):string
518
 */
519
function pad(int $length, string $padContent = ' ', int $type = STR_PAD_RIGHT): Closure
520
{
521
    /**
522
     * @param string $string The string to pad out.
523
     * @return string
524
     */
525
    return function (string $string) use ($length, $padContent, $type): string {
526
        return \str_pad($string, $length, $padContent, $type);
527
    };
528
}
529
530
/**
531
 * Returns a callable for repeating a string by a defined number of times.
532
 *
533
 * @param int $count Number of times to repeat string.
534
 * @return Closure(string):string
535
 */
536
function repeat(int $count): Closure
537
{
538
    /**
539
     * @param string $string The string to repeat
540
     * @return string
541
     */
542
    return function (string $string) use ($count): string {
543
        return \str_repeat($string, $count);
544
    };
545
}
546
547
/**
548
 * Returns a callback for creating a word counter, with set format and char list.
549
 *
550
 * @param int $format can use WORD_COUNT_NUMBER_OF_WORDS | WORD_COUNT_ARRAY | WORD_COUNT_ASSOCIATIVE_ARRAY
551
 * @param string|null $charList The char list of option values considered words.
552
 * @return Closure(string):(int|string[])
553
 */
554
function wordCount(int $format = WORD_COUNT_NUMBER_OF_WORDS, ?string $charList = null): Closure
555
{
556
    /**
557
     * @param string $string The string to pad out.
558
     * @return int|string[]
559
     */
560
    return function (string $string) use ($format, $charList) {
561
        return $charList
562
            ? (\str_word_count($string, $format, $charList) ?: 0)
563
            : (\str_word_count($string, $format) ?: 0);
564
    };
565
}
566
567
/**
568
 * Creates a function for stripping tags with a defined set of allowed tags.
569
 *
570
 * @param string|null $allowedTags The allowed tags, pass null or leave blank for none.
571
 * @return Closure(string):string
572
 */
573
function stripTags(?string $allowedTags = null): Closure
574
{
575
    /**
576
     * @param string $string The string to strip tags from.
577
     * @return string
578
     */
579
    return function (string $string) use ($allowedTags): string {
580
        return $allowedTags
581
            ? \strip_tags($string, $allowedTags)
582
            : \strip_tags($string);
583
    };
584
}
585
586
/**
587
 * Returns a callable for finding the first postition of a defined value in any string.
588
 *
589
 * @param string $needle The value to look for.
590
 * @param int  $offset The offset to start
591
 * @param int $flags STRINGS_CASE_SENSITIVE | STRINGS_CASE_INSENSITIVE
592
 * @return Closure(string):?int
593
 */
594
function firstPosition(string $needle, int $offset = 0, int $flags = STRINGS_CASE_SENSITIVE): Closure
595
{
596
    $caseSensitive = ! (bool) ($flags & STRINGS_CASE_INSENSITIVE); // Assumes true unless INSESNITVE passed
597
598
    /**
599
     * @param string $haystack The haystack to look through.
600
     * @return int|null
601
     */
602
    return function (string $haystack) use ($needle, $offset, $caseSensitive): ?int {
603
        $pos = $caseSensitive
604
            ? strpos($haystack, $needle, $offset)
605
            : stripos($haystack, $needle, $offset);
606
        return is_int($pos) ? (int) $pos : null;
0 ignored issues
show
introduced by
The condition is_int($pos) is always true.
Loading history...
607
    };
608
}
609
610
/**
611
 * Returns a callable for finding the first position of a defined value in any string.
612
 *
613
 * @param string $needle The value to look for.
614
 * @param int  $offset The offset to start
615
 * @param int $flags STRINGS_CASE_SENSITIVE | STRINGS_CASE_INSENSITIVE
616
 * @return Closure(string):?int
617
 */
618
function lastPosition(string $needle, int $offset = 0, int $flags = STRINGS_CASE_SENSITIVE): Closure
619
{
620
    $caseSensitive = ! (bool) ($flags & STRINGS_CASE_INSENSITIVE); // Assumes true unless INSESNITVE passed
621
622
    /**
623
     * @param string $haystack The haystack to look through.
624
     * @return int|null
625
     */
626
    return function (string $haystack) use ($needle, $offset, $caseSensitive): ?int {
627
        $pos = $caseSensitive
628
            ? strrpos($haystack, $needle, $offset)
629
            : strripos($haystack, $needle, $offset);
630
        return is_int($pos) ? (int) $pos : null;
0 ignored issues
show
introduced by
The condition is_int($pos) is always true.
Loading history...
631
    };
632
}
633
634
/**
635
 * Returns a callable for looking for the first occurrence of a substring.
636
 * When found can return all after or before the needle (sub string)
637
 * Can be done as case sensitive (strstr()) or insensitive (stristr())
638
 *
639
 * @param string $needle The substring to look for.
640
 * @param int $flags Possible STRINGS_CASE_INSENSITIVE | STRINGS_CASE_SENSITIVE | STRINGS_AFTER_NEEDLE | STRINGS_BEFORE_NEEDLE
641
 * @return Closure(string):string
642
 */
643
function firstSubString(string $needle, int $flags = STRINGS_CASE_SENSITIVE | STRINGS_AFTER_NEEDLE): Closure
644
{
645
646
    // Decode flags, only look for none defaults.
647
    $beforeNeedle  = (bool) ($flags & STRINGS_BEFORE_NEEDLE);
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 string
653
     */
654
    return function (string $haystack) use ($needle, $beforeNeedle, $caseSensitive): string {
655
        $result = $caseSensitive
656
            ? strstr($haystack, $needle, $beforeNeedle)
657
            : stristr($haystack, $needle, $beforeNeedle);
658
        return is_string($result) ? $result : '';
0 ignored issues
show
introduced by
The condition is_string($result) is always true.
Loading history...
659
    };
660
}
661
662
/**
663
 * Returns a callable for creating a function which finds the first occurrence of
664
 * any character (from a list) in a defined string.
665
 *
666
 * @param string $chars All chars to check with.
667
 * @return Closure(string):string
668
 */
669
function firstChar(string $chars): Closure
670
{
671
    /**
672
     * @param string $haystack
673
     * @return string
674
     */
675
    return function (string $haystack) use ($chars): string {
676
        $result = strpbrk($haystack, $chars);
677
        return is_string($result) ? $result : '';
0 ignored issues
show
introduced by
The condition is_string($result) is always true.
Loading history...
678
    };
679
}
680
681
/**
682
 * Returns a function that finds the last char in a string and returns the following text.
683
 * Matches the first char passed, if more than 1 char passed, the rest are ignored.
684
 *
685
 * @param string $char
686
 * @return Closure(string):string
687
 */
688
function lastChar(string $char): Closure
689
{
690
    /**
691
     * @param string $haystack
692
     * @return string
693
     */
694
    return function (string $haystack) use ($char): string {
695
        $result = strrchr($haystack, $char);
696
        return is_string($result) ? $result : '';
0 ignored issues
show
introduced by
The condition is_string($result) is always true.
Loading history...
697
    };
698
}
699
700
/**
701
 * Returns a callable which translates substrings from a defined dictionary.
702
 * Dictionary should be ['from' => 'to' ]
703
 *
704
 * @param array<string, mixed> $dictionary
705
 * @return Closure(string):string
706
 */
707
function translateWith(array $dictionary): Closure
708
{
709
    /**
710
     * @param string $haystack
711
     * @return string
712
     */
713
    return function (string $haystack) use ($dictionary): string {
714
        $result = strtr($haystack, $dictionary);
715
        return $result;
716
    };
717
}
718
719
/**
720
 * Creates a callable for a string safe function compose.
721
 *
722
 * @uses F\composeTypeSafe
723
 * @param callable(mixed):string ...$callable
724
 * @return Closure(mixed):string
725
 */
726
function composeSafeStringFunc(callable ...$callable): Closure
727
{
728
    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

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