1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace BenTools\OpenCubes; |
5
|
|
|
|
6
|
|
|
use BenTools\UriFactory\UriFactory; |
7
|
|
|
use BenTools\UriFactory\UriFactoryInterface; |
8
|
|
|
use Psr\Http\Message\UriInterface; |
9
|
|
|
use function BenTools\QueryString\query_string; |
10
|
|
|
use function BenTools\QueryString\withoutNumericIndices; |
11
|
|
|
use function BenTools\UriFactory\Helper\uri; |
12
|
|
|
|
13
|
|
|
function is_sequential_array($array): bool |
14
|
|
|
{ |
15
|
|
|
if (!is_array($array)) { |
16
|
|
|
return false; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
return array_keys($array) === range(0, count($array) - 1); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
function is_indexed_array($array): bool |
23
|
|
|
{ |
24
|
|
|
if (!is_array($array)) { |
25
|
|
|
return false; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
try { |
29
|
|
|
(function (int...$values) { |
30
|
|
|
return $values; |
31
|
|
|
})(...array_keys($array)); |
32
|
|
|
return true; |
33
|
|
|
} catch (\TypeError $e) { |
34
|
|
|
return false; |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param iterable $iterable |
40
|
|
|
* @return bool |
41
|
|
|
*/ |
42
|
|
|
function contains_only_scalars(iterable $iterable): bool |
43
|
|
|
{ |
44
|
|
|
foreach ($iterable as $item) { |
45
|
|
|
if (!is_scalar($item)) { |
46
|
|
|
return false; |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
return true; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param iterable $iterable |
54
|
|
|
* @return bool |
55
|
|
|
*/ |
56
|
|
|
function contains_only_integers(iterable $iterable): bool |
57
|
|
|
{ |
58
|
|
|
foreach ($iterable as $item) { |
59
|
|
|
if (!is_int($item)) { |
60
|
|
|
return false; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
return true; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param UriFactoryInterface|null $factory |
68
|
|
|
* @return UriInterface |
69
|
|
|
* @throws \RuntimeException |
70
|
|
|
*/ |
71
|
|
|
function current_location(UriFactoryInterface $factory = null): UriInterface |
72
|
|
|
{ |
73
|
|
|
if ('cli' === php_sapi_name()) { |
74
|
|
|
return uri('/'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return UriFactory::factory()->createUriFromCurrentLocation($factory); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param $value |
82
|
|
|
* @return object |
83
|
|
|
*/ |
84
|
|
|
function cast($value) |
85
|
|
|
{ |
86
|
|
|
return new class($value) |
87
|
|
|
{ |
88
|
|
|
private $value; |
89
|
|
|
|
90
|
|
|
public function __construct(?string $value) |
91
|
|
|
{ |
92
|
|
|
$this->value = $value; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return int |
97
|
|
|
*/ |
98
|
|
|
public function asInt(): int |
99
|
|
|
{ |
100
|
|
|
return (int) $this->value; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return int|null |
105
|
|
|
*/ |
106
|
|
|
public function asIntOrNull(): ?int |
107
|
|
|
{ |
108
|
|
|
if (0 === strlen((string) $this->value)) { |
109
|
|
|
return null; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return (int) $this->value; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return float |
117
|
|
|
*/ |
118
|
|
|
public function asFloat(): float |
119
|
|
|
{ |
120
|
|
|
return (float) $this->value; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @return float|null |
125
|
|
|
*/ |
126
|
|
|
public function asFloatOrNull(): ?float |
127
|
|
|
{ |
128
|
|
|
if (0 === strlen((string) $this->value)) { |
129
|
|
|
return null; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return (float) $this->value; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @return string |
137
|
|
|
*/ |
138
|
|
|
public function asString(): string |
139
|
|
|
{ |
140
|
|
|
return (string) $this->value; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @return string|null |
145
|
|
|
*/ |
146
|
|
|
public function asStringOrNull(): ?string |
147
|
|
|
{ |
148
|
|
|
if (0 === strlen((string) $this->value)) { |
149
|
|
|
return null; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return (string) $this->value; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @return bool |
157
|
|
|
*/ |
158
|
|
|
public function asBool(): bool |
159
|
|
|
{ |
160
|
|
|
return filter_var($this->value, FILTER_VALIDATE_BOOLEAN); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @return bool|null |
165
|
|
|
*/ |
166
|
|
|
public function asBoolOrNull(): ?bool |
167
|
|
|
{ |
168
|
|
|
if (0 === strlen((string) $this->value)) { |
169
|
|
|
return null; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
return filter_var($this->value, FILTER_VALIDATE_BOOLEAN); |
173
|
|
|
} |
174
|
|
|
}; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @param UriInterface|null $uri |
179
|
|
|
* @return string|null |
180
|
|
|
*/ |
181
|
|
|
function stringify_uri(?UriInterface $uri): ?string |
182
|
|
|
{ |
183
|
|
|
if (null === $uri) { |
184
|
|
|
return null; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
$uri = $uri->withQuery( |
188
|
|
|
(string) query_string($uri)->withRenderer(withoutNumericIndices()) |
189
|
|
|
); |
190
|
|
|
|
191
|
|
|
return rawurldecode((string) $uri); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @param array $array |
196
|
|
|
* @return array |
197
|
|
|
*/ |
198
|
|
|
function remove_null_values(array $array) |
199
|
|
|
{ |
200
|
|
|
return array_diff($array, array_filter($array, 'is_null')); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @param iterable $items |
205
|
|
|
* @return mixed |
206
|
|
|
* @throws \InvalidArgumentException |
207
|
|
|
*/ |
208
|
|
|
function first_of(iterable $items, bool $throwErrorWhenEmpty = false) |
209
|
|
|
{ |
210
|
|
|
foreach ($items as $item) { |
211
|
|
|
return $item; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
if (true === $throwErrorWhenEmpty) { |
215
|
|
|
throw new \InvalidArgumentException('Expected at least 1 item'); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
return null; |
219
|
|
|
} |
220
|
|
|
|