(array $rpn, array $row)
| 333 | } |
| 334 | |
| 335 | private function evaluate(array $rpn, array $row): ?float { |
| 336 | $stack = []; |
| 337 | foreach ($rpn as $token) { |
| 338 | if (str_starts_with($token, '{')) { |
| 339 | $value = $row[substr($token, 1, -1)] ?? null; |
| 340 | $stack[] = $value; |
| 341 | } elseif (str_starts_with($token, "'")) { |
| 342 | $stack[] = str_replace("''", "'", substr($token, 1, -1)); |
| 343 | } elseif (str_starts_with($token, 'lookup:')) { |
| 344 | $args = array_splice($stack, -(int)substr($token, 7)); |
| 345 | $value = array_shift($args); |
| 346 | $result = array_pop($args); |
| 347 | for ($index = 0; $index < count($args); $index += 2) { |
| 348 | // Text matches are exact and case-sensitive; missing values use the fallback. |
| 349 | if ($value !== null && $args[$index] !== null && (string)$value === (string)$args[$index]) { |
| 350 | $result = $args[$index + 1]; |
| 351 | break; |
| 352 | } |
| 353 | } |
| 354 | $stack[] = is_numeric($result) ? (float)$result : null; |
| 355 | } elseif (is_numeric($token)) { |
| 356 | $stack[] = (float)$token; |
| 357 | } elseif ($token === 'neg') { |
| 358 | $value = array_pop($stack); |
| 359 | $stack[] = !is_numeric($value) ? null : -(float)$value; |
| 360 | } else { |
| 361 | $right = array_pop($stack); |
| 362 | $left = array_pop($stack); |
| 363 | $stack[] = !is_numeric($left) || !is_numeric($right) || ($token === '/' && $right == 0.0) ? null : match ($token) { |
| 364 | '+' => $left + $right, |
| 365 | '-' => $left - $right, |
| 366 | '*' => $left * $right, |
| 367 | '/' => $left / $right, |
| 368 | }; |
| 369 | } |
| 370 | } |
| 371 | $value = $stack[0] ?? null; |
| 372 | return is_numeric($value) && is_finite((float)$value) ? (float)$value : null; |
| 373 | } |
| 374 | } |
no test coverage detected