-
-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathAccessHelper.php
More file actions
179 lines (144 loc) · 4.75 KB
/
Copy pathAccessHelper.php
File metadata and controls
179 lines (144 loc) · 4.75 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
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
179
<?php
/**
* JSONPath implementation for PHP.
*
* @license https://github.com/SoftCreatR/JSONPath/blob/main/LICENSE MIT License
*/
declare(strict_types=1);
namespace Flow\JSONPath;
use ArrayAccess;
use Traversable;
class AccessHelper
{
/**
* @return array<int, int|string>
*/
public static function collectionKeys(mixed $collection): array
{
if (\is_object($collection)) {
return \array_keys(\get_object_vars($collection));
}
return \array_keys($collection);
}
public static function isCollectionType(mixed $collection): bool
{
return \is_array($collection) || \is_object($collection);
}
public static function keyExists(mixed $collection, int|string|null $key, bool $magicIsAllowed = false): bool
{
if ($magicIsAllowed && \is_object($collection) && \method_exists($collection, '__get')) {
return true;
}
if (\is_array($collection)) {
if (\is_int($key) && $key < 0) {
$keys = \array_keys($collection);
$index = \count($keys) + $key;
return $index >= 0 && \array_key_exists($index, $keys);
}
return \array_key_exists($key ?? '', $collection);
}
if ($collection instanceof ArrayAccess) {
return $collection->offsetExists($key);
}
if (\is_object($collection)) {
return \property_exists($collection, (string)$key);
}
return false;
}
/**
* @todo Optimize conditions
*/
public static function getValue(mixed $collection, int|string|null $key, bool $magicIsAllowed = false): mixed
{
if (
$magicIsAllowed
&& \is_object($collection)
&& !$collection instanceof ArrayAccess && \method_exists($collection, '__get')
) {
$return = $collection->__get($key);
} elseif (\is_int($key) && $collection instanceof Traversable && !$collection instanceof ArrayAccess) {
$return = self::getValueByIndex($collection, $key);
} elseif (\is_object($collection) && !$collection instanceof ArrayAccess) {
$return = $collection->{$key};
} elseif ($collection instanceof ArrayAccess) {
$return = $collection->offsetExists($key) ? $collection->offsetGet($key) : null;
} elseif (\is_array($collection)) {
if (\is_int($key) && $key < 0) {
$index = \count($collection) + $key;
$return = $index >= 0 && \array_key_exists($index, $collection) ? $collection[$index] : null;
} else {
$return = $collection[$key] ?? null;
}
} else {
$return = null;
}
return $return;
}
/**
* Find item in php collection by index
* Written this way to handle instances ArrayAccess or Traversable objects
*/
private static function getValueByIndex(mixed $collection, int $key): mixed
{
$i = 0;
foreach ($collection as $val) {
if ($i === $key) {
return $val;
}
$i++;
}
if ($key < 0) {
$total = $i;
$i = 0;
foreach ($collection as $val) {
if ($i - $total === $key) {
return $val;
}
$i++;
}
}
return null;
}
public static function setValue(mixed &$collection, int|string|null $key, mixed $value): mixed
{
if (\is_object($collection) && !$collection instanceof ArrayAccess) {
$collection->{$key} = $value;
return $value;
}
if ($collection instanceof ArrayAccess) {
$collection->offsetSet($key, $value);
return $value;
}
$collection[$key] = $value;
return $value;
}
public static function unsetValue(mixed &$collection, int|string|null $key): void
{
if (\is_object($collection) && !$collection instanceof ArrayAccess) {
unset($collection->{$key});
}
if ($collection instanceof ArrayAccess) {
$collection->offsetUnset($key);
}
if (\is_array($collection)) {
unset($collection[$key]);
}
}
/**
* @throws JSONPathException
*/
/**
* @return array<int, mixed>
* @throws JSONPathException
*/
public static function arrayValues(mixed $collection): array
{
if (\is_array($collection)) {
return \array_values($collection);
}
if (\is_object($collection)) {
return \array_values((array)$collection);
}
throw new JSONPathException('Invalid variable type for arrayValues');
}
}