Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayDataFilterTrait.php
More file actions
Latest commit
61 lines (55 loc) · 1.5 KB
/
Copy pathArrayDataFilterTrait.php
File metadata and controls
61 lines (55 loc) · 1.5 KB
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
<?php
/*
* This file is part of the Koded package.
*
* (c) Mihail Binev <mihail@kodeart.com>
*
* Please view the LICENSE distributed with this source code
* for the full copyright and license information.
*/
namespaceKoded\Stdlib;
usefunctionarray_key_exists;
usefunctionexplode;
usefunctionstr_replace;
usefunctionstr_starts_with;
usefunctionstrtolower;
trait ArrayDataFilterTrait
{
publicfunctionfilter(
iterable$data,
string$prefix,
bool$lowercase = true,
bool$trim = true): array
{
$filtered = [];
foreach ($dataas$index => $value) {
if ($trim && '' !== $prefix && str_starts_with($index, $prefix)) {
$index = str_replace($prefix, '', $index);
}
$filtered[$lowercase ? strtolower($index) : $index] = $value;
}
return$filtered;
}
publicfunctionfind(string$index, mixed$default = null): mixed
{
if (isset($this->data[$index])) {
return$this->data[$index];
}
$data = $this->data;
foreach (explode('.', $index) as$token) {
if (false === array_key_exists($token, $data)) {
return$default;
}
$data =& $data[$token];
}
return$data;
}
publicfunctionextract(array$indexes): array
{
$found = [];
foreach ($indexesas$index) {
$found[$index] = $this->data[$index] ?? null;
}
return$found;
}
}