From 6a058e6b539d5261890d49bde0bcbb2f9f89d1bb Mon Sep 17 00:00:00 2001 From: Joseph Bielawski Date: Sun, 11 Jan 2026 10:59:14 +0100 Subject: [PATCH] Adjust `Parameter::asNumber()` behaviour to match closer the PHP one --- src/core/etl/src/Flow/ETL/Function/Parameter.php | 10 ++++++++-- .../Flow/ETL/Tests/Unit/Function/ParameterTest.php | 4 ++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/core/etl/src/Flow/ETL/Function/Parameter.php b/src/core/etl/src/Flow/ETL/Function/Parameter.php index 447c60a2e..241845b99 100644 --- a/src/core/etl/src/Flow/ETL/Function/Parameter.php +++ b/src/core/etl/src/Flow/ETL/Function/Parameter.php @@ -153,11 +153,17 @@ public function asNumber(Row $row, FlowContext $context, int|float|null $default { $result = $this->eval($row, $context); - if (\is_string($result)) { + if (!\is_numeric($result)) { return $default; } - return \is_numeric($result) ? $result : $default; + return match (true) { + \is_int($result), + \is_float($result) => $result, + ($result == (int) $result) => (int) $result, + ($result == (float) $result) => (float) $result, + default => $default, + }; } public function asObject(Row $row, FlowContext $context) : ?object diff --git a/src/core/etl/tests/Flow/ETL/Tests/Unit/Function/ParameterTest.php b/src/core/etl/tests/Flow/ETL/Tests/Unit/Function/ParameterTest.php index a0819719c..324ef895b 100644 --- a/src/core/etl/tests/Flow/ETL/Tests/Unit/Function/ParameterTest.php +++ b/src/core/etl/tests/Flow/ETL/Tests/Unit/Function/ParameterTest.php @@ -67,6 +67,10 @@ public static function number_data_provider() : \Generator yield 'zero' => [0, null, 0]; yield 'negative integer' => [-42, null, -42]; yield 'negative float' => [-3.14, null, -3.14]; + yield 'integer as string' => ['99', null, 99]; + yield 'float as string' => ['99.5', null, 99.5]; + yield 'negative integer as string' => ['-42', null, -42]; + yield 'negative float as string' => ['-3.14', null, -3.14]; yield 'string with default' => ['not numeric', 99, 99]; yield 'string with float default' => ['not numeric', 99.5, 99.5]; yield 'boolean with default' => [true, 99, 99];