|
2 | 2 |
|
3 | 3 | declare(strict_types=1); |
4 | 4 |
|
| 5 | +namespace PhpSchool\PhpWorkshop; |
| 6 | + |
5 | 7 | use PhpSchool\PhpWorkshop\Utils\Collection; |
6 | 8 | use PhpSchool\PhpWorkshop\Utils\StringUtils; |
7 | 9 |
|
8 | | -if (!function_exists('mb_str_pad')) { |
9 | | - |
10 | | - /** |
11 | | - * @param string $input |
12 | | - * @param int $padLength |
13 | | - * @param string $padString |
14 | | - * @param int $padType |
15 | | - * @return string |
16 | | - */ |
17 | | - function mb_str_pad(string $input, int $padLength, string $padString = ' ', int $padType = STR_PAD_RIGHT): string |
18 | | - { |
19 | | - $diff = strlen($input) - mb_strlen($input); |
20 | | - return str_pad($input, $padLength + $diff, $padString, $padType); |
21 | | - } |
| 10 | +/** |
| 11 | + * @param string $input |
| 12 | + * @param int $padLength |
| 13 | + * @param string $padString |
| 14 | + * @param int $padType |
| 15 | + * @return string |
| 16 | + */ |
| 17 | +function mb_str_pad(string $input, int $padLength, string $padString = ' ', int $padType = STR_PAD_RIGHT): string |
| 18 | +{ |
| 19 | + $diff = strlen($input) - mb_strlen($input); |
| 20 | + return str_pad($input, $padLength + $diff, $padString, $padType); |
22 | 21 | } |
23 | 22 |
|
24 | | -if (!function_exists('camel_case_to_kebab_case')) { |
25 | | - |
26 | | - /** |
27 | | - * @param string $string |
28 | | - * @return string |
29 | | - */ |
30 | | - function camel_case_to_kebab_case(string $string): string |
31 | | - { |
32 | | - return (string) preg_replace_callback('/[A-Z]/', function ($matches) { |
33 | | - return '-' . strtolower($matches[0]); |
34 | | - }, $string); |
35 | | - } |
| 23 | +/** |
| 24 | + * @param string $string |
| 25 | + * @return string |
| 26 | + */ |
| 27 | +function camel_case_to_kebab_case(string $string): string |
| 28 | +{ |
| 29 | + return (string) preg_replace_callback('/[A-Z]/', function ($matches) { |
| 30 | + return '-' . strtolower($matches[0]); |
| 31 | + }, $string); |
36 | 32 | } |
37 | 33 |
|
38 | | -if (!function_exists('canonicalise_path')) { |
39 | | - |
40 | | - /** |
41 | | - * @param string $path |
42 | | - * @return string |
43 | | - */ |
44 | | - function canonicalise_path(string $path): string |
45 | | - { |
46 | | - return StringUtils::canonicalisePath($path); |
47 | | - } |
| 34 | +/** |
| 35 | + * @param string $path |
| 36 | + * @return string |
| 37 | + */ |
| 38 | +function canonicalise_path(string $path): string |
| 39 | +{ |
| 40 | + return StringUtils::canonicalisePath($path); |
48 | 41 | } |
49 | 42 |
|
50 | | -if (!function_exists('pluralise')) { |
51 | | - |
52 | | - /** |
53 | | - * @param string $string |
54 | | - * @param array<mixed> $items |
55 | | - * @param string[] ...$args |
56 | | - * @return string |
57 | | - */ |
58 | | - function pluralise(string $string, array $items, string ...$args): string |
59 | | - { |
60 | | - return StringUtils::pluralise($string, $items, ...$args); |
61 | | - } |
| 43 | +/** |
| 44 | + * @param string $string |
| 45 | + * @param array<mixed> $items |
| 46 | + * @param string[] ...$args |
| 47 | + * @return string |
| 48 | + */ |
| 49 | +function pluralise(string $string, array $items, string ...$args): string |
| 50 | +{ |
| 51 | + return StringUtils::pluralise($string, $items, ...$args); |
62 | 52 | } |
63 | 53 |
|
64 | | -if (!function_exists('collect')) { |
65 | | - |
66 | | - /** |
67 | | - * @template TKey of array-key |
68 | | - * @template T |
69 | | - * @param array<TKey, T> $array |
70 | | - * @return Collection<TKey, T> |
71 | | - */ |
72 | | - function collect(array $array): Collection |
73 | | - { |
74 | | - /** @var Collection<TKey, T> $collection */ |
75 | | - $collection = new Collection($array); |
76 | | - return $collection; |
77 | | - } |
| 54 | +/** |
| 55 | + * @template TKey of array-key |
| 56 | + * @template T |
| 57 | + * @param array<TKey, T> $array |
| 58 | + * @return Collection<TKey, T> |
| 59 | + */ |
| 60 | +function collect(array $array): Collection |
| 61 | +{ |
| 62 | + /** @var Collection<TKey, T> $collection */ |
| 63 | + $collection = new Collection($array); |
| 64 | + return $collection; |
78 | 65 | } |
79 | 66 |
|
80 | | - |
81 | | -if (!function_exists('any')) { |
82 | | - |
83 | | - /** |
84 | | - * @param array<mixed> $values |
85 | | - * @param callable $cb |
86 | | - * @return bool |
87 | | - */ |
88 | | - function any(array $values, callable $cb): bool |
89 | | - { |
90 | | - return array_reduce($values, function (bool $carry, $value) use ($cb) { |
91 | | - return $carry || $cb($value); |
92 | | - }, false); |
93 | | - } |
| 67 | +/** |
| 68 | + * @param array<mixed> $values |
| 69 | + * @param callable $cb |
| 70 | + * @return bool |
| 71 | + */ |
| 72 | +function any(array $values, callable $cb): bool |
| 73 | +{ |
| 74 | + return array_reduce($values, function (bool $carry, $value) use ($cb) { |
| 75 | + return $carry || $cb($value); |
| 76 | + }, false); |
94 | 77 | } |
0 commit comments