Debian Codegraph @

lib/3rdParty/composer/mtdowling/jmespath.php/src/Utils.php

raw symbols SCIP
1 <?php
2 namespace JmesPath;
3
4 class Utils
5 {
6 public static $typeMap = [
7 'boolean' => 'boolean',
8 'string' => 'string',
9 'NULL' => 'null',
10 'double' => 'number',
11 'float' => 'number',
12 'integer' => 'number'
13 ];
14
15 /**
16 * Returns true if the value is truthy
17 *
18 * @param mixed $value Value to check
19 *
20 * @return bool
21 */
22 public static function isTruthy($value)
23 {
24 if (!$value) {
25 return $value === 0 || $value === 0.0 || $value === '0';
26 } elseif ($value instanceof \stdClass) {
27 return (bool) get_object_vars($value);
28 } else {
29 return true;
30 }
31 }
32
33 /**
34 * Gets the JMESPath type equivalent of a PHP variable.
35 *
36 * @param mixed $arg PHP variable
37 * @return string Returns the JSON data type
38 * @throws \InvalidArgumentException when an unknown type is given.
39 */
40 public static function type($arg)
41 {
42 $type = gettype($arg);
43 if (isset(self::$typeMap[$type])) {
44 return self::$typeMap[$type];
45 } elseif ($type === 'array') {
46 if (empty($arg)) {
47 return 'array';
48 }
49 reset($arg);
50 return key($arg) === 0 ? 'array' : 'object';
51 } elseif ($arg instanceof \stdClass) {
52 return 'object';
53 } elseif ($arg instanceof \Closure) {
54 return 'expression';
55 } elseif ($arg instanceof \ArrayAccess
56 && $arg instanceof \Countable
57 ) {
58 return count($arg) == 0 || $arg->offsetExists(0)
59 ? 'array'
60 : 'object';
61 } elseif (is_object($arg)) {
62 if (method_exists($arg, '__toString')) {
63 return 'string';
64 }
65
66 throw new \InvalidArgumentException(
67 'Unable to determine JMESPath type from ' . get_class($arg)
68 );
69 }
70
71 throw new \InvalidArgumentException(
72 'Unable to determine JMESPath type from ' . gettype($arg)
73 );
74 }
75
76 /**
77 * Determine if the provided value is a JMESPath compatible object.
78 *
79 * @param mixed $value
80 *
81 * @return bool
82 */
83 public static function isObject($value)
84 {
85 if (is_array($value)) {
86 return !$value || array_keys($value)[0] !== 0;
87 }
88
89 // Handle array-like values. Must be empty or offset 0 does not exist
90 return $value instanceof \Countable && $value instanceof \ArrayAccess
91 ? count($value) == 0 || !$value->offsetExists(0)
92 : $value instanceof \stdClass;
93 }
94
95 /**
96 * Determine if the provided value is a JMESPath compatible array.
97 *
98 * @param mixed $value
99 *
100 * @return bool
101 */
102 public static function isArray($value)
103 {
104 if (is_array($value)) {
105 return !$value || array_keys($value)[0] === 0;
106 }
107
108 // Handle array-like values. Must be empty or offset 0 exists.
109 return $value instanceof \Countable && $value instanceof \ArrayAccess
110 ? count($value) == 0 || $value->offsetExists(0)
111 : false;
112 }
113
114 /**
115 * JSON-semantic equality: one number type, structural comparison for arrays
116 * and objects, and no object key-order sensitivity.
117 * Empty arrays and empty objects compare equal because PHP cannot represent
118 * that distinction after associative JSON decoding.
119 *
120 * @param mixed $a First value to compare
121 * @param mixed $b Second value to compare
122 *
123 * @return bool
124 */
125 public static function isEqual($a, $b)
126 {
127 $typeA = self::type($a);
128 $typeB = self::type($b);
129
130 if ($typeA !== $typeB) {
131 return ($typeA === 'array' || $typeA === 'object')
132 && ($typeB === 'array' || $typeB === 'object')
133 && (array) $a === []
134 && (array) $b === [];
135 }
136
137 if ($typeA === 'number') {
138 return $a == $b;
139 }
140
141 if ($typeA === 'array' || $typeA === 'object') {
142 $a = (array) $a;
143 $b = (array) $b;
144
145 if (count($a) !== count($b)) {
146 return false;
147 }
148
149 foreach ($a as $key => $value) {
150 if (!array_key_exists($key, $b) || !self::isEqual($value, $b[$key])) {
151 return false;
152 }
153 }
154
155 return true;
156 }
157
158 return $a === $b;
159 }
160
161 /**
162 * Safely add together two values.
163 *
164 * @param mixed $a First value to add
165 * @param mixed $b Second value to add
166 *
167 * @return int|float
168 */
169 public static function add($a, $b)
170 {
171 if (is_numeric($a)) {
172 if (is_numeric($b)) {
173 return $a + $b;
174 } else {
175 return $a;
176 }
177 } else {
178 if (is_numeric($b)) {
179 return $b;
180 } else {
181 return 0;
182 }
183 }
184 }
185
186 /**
187 * JMESPath requires a stable sorting algorithm, so here we'll implement
188 * a simple Schwartzian transform that uses array index positions as tie
189 * breakers.
190 *
191 * @param array $data List or map of data to sort
192 * @param callable $sortFn Callable used to sort values
193 *
194 * @return array Returns the sorted array
195 * @link http://en.wikipedia.org/wiki/Schwartzian_transform
196 */
197 public static function stableSort(array $data, callable $sortFn)
198 {
199 // Decorate each item by creating an array of [value, index]
200 array_walk($data, function (&$v, $k) {
201 $v = [$v, $k];
202 });
203 // Sort by the sort function and use the index as a tie-breaker
204 uasort($data, function ($a, $b) use ($sortFn) {
205 return $sortFn($a[0], $b[0]) ?: ($a[1] < $b[1] ? -1 : 1);
206 });
207
208 // Undecorate each item and return the resulting sorted array
209 return array_map(function ($v) {
210 return $v[0];
211 }, array_values($data));
212 }
213
214 /**
215 * Creates a Python-style slice of a string or array.
216 *
217 * @param array|string $value Value to slice
218 * @param int|null $start Starting position
219 * @param int|null $stop Stop position
220 * @param int $step Step (1, 2, -1, -2, etc.)
221 *
222 * @return array|string
223 * @throws \InvalidArgumentException
224 */
225 public static function slice($value, $start = null, $stop = null, $step = 1)
226 {
227 if (!is_string($value) && !self::isArray($value)) {
228 throw new \InvalidArgumentException('Expects string or array');
229 }
230
231 return self::sliceIndices($value, $start, $stop, $step);
232 }
233
234 private static function adjustEndpoint($length, $endpoint, $step)
235 {
236 if ($endpoint < 0) {
237 $endpoint += $length;
238 if ($endpoint < 0) {
239 $endpoint = $step < 0 ? -1 : 0;
240 }
241 } elseif ($endpoint >= $length) {
242 $endpoint = $step < 0 ? $length - 1 : $length;
243 }
244
245 return $endpoint;
246 }
247
248 private static function adjustSlice($length, $start, $stop, $step)
249 {
250 if ($step === null) {
251 $step = 1;
252 } elseif ($step === 0) {
253 throw new \RuntimeException('step cannot be 0');
254 }
255
256 if ($start === null) {
257 $start = $step < 0 ? $length - 1 : 0;
258 } else {
259 $start = self::adjustEndpoint($length, $start, $step);
260 }
261
262 if ($stop === null) {
263 $stop = $step < 0 ? -1 : $length;
264 } else {
265 $stop = self::adjustEndpoint($length, $stop, $step);
266 }
267
268 return [$start, $stop, $step];
269 }
270
271 private static function sliceIndices($subject, $start, $stop, $step)
272 {
273 $type = gettype($subject);
274 if ($type == 'string') {
275 $subject = mb_str_split($subject, 1, 'UTF-8');
276 }
277 $len = count($subject);
278 list($start, $stop, $step) = self::adjustSlice($len, $start, $stop, $step);
279
280 $result = [];
281 if ($step > 0) {
282 for ($i = $start; $i < $stop; $i += $step) {
283 $result[] = $subject[$i];
284 }
285 } else {
286 for ($i = $start; $i > $stop; $i += $step) {
287 $result[] = $subject[$i];
288 }
289 }
290
291 return $type == 'string' ? implode('', $result) : $result;
292 }
293 }