| 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 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 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 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 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 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
public static function isObject($value) |
| 84 |
{ |
| 85 |
if (is_array($value)) { |
| 86 |
return !$value || array_keys($value)[0] !== 0; |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
return $value instanceof \Countable && $value instanceof \ArrayAccess |
| 91 |
? count($value) == 0 || !$value->offsetExists(0) |
| 92 |
: $value instanceof \stdClass; |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
public static function isArray($value) |
| 103 |
{ |
| 104 |
if (is_array($value)) { |
| 105 |
return !$value || array_keys($value)[0] === 0; |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
return $value instanceof \Countable && $value instanceof \ArrayAccess |
| 110 |
? count($value) == 0 || $value->offsetExists(0) |
| 111 |
: false; |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 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 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 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 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
public static function stableSort(array $data, callable $sortFn) |
| 198 |
{ |
| 199 |
|
| 200 |
array_walk($data, function (&$v, $k) { |
| 201 |
$v = [$v, $k]; |
| 202 |
}); |
| 203 |
|
| 204 |
uasort($data, function ($a, $b) use ($sortFn) { |
| 205 |
return $sortFn($a[0], $b[0]) ?: ($a[1] < $b[1] ? -1 : 1); |
| 206 |
}); |
| 207 |
|
| 208 |
|
| 209 |
return array_map(function ($v) { |
| 210 |
return $v[0]; |
| 211 |
}, array_values($data)); |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 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 |
} |