-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
178 lines (159 loc) · 3.52 KB
/
functions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
declare(strict_types=1);
/**
* @template TClamp of int|float
*
* @param TClamp $number
* @param TClamp $min
* @param TClamp $max
*
* @return TClamp
*
* Remove when https://wiki.php.net/rfc/clamp is merged
*/
function clamp(int|float $number, int|float $min, int|float $max): int|float
{
if ($number > $max) {
return $max;
}
if ($number < $min) {
return $min;
}
return $number;
}
/**
* @template T
*
* @param iterable<array-key, T> $input
* @param Closure(T): bool $criteria
*
* @return ?T
*/
function find_first(iterable $input, Closure $criteria)
{
foreach ($input as $item) {
if ($criteria($item)) {
return $item;
}
}
return null;
}
/**
* Applied callback on each element of iterable, and returns Generator of results.
*
* Useful for big data processing, and cleaner solution than @see array_map()
*
* @template T
* @template R
*
* @param iterable<T> $input
* @param Closure(T): R $callback
*
* @return Generator<array-key, R>
*/
function iterable_map(iterable $input, Closure $callback): Generator
{
foreach ($input as $item) {
yield $callback($item);
}
}
/**
* @template TK
* @template TV
*
* @param iterable<TK, TV> $input
*
* @return array<TK, TV>
*/
function iterable_to_array(iterable $input): array
{
$out = [];
foreach ($input as $key => $value) {
$out[$key] = $value;
}
return $out;
}
/**
* Function useful to generate `Did you mean...` type of messages when exceptions are thrown.
* Uses 1/3 length tolerance like other cases inside Symfony.
*
* @param iterable<string> $haystack
*/
function closest(string $needle, iterable $haystack): ?string
{
$tolerance = strlen($needle) / 3;
$closestLevel = strlen($needle);
$suggestion = null;
foreach ($haystack as $item) {
$distance = levenshtein($needle, $item);
if ($distance < $closestLevel && $distance < $tolerance) {
$closestLevel = $distance;
$suggestion = $item;
}
}
return $suggestion;
}
/**
* @template T
*
* @param list<T> $input
* @param Closure(T): string $comparator
*
* @return list<T>
*/
function array_unique_comparable(array $input, Closure $comparator): array
{
$unique = [];
foreach ($input as $item) {
$identifier = $comparator($item);
if (!isset($unique[$identifier])) {
$unique[$identifier] = $item;
}
}
return array_values($unique);
}
/**
* This is a fix for original function that only does (string)$object comparisons: https://www.php.net/manual/en/function.array-unique.php
*
* @template TU of object
*
* @param array<array-key, TU> $entities
*
* @return list<TU>
*/
function array_unique_objects(array $entities): array
{
$result = [];
foreach ($entities as $entity) {
if (!in_array($entity, $result, true)) {
$result[] = $entity;
}
}
return $result;
}
function unless(bool $condition, Closure $callback): void
{
if (!$condition) {
$callback();
}
}
/**
* @param list<?string> $parts
*/
function implode_non_empty(string $separator, array $parts): ?string
{
$filtered = array_filter($parts, fn(?string $part) => (bool)$part);
return implode($separator, $filtered) ?: null;
}
/**
* @param non-empty-list<array-key> $lookup
*/
function any_key_exists(array $lookup, array $haystack): bool
{
foreach ($lookup as $item) {
if (array_key_exists($item, $haystack)) {
return true;
}
}
return false;
}