-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmixed.php
183 lines (174 loc) · 5.66 KB
/
mixed.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
179
180
181
182
183
<?php
/**
* Unified interface for checking if property exists an object or if a key exists on an array.
*
* @param array|object $data
* @param string $key
* @return bool
*/
function hasField ($data, $key)
{
if (is_object ($data))
return property_exists ($data, $key) || ($data instanceof ArrayAccess && isset($data[$key]));
if (is_array ($data))
return array_key_exists ($key, $data);
throw new \InvalidArgumentException;
}
/**
* Unified interface for retrieving a value by property from an object or by key from an array.
*
* @param array|object $data
* @param string $key
* @param mixed $default Value to return if the key doesn't exist.
* @return mixed
*/
function getField ($data, $key, $default = null)
{
if (is_object ($data)) {
if (property_exists ($data, $key))
return $data->$key;
if ($data instanceof ArrayAccess && isset($data[$key]))
return $data[$key];
return $default;
}
if (is_array ($data))
return array_key_exists ($key, $data) ? $data[$key] : $default;
throw new \InvalidArgumentException;
}
/**
* Unified interface to set a value on an object's property or at an array's key.
*
* @param array|object $data
* @param string $key
* @param mixed $value
*/
function setField (&$data, $key, $value)
{
if (is_object ($data))
$data->$key = $value;
else if (is_array ($data))
$data[$key] = $value;
else throw new \InvalidArgumentException;
}
/**
* Extracts values having specific keys from the given array/object.
*
* @param array|object|null $target
* @param array $keys A list of keys to be extracted.
* @return array A map of keys to extracted values or an empty array if $target is null.
* @throws InvalidArgumentException If the target is not an array, object or null.
*/
function getFields ($target, array $keys)
{
if (is_array ($target))
return array_only ($target, $keys);
if (is_object ($target))
return object_only ($target, $keys);
if (is_null ($target))
return [];
throw new InvalidArgumentException ("Not an object or array");
}
/**
* Unified interface for retrieving a reference to an object's property or to an array's element.
*
* If the key doesn't exist, it is initialized to a null value.
*
* @param mixed $data
* @param string $key
* @param mixed $default Value to store at the specified key if that key doesn't exist.
* Valid ony if `$createObj == false` (the default).
* @param bool $createObj When true, the `$default` is ignored and a new instance of StdClass is used instead.<br>
* This avoids unnecessary object instantiations.
* @return mixed Reference to the value.
* @throws InvalidArgumentException
*/
function & getFieldRef (&$data, $key, $default = null, $createObj = false)
{
if (is_object ($data)) {
if (!property_exists ($data, $key))
$data->$key = $createObj ? new StdClass : $default;
return $data->$key;
}
if (is_array ($data)) {
if (!array_key_exists ($key, $data))
$data[$key] = $default;
return $data[$key];
}
throw new InvalidArgumentException ("Not an object or array");
}
/**
* Unified interface for retrieving a value by property name from an object or by key name from an array, using a
* dot-delimited path to navigate a given data structure.
*
* @param array|object $data The target data structure.
* @param string $path A dot-delimited path.
* @param mixed $def [optional] Default value if the key/property is missing or its value is null.
* @return mixed|null
*/
function getAt ($data, $path, $def = null)
{
$segs = $path === '' ? [] : explode ('.', $path);
$cur = $data;
foreach ($segs as $seg) {
if (is_null ($cur = getField ($cur, $seg))) break;;
}
return isset($cur) ? $cur : $def;
}
/**
* Unified interface for retrieving a reference by property name from an object or by key name from an array, using a
* dot-delimited path to navigate a given data structure.
*
* @param array|object $data The target data structure.
* @param string $path A dot-delimited path.
* @return mixed|null
*/
function & getRefAt (& $data, $path)
{
$segs = $path === '' ? [] : explode ('.', $path);
$cur = $data;
foreach ($segs as $seg) {
if (is_null ($cur =& getFieldRef ($cur, $seg))) break;;
}
return $cur;
}
/**
* Unified interface for setting a value by property name on an object or by key name on an array, using a
* dot-delimited path to navigate a given data structure.
*
* @param array|object $data The target data structure.
* @param string $path A dot-delimited path.
* @param mixed $v The value.
* @param bool $assoc true if arrays should be provided for missing path nodes, otherwise objects will be
* created.
*/
function setAt (& $data, $path, $v, $assoc = false)
{
$segs = $path === '' ? [] : explode ('.', $path);
$cur =& $data;
foreach ($segs as $seg)
$cur =& getFieldRef ($cur, $seg, [], !$assoc);
$cur = $v;
}
/**
* Unified interface for unsetting a value by property name on an object or by key name on an array, using a
* dot-delimited path to navigate a given data structure.
*
* @param array|object $data The target data structure.
* @param string $path A dot-delimited path.
*/
function unsetAt (& $data, $path)
{
if ($path === '')
$data = is_array($data) ? [] : (object)[];
else {
$paths =explode ('.', $path);
$key = array_pop ($paths);
$path = implode ('.', $paths);
$v =& getRefAt ($data, $path);
if (is_array ($v))
unset ($v[$key]);
else if (is_object ($v))
unset ($v->$key);
else throw new InvalidArgumentException ("Not an object or array");
}
}