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