| 1 |
<?php |
| 2 |
namespace JmesPath; |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
class FnDispatcher |
| 11 |
{ |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
public static function getInstance() |
| 18 |
{ |
| 19 |
static $instance = null; |
| 20 |
if (!$instance) { |
| 21 |
$instance = new self(); |
| 22 |
} |
| 23 |
|
| 24 |
return $instance; |
| 25 |
} |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public function __invoke($fn, array $args) |
| 34 |
{ |
| 35 |
return $this->{'fn_' . $fn}($args); |
| 36 |
} |
| 37 |
|
| 38 |
private function fn_abs(array $args) |
| 39 |
{ |
| 40 |
$this->validate('abs', $args, [['number']]); |
| 41 |
return abs($args[0]); |
| 42 |
} |
| 43 |
|
| 44 |
private function fn_avg(array $args) |
| 45 |
{ |
| 46 |
$this->validate('avg', $args, [['array']]); |
| 47 |
$sum = $this->reduce('avg:0', $args[0], ['number'], function ($a, $b) { |
| 48 |
return Utils::add($a, $b); |
| 49 |
}); |
| 50 |
return $args[0] ? ($sum / count($args[0])) : null; |
| 51 |
} |
| 52 |
|
| 53 |
private function fn_ceil(array $args) |
| 54 |
{ |
| 55 |
$this->validate('ceil', $args, [['number']]); |
| 56 |
return ceil($args[0]); |
| 57 |
} |
| 58 |
|
| 59 |
private function fn_contains(array $args) |
| 60 |
{ |
| 61 |
$this->validate('contains', $args, [['string', 'array'], ['any']]); |
| 62 |
if (is_array($args[0])) { |
| 63 |
foreach ($args[0] as $value) { |
| 64 |
if (Utils::isEqual($value, $args[1])) { |
| 65 |
return true; |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
return false; |
| 70 |
} |
| 71 |
|
| 72 |
return is_string($args[1]) |
| 73 |
? mb_strpos($args[0], $args[1], 0, 'UTF-8') !== false |
| 74 |
: false; |
| 75 |
} |
| 76 |
|
| 77 |
private function fn_ends_with(array $args) |
| 78 |
{ |
| 79 |
$this->validate('ends_with', $args, [['string'], ['string']]); |
| 80 |
list($search, $suffix) = $args; |
| 81 |
return $suffix === '' || mb_substr($search, -mb_strlen($suffix, 'UTF-8'), null, 'UTF-8') === $suffix; |
| 82 |
} |
| 83 |
|
| 84 |
private function fn_floor(array $args) |
| 85 |
{ |
| 86 |
$this->validate('floor', $args, [['number']]); |
| 87 |
return floor($args[0]); |
| 88 |
} |
| 89 |
|
| 90 |
private function fn_not_null(array $args) |
| 91 |
{ |
| 92 |
if (!$args) { |
| 93 |
throw new \RuntimeException( |
| 94 |
"not_null() expects 1 or more arguments, 0 were provided" |
| 95 |
); |
| 96 |
} |
| 97 |
|
| 98 |
return array_reduce($args, function ($carry, $item) { |
| 99 |
return $carry !== null ? $carry : $item; |
| 100 |
}); |
| 101 |
} |
| 102 |
|
| 103 |
private function fn_join(array $args) |
| 104 |
{ |
| 105 |
$this->validate('join', $args, [['string'], ['array']]); |
| 106 |
$fn = function ($a, $b, $i) use ($args) { |
| 107 |
return $i ? ($a . $args[0] . $b) : $b; |
| 108 |
}; |
| 109 |
return $args[1] ? $this->reduce('join:0', $args[1], ['string'], $fn) : ''; |
| 110 |
} |
| 111 |
|
| 112 |
private function fn_keys(array $args) |
| 113 |
{ |
| 114 |
$this->validate('keys', $args, [['object']]); |
| 115 |
return array_keys((array) $args[0]); |
| 116 |
} |
| 117 |
|
| 118 |
private function fn_length(array $args) |
| 119 |
{ |
| 120 |
$this->validate('length', $args, [['string', 'array', 'object']]); |
| 121 |
return is_string($args[0]) ? mb_strlen($args[0], 'UTF-8') : count((array) $args[0]); |
| 122 |
} |
| 123 |
|
| 124 |
private function fn_max(array $args) |
| 125 |
{ |
| 126 |
$this->validate('max', $args, [['array']]); |
| 127 |
$fn = function ($a, $b, $i) { |
| 128 |
return $i && self::compareValues($a, $b) >= 0 ? $a : $b; |
| 129 |
}; |
| 130 |
return $this->reduce('max:0', $args[0], ['number', 'string'], $fn); |
| 131 |
} |
| 132 |
|
| 133 |
private function fn_max_by(array $args) |
| 134 |
{ |
| 135 |
$this->validate('max_by', $args, [['array'], ['expression']]); |
| 136 |
$expr = $this->wrapExpression('max_by:1', $args[1], ['number', 'string']); |
| 137 |
$carryKey = null; |
| 138 |
$fn = function ($carry, $item, $index) use ($expr, &$carryKey) { |
| 139 |
if (!$index) { |
| 140 |
return $item; |
| 141 |
} |
| 142 |
if ($index === 1) { |
| 143 |
$carryKey = $expr($carry); |
| 144 |
} |
| 145 |
$itemKey = $expr($item); |
| 146 |
$this->validateSeq('max_by:0', ['number', 'string'], $carryKey, $itemKey); |
| 147 |
if (self::compareValues($carryKey, $itemKey) >= 0) { |
| 148 |
return $carry; |
| 149 |
} |
| 150 |
$carryKey = $itemKey; |
| 151 |
return $item; |
| 152 |
}; |
| 153 |
return $this->reduce('max_by:1', $args[0], ['any'], $fn); |
| 154 |
} |
| 155 |
|
| 156 |
private function fn_min(array $args) |
| 157 |
{ |
| 158 |
$this->validate('min', $args, [['array']]); |
| 159 |
$fn = function ($a, $b, $i) { |
| 160 |
return $i && self::compareValues($a, $b) <= 0 ? $a : $b; |
| 161 |
}; |
| 162 |
return $this->reduce('min:0', $args[0], ['number', 'string'], $fn); |
| 163 |
} |
| 164 |
|
| 165 |
private function fn_min_by(array $args) |
| 166 |
{ |
| 167 |
$this->validate('min_by', $args, [['array'], ['expression']]); |
| 168 |
$expr = $this->wrapExpression('min_by:1', $args[1], ['number', 'string']); |
| 169 |
$carryKey = null; |
| 170 |
$fn = function ($carry, $item, $index) use ($expr, &$carryKey) { |
| 171 |
if (!$index) { |
| 172 |
return $item; |
| 173 |
} |
| 174 |
if ($index === 1) { |
| 175 |
$carryKey = $expr($carry); |
| 176 |
} |
| 177 |
$itemKey = $expr($item); |
| 178 |
$this->validateSeq('min_by:0', ['number', 'string'], $carryKey, $itemKey); |
| 179 |
if (self::compareValues($carryKey, $itemKey) <= 0) { |
| 180 |
return $carry; |
| 181 |
} |
| 182 |
$carryKey = $itemKey; |
| 183 |
return $item; |
| 184 |
}; |
| 185 |
return $this->reduce('min_by:1', $args[0], ['any'], $fn); |
| 186 |
} |
| 187 |
|
| 188 |
private function fn_reverse(array $args) |
| 189 |
{ |
| 190 |
$this->validate('reverse', $args, [['array', 'string']]); |
| 191 |
if (is_array($args[0])) { |
| 192 |
return array_reverse($args[0]); |
| 193 |
} elseif (is_string($args[0])) { |
| 194 |
return implode('', array_reverse(mb_str_split($args[0], 1, 'UTF-8'))); |
| 195 |
} else { |
| 196 |
throw new \RuntimeException('Cannot reverse provided argument'); |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
private function fn_sum(array $args) |
| 201 |
{ |
| 202 |
$this->validate('sum', $args, [['array']]); |
| 203 |
$fn = function ($a, $b) { |
| 204 |
return Utils::add($a, $b); |
| 205 |
}; |
| 206 |
return $args[0] ? $this->reduce('sum:0', $args[0], ['number'], $fn) : 0; |
| 207 |
} |
| 208 |
|
| 209 |
private function fn_sort(array $args) |
| 210 |
{ |
| 211 |
$this->validate('sort', $args, [['array']]); |
| 212 |
$valid = ['string', 'number']; |
| 213 |
return Utils::stableSort($args[0], function ($a, $b) use ($valid) { |
| 214 |
$this->validateSeq('sort:0', $valid, $a, $b); |
| 215 |
return self::compareValues($a, $b); |
| 216 |
}); |
| 217 |
} |
| 218 |
|
| 219 |
private function fn_sort_by(array $args) |
| 220 |
{ |
| 221 |
$this->validate('sort_by', $args, [['array'], ['expression']]); |
| 222 |
$expr = $args[1]; |
| 223 |
$valid = ['string', 'number']; |
| 224 |
return Utils::stableSort( |
| 225 |
$args[0], |
| 226 |
function ($a, $b) use ($expr, $valid) { |
| 227 |
$va = $expr($a); |
| 228 |
$vb = $expr($b); |
| 229 |
$this->validateSeq('sort_by:0', $valid, $va, $vb); |
| 230 |
return self::compareValues($va, $vb); |
| 231 |
} |
| 232 |
); |
| 233 |
} |
| 234 |
|
| 235 |
private function fn_starts_with(array $args) |
| 236 |
{ |
| 237 |
$this->validate('starts_with', $args, [['string'], ['string']]); |
| 238 |
list($search, $prefix) = $args; |
| 239 |
return $prefix === '' || mb_strpos($search, $prefix, 0, 'UTF-8') === 0; |
| 240 |
} |
| 241 |
|
| 242 |
private function fn_type(array $args) |
| 243 |
{ |
| 244 |
$this->validateArity('type', count($args), 1); |
| 245 |
return Utils::type($args[0]); |
| 246 |
} |
| 247 |
|
| 248 |
private function fn_to_string(array $args) |
| 249 |
{ |
| 250 |
$this->validateArity('to_string', count($args), 1); |
| 251 |
$v = $args[0]; |
| 252 |
if (is_string($v)) { |
| 253 |
return $v; |
| 254 |
} elseif (is_object($v) |
| 255 |
&& !($v instanceof \JsonSerializable) |
| 256 |
&& method_exists($v, '__toString') |
| 257 |
) { |
| 258 |
return (string) $v; |
| 259 |
} |
| 260 |
|
| 261 |
return json_encode($v); |
| 262 |
} |
| 263 |
|
| 264 |
private function fn_to_number(array $args) |
| 265 |
{ |
| 266 |
$this->validateArity('to_number', count($args), 1); |
| 267 |
$value = $args[0]; |
| 268 |
|
| 269 |
if (Utils::type($value) == 'number') { |
| 270 |
return $value; |
| 271 |
} |
| 272 |
|
| 273 |
if (!is_string($value)) { |
| 274 |
return null; |
| 275 |
} |
| 276 |
|
| 277 |
return $this->parseJsonNumber($value); |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
private function parseJsonNumber($value) |
| 286 |
{ |
| 287 |
if (!preg_match('/^-?(?:0|[1-9][0-9]*)(?:\.[0-9]+)?(?:[eE][+-]?[0-9]+)?$/D', $value)) { |
| 288 |
return null; |
| 289 |
} |
| 290 |
|
| 291 |
if (preg_match('/^-?(?:0|[1-9][0-9]*)$/D', $value)) { |
| 292 |
return $this->parseJsonInteger($value); |
| 293 |
} |
| 294 |
|
| 295 |
$number = (float) $value; |
| 296 |
|
| 297 |
return is_finite($number) ? $number : null; |
| 298 |
} |
| 299 |
|
| 300 |
private function parseJsonInteger($value) |
| 301 |
{ |
| 302 |
$negative = $value[0] === '-'; |
| 303 |
$digits = $negative ? substr($value, 1) : $value; |
| 304 |
$limit = $negative ? substr((string) PHP_INT_MIN, 1) : (string) PHP_INT_MAX; |
| 305 |
|
| 306 |
if (strlen($digits) < strlen($limit) |
| 307 |
|| (strlen($digits) === strlen($limit) && strcmp($digits, $limit) <= 0) |
| 308 |
) { |
| 309 |
return (int) $value; |
| 310 |
} |
| 311 |
|
| 312 |
$number = (float) $value; |
| 313 |
|
| 314 |
return is_finite($number) ? $number : null; |
| 315 |
} |
| 316 |
|
| 317 |
private function fn_values(array $args) |
| 318 |
{ |
| 319 |
$this->validate('values', $args, [['array', 'object']]); |
| 320 |
return array_values((array) $args[0]); |
| 321 |
} |
| 322 |
|
| 323 |
private function fn_merge(array $args) |
| 324 |
{ |
| 325 |
if (!$args) { |
| 326 |
throw new \RuntimeException( |
| 327 |
"merge() expects 1 or more arguments, 0 were provided" |
| 328 |
); |
| 329 |
} |
| 330 |
|
| 331 |
return call_user_func_array('array_replace', $args); |
| 332 |
} |
| 333 |
|
| 334 |
private function fn_to_array(array $args) |
| 335 |
{ |
| 336 |
$this->validate('to_array', $args, [['any']]); |
| 337 |
|
| 338 |
return Utils::isArray($args[0]) ? $args[0] : [$args[0]]; |
| 339 |
} |
| 340 |
|
| 341 |
private function fn_map(array $args) |
| 342 |
{ |
| 343 |
$this->validate('map', $args, [['expression'], ['array']]); |
| 344 |
$result = []; |
| 345 |
foreach ($args[1] as $a) { |
| 346 |
$result[] = $args[0]($a); |
| 347 |
} |
| 348 |
return $result; |
| 349 |
} |
| 350 |
|
| 351 |
private function typeError($from, $msg) |
| 352 |
{ |
| 353 |
if (mb_strpos($from, ':', 0, 'UTF-8')) { |
| 354 |
list($fn, $pos) = explode(':', $from); |
| 355 |
throw new \RuntimeException( |
| 356 |
sprintf('Argument %d of %s %s', $pos, $fn, $msg) |
| 357 |
); |
| 358 |
} else { |
| 359 |
throw new \RuntimeException( |
| 360 |
sprintf('Type error: %s %s', $from, $msg) |
| 361 |
); |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
private function validateArity($from, $given, $expected) |
| 366 |
{ |
| 367 |
if ($given != $expected) { |
| 368 |
$err = "%s() expects {$expected} arguments, {$given} were provided"; |
| 369 |
throw new \RuntimeException(sprintf($err, $from)); |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
private function validate($from, $args, $types = []) |
| 374 |
{ |
| 375 |
$this->validateArity($from, count($args), count($types)); |
| 376 |
foreach ($args as $index => $value) { |
| 377 |
if (!isset($types[$index]) || !$types[$index]) { |
| 378 |
continue; |
| 379 |
} |
| 380 |
$this->validateType("{$from}:{$index}", $value, $types[$index]); |
| 381 |
} |
| 382 |
} |
| 383 |
|
| 384 |
private function validateType($from, $value, array $types) |
| 385 |
{ |
| 386 |
if ($types[0] == 'any' |
| 387 |
|| in_array(Utils::type($value), $types) |
| 388 |
|| ($value === [] && in_array('object', $types)) |
| 389 |
) { |
| 390 |
return; |
| 391 |
} |
| 392 |
$msg = 'must be one of the following types: ' . implode(', ', $types) |
| 393 |
. '. ' . Utils::type($value) . ' found'; |
| 394 |
$this->typeError($from, $msg); |
| 395 |
} |
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
private function validateSeq($from, array $types, $a, $b) |
| 407 |
{ |
| 408 |
$ta = Utils::type($a); |
| 409 |
$tb = Utils::type($b); |
| 410 |
|
| 411 |
if ($ta !== $tb) { |
| 412 |
$msg = "encountered a type mismatch in sequence: {$ta}, {$tb}"; |
| 413 |
$this->typeError($from, $msg); |
| 414 |
} |
| 415 |
|
| 416 |
$typeMatch = ($types && $types[0] == 'any') || in_array($ta, $types); |
| 417 |
if (!$typeMatch) { |
| 418 |
$msg = 'encountered a type error in sequence. The argument must be ' |
| 419 |
. 'an array of ' . implode('|', $types) . ' types. ' |
| 420 |
. "Found {$ta}, {$tb}."; |
| 421 |
$this->typeError($from, $msg); |
| 422 |
} |
| 423 |
} |
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
private static function compareValues($a, $b) |
| 434 |
{ |
| 435 |
return Utils::type($a) === 'string' |
| 436 |
? strcmp((string) $a, (string) $b) |
| 437 |
: ($a <=> $b); |
| 438 |
} |
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
private function reduce($from, array $values, array $types, callable $reduce) |
| 451 |
{ |
| 452 |
$i = -1; |
| 453 |
return array_reduce( |
| 454 |
$values, |
| 455 |
function ($carry, $item) use ($from, $types, $reduce, &$i) { |
| 456 |
if (++$i > 0) { |
| 457 |
$this->validateSeq($from, $types, $carry, $item); |
| 458 |
} |
| 459 |
return $reduce($carry, $item, $i); |
| 460 |
} |
| 461 |
); |
| 462 |
} |
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
private function wrapExpression($from, callable $expr, array $types) |
| 474 |
{ |
| 475 |
list($fn, $pos) = explode(':', $from); |
| 476 |
$from = "The expression return value of argument {$pos} of {$fn}"; |
| 477 |
return function ($value) use ($from, $expr, $types) { |
| 478 |
$value = $expr($value); |
| 479 |
$this->validateType($from, $value, $types); |
| 480 |
return $value; |
| 481 |
}; |
| 482 |
} |
| 483 |
|
| 484 |
|
| 485 |
public function __call($name, $args) |
| 486 |
{ |
| 487 |
$name = str_replace('fn_', '', $name); |
| 488 |
throw new \RuntimeException("Call to undefined function {$name}"); |
| 489 |
} |
| 490 |
} |