Passed
Pull Request — develop (#76)
by Glynn
02:40
created

digit()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 1
nop 3
dl 0
loc 10
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 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.
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...
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 similarAsBase(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 comparisonString (similar_text as second)
512
 *
513
 * @param string $comparisonString The string to compare against base.
514
 * @param bool $asPc If set to true will return the percentage match, rather than char count.
515
 * @return Closure(string):Number
516
 */
517
function similarAsComparison(string $comparisonString, bool $asPc = false): Closure
518
{
519
    /**
520
     * @param string $comparisonString The string to act as the base.
521
     * @return Number
522
     */
523
    return function (string $base) use ($comparisonString, $asPc) {
524
        $pc       = 0.00;
525
        $matching = similar_text($base, $comparisonString, $pc);
526
        return $asPc ? $pc : $matching;
527
    };
528
}
529
530
/**
531
 * Returns a callable for padding out a string.
532
 *
533
 * @param int $length Max length to make string.
534
 * @param string $padContent The value to padd the string with (defaults to ' ')
535
 * @param int $type How to pad, please use these constants. STR_PAD_RIGHT|STR_PAD_LEFT|STR_PAD_BOTH
536
 * @return Closure(string):string
537
 */
538
function pad(int $length, string $padContent = ' ', int $type = STR_PAD_RIGHT): Closure
539
{
540
    /**
541
     * @param string $string The string to pad out.
542
     * @return string
543
     */
544
    return function (string $string) use ($length, $padContent, $type): string {
545
        return \str_pad($string, $length, $padContent, $type);
546
    };
547
}
548
549
/**
550
 * Returns a callable for repeating a string by a defined number of times.
551
 *
552
 * @param int $count Number of times to repeat string.
553
 * @return Closure(string):string
554
 */
555
function repeat(int $count): Closure
556
{
557
    /**
558
     * @param string $string The string to repeat
559
     * @return string
560
     */
561
    return function (string $string) use ($count): string {
562
        return \str_repeat($string, $count);
563
    };
564
}
565
566
/**
567
 * Returns a callback for creating a word counter, with set format and char list.
568
 *
569
 * @param int $format can use WORD_COUNT_NUMBER_OF_WORDS | WORD_COUNT_ARRAY | WORD_COUNT_ASSOCIATIVE_ARRAY
570
 * @param string|null $charList The char list of option values considered words.
571
 * @return Closure(string):(int|string[])
572
 */
573
function wordCount(int $format = WORD_COUNT_NUMBER_OF_WORDS, ?string $charList = null): Closure
574
{
575
    /**
576
     * @param string $string The string to pad out.
577
     * @return int|string[]
578
     */
579
    return function (string $string) use ($format, $charList) {
580
        return $charList
581
            ? (\str_word_count($string, $format, $charList) ?: 0)
582
            : (\str_word_count($string, $format) ?: 0);
583
    };
584
}
585
586
/**
587
 * Creates a function for stripping tags with a defined set of allowed tags.
588
 *
589
 * @param string|null $allowedTags The allowed tags, pass null or leave blank for none.
590
 * @return Closure(string):string
591
 */
592
function stripTags(?string $allowedTags = null): Closure
593
{
594
    /**
595
     * @param string $string The string to strip tags from.
596
     * @return string
597
     */
598
    return function (string $string) use ($allowedTags): string {
599
        return $allowedTags
600
            ? \strip_tags($string, $allowedTags)
601
            : \strip_tags($string);
602
    };
603
}
604
605
/**
606
 * Returns a callable for finding the first postition of a defined value in any string.
607
 *
608
 * @param string $needle The value to look for.
609
 * @param int  $offset The offset to start
610
 * @param int $flags STRINGS_CASE_SENSITIVE | STRINGS_CASE_INSENSITIVE
611
 * @return Closure(string):?int
612
 */
613
function firstPosition(string $needle, int $offset = 0, int $flags = STRINGS_CASE_SENSITIVE): Closure
614
{
615
    $caseSensitive = ! (bool) ($flags & STRINGS_CASE_INSENSITIVE); // Assumes true unless INSESNITVE passed
616
617
    /**
618
     * @param string $haystack The haystack to look through.
619
     * @return int|null
620
     */
621
    return function (string $haystack) use ($needle, $offset, $caseSensitive): ?int {
622
        $pos = $caseSensitive
623
            ? strpos($haystack, $needle, $offset)
624
            : stripos($haystack, $needle, $offset);
625
        return is_int($pos) ? (int) $pos : null;
0 ignored issues
show
introduced by
The condition is_int($pos) is always true.
Loading history...
626
    };
627
}
628
629
/**
630
 * Returns a callable for finding the first position of a defined value in any string.
631
 *
632
 * @param string $needle The value to look for.
633
 * @param int  $offset The offset to start
634
 * @param int $flags STRINGS_CASE_SENSITIVE | STRINGS_CASE_INSENSITIVE
635
 * @return Closure(string):?int
636
 */
637
function lastPosition(string $needle, int $offset = 0, int $flags = STRINGS_CASE_SENSITIVE): Closure
638
{
639
    $caseSensitive = ! (bool) ($flags & STRINGS_CASE_INSENSITIVE); // Assumes true unless INSESNITVE passed
640
641
    /**
642
     * @param string $haystack The haystack to look through.
643
     * @return int|null
644
     */
645
    return function (string $haystack) use ($needle, $offset, $caseSensitive): ?int {
646
        $pos = $caseSensitive
647
            ? strrpos($haystack, $needle, $offset)
648
            : strripos($haystack, $needle, $offset);
649
        return is_int($pos) ? (int) $pos : null;
0 ignored issues
show
introduced by
The condition is_int($pos) is always true.
Loading history...
650
    };
651
}
652
653
/**
654
 * Returns a callable for looking for the first occurrence of a substring.
655
 * When found can return all after or before the needle (sub string)
656
 * Can be done as case sensitive (strstr()) or insensitive (stristr())
657
 *
658
 * @param string $needle The substring to look for.
659
 * @param int $flags Possible STRINGS_CASE_INSENSITIVE | STRINGS_CASE_SENSITIVE | STRINGS_AFTER_NEEDLE | STRINGS_BEFORE_NEEDLE
660
 * @return Closure(string):string
661
 */
662
function firstSubString(string $needle, int $flags = STRINGS_CASE_SENSITIVE | STRINGS_AFTER_NEEDLE): Closure
663
{
664
    // Decode flags, only look for none defaults.
665
    $beforeNeedle  = (bool) ($flags & STRINGS_BEFORE_NEEDLE);
666
    $caseSensitive = ! (bool) ($flags & STRINGS_CASE_INSENSITIVE); // Assumes true unless INSESNITVE passed
667
668
    /**
669
     * @param string $haystack The haystack to look through.
670
     * @return string
671
     */
672
    return function (string $haystack) use ($needle, $beforeNeedle, $caseSensitive): string {
673
        $result = $caseSensitive
674
            ? strstr($haystack, $needle, $beforeNeedle)
675
            : stristr($haystack, $needle, $beforeNeedle);
676
        return is_string($result) ? $result : '';
0 ignored issues
show
introduced by
The condition is_string($result) is always true.
Loading history...
677
    };
678
}
679
680
/**
681
 * Returns a callable for creating a function which finds the first occurrence of
682
 * any character (from a list) in a defined string.
683
 *
684
 * @param string $chars All chars to check with.
685
 * @return Closure(string):string
686
 */
687
function firstChar(string $chars): Closure
688
{
689
    /**
690
     * @param string $haystack
691
     * @return string
692
     */
693
    return function (string $haystack) use ($chars): string {
694
        $result = strpbrk($haystack, $chars);
695
        return is_string($result) ? $result : '';
0 ignored issues
show
introduced by
The condition is_string($result) is always true.
Loading history...
696
    };
697
}
698
699
/**
700
 * Returns a function that finds the last char in a string and returns the following text.
701
 * Matches the first char passed, if more than 1 char passed, the rest are ignored.
702
 *
703
 * @param string $char
704
 * @return Closure(string):string
705
 */
706
function lastChar(string $char): Closure
707
{
708
    /**
709
     * @param string $haystack
710
     * @return string
711
     */
712
    return function (string $haystack) use ($char): string {
713
        $result = strrchr($haystack, $char);
714
        return is_string($result) ? $result : '';
0 ignored issues
show
introduced by
The condition is_string($result) is always true.
Loading history...
715
    };
716
}
717
718
/**
719
 * Returns a callable which translates substrings from a defined dictionary.
720
 * Dictionary should be ['from' => 'to' ]
721
 *
722
 * @param array<string, mixed> $dictionary
723
 * @return Closure(string):string
724
 */
725
function translateWith(array $dictionary): Closure
726
{
727
    /**
728
     * @param string $haystack
729
     * @return string
730
     */
731
    return function (string $haystack) use ($dictionary): string {
732
        $result = strtr($haystack, $dictionary);
733
        return $result;
734
    };
735
}
736
737
/**
738
 * Creates a callable for a string safe function compose.
739
 *
740
 * @uses F\composeTypeSafe
741
 * @param callable(mixed):string ...$callable
742
 * @return Closure(mixed):string
743
 */
744
function composeSafeStringFunc(callable ...$callable): Closure
745
{
746
    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

746
    return /** @scrutinizer ignore-call */ F\composeTypeSafe('is_string', ...$callable);
Loading history...
747
}
748
749
/**
750
 * Creates a callable for compiling a string.
751
 *
752
 * @param string $initial
753
 * @return Closure(string|null):(Closure|string)
754
 */
755
function stringCompiler(string $initial = ''): Closure
756
{
757
    /**
758
     * @param string|null $value
759
     * @return Closure|string
760
     */
761
    return function (?string $value = null) use ($initial) {
762
        if ($value) {
763
            $initial .= $value;
764
        }
765
        return $value ? stringCompiler($initial) : $initial;
766
    };
767
}
768
769
/**
770
 * Checks if the passe value is a string and if its length is 0.
771
 *
772
 * @param mixed $value
773
 * @return bool
774
 */
775
function isBlank($value): bool
776
{
777
    return is_string($value) && \mb_strlen($value) === 0;
778
}
779
780
781
/************************************************************************/
782
/************************************************************************/
783
/**                        Deprecated Functions                        **/
784
/************************************************************************/
785
/************************************************************************/
786
787
788
/**
789
 * See decimalNumber()
790
 * This function will be removed in later versions.
791
 *
792
 * @deprecated Use decimalNumber() instead.
793
 *
794
 * @param int $precision
795
 * @param string $point
796
 * @param string $thousands
797
 * @return Closure
798
 */
799
function decimialNumber($precision = 2, $point = '.', $thousands = ''): Closure
800
{
801
    trigger_error('Deprecated function called. Please use digit. This function will be removed in later versions.', E_USER_DEPRECATED);
802
    return digit($precision, $point, $thousands);
803
}
804
805
/**
806
 * Converts a number (loose type) to a string representation of a float.
807
 *
808
 * @param string|int|float $precision Number of decimal places
809
 * @param string $point The decimal separator
810
 * @param string $thousands The thousand separator.
811
 * @return Closure(string|int|float):string
812
 */
813
function decimalNumber($precision = 2, $point = '.', $thousands = ''): Closure
814
{
815
    trigger_error('Deprecated function called. Please use digit. This function will be removed in later versions.', E_USER_DEPRECATED);
816
    return digit($precision, $point, $thousands);
817
}
818
819
/**
820
 * See similarAsComparison()
821
 *
822
 * @deprecated Use similarAsComparison() instead.
823
 * @param string $comparisonString
824
 * @param bool $asPc
825
 * @return Closure
826
 */
827
function similarAsComparisson(string $comparisonString, bool $asPc = false): Closure
828
{
829
    trigger_error('Deprecated function called. Please use similarAsComparison. This function will be removed in later versions.', E_USER_DEPRECATED);
830
    return similarAsComparison($comparisonString, $asPc);
831
}
832
833
/**
834
 * See firstPosition()
835
 *
836
 * @deprecated
837
 * @param string $needle
838
 * @param int $offset
839
 * @param int $flags
840
 * @return Closure
841
 */
842
function firstPosistion(string $needle, int $offset = 0, int $flags = STRINGS_CASE_SENSITIVE): Closure
843
{
844
    trigger_error('Deprecated function called. Please use firstPosition. This function will be removed in later versions.', E_USER_DEPRECATED);
845
    return firstPosition($needle, $offset, $flags);
846
}
847
848
/**
849
 * See lastPosition()
850
 *
851
 * @deprecated Use lastPosition() instead.
852
 * @param string $needle The value to look for.
853
 * @param int  $offset The offset to start
854
 * @param int $flags STRINGS_CASE_SENSITIVE | STRINGS_CASE_INSENSITIVE
855
 * @return Closure
856
 */
857
function lastPosistion(string $needle, int $offset = 0, int $flags = STRINGS_CASE_SENSITIVE): Closure
858
{
859
    trigger_error('Deprecated function called. Please use lastPosition. This function will be removed in later versions.', E_USER_DEPRECATED);
860
    return lastPosition($needle, $offset, $flags);
861
}
862