diff --git a/src/core/etl/src/Flow/ETL/DSL/functions.php b/src/core/etl/src/Flow/ETL/DSL/functions.php index c2943e252..0cc63ff9b 100644 --- a/src/core/etl/src/Flow/ETL/DSL/functions.php +++ b/src/core/etl/src/Flow/ETL/DSL/functions.php @@ -648,10 +648,7 @@ function date_time_format(ScalarFunction $ref, string $format) : DateTimeFormat return new DateTimeFormat($ref, $format); } -/** - * @param non-empty-string $separator - */ -function split(ScalarFunction $ref, string $separator, int $limit = PHP_INT_MAX) : Split +function split(ScalarFunction $ref, ScalarFunction|string $separator, ScalarFunction|int $limit = PHP_INT_MAX) : Split { return new Split($ref, $separator, $limit); } diff --git a/src/core/etl/src/Flow/ETL/Function/ScalarFunctionChain.php b/src/core/etl/src/Flow/ETL/Function/ScalarFunctionChain.php index d45160228..712574762 100644 --- a/src/core/etl/src/Flow/ETL/Function/ScalarFunctionChain.php +++ b/src/core/etl/src/Flow/ETL/Function/ScalarFunctionChain.php @@ -349,6 +349,11 @@ public function size() : self return new Size($this); } + public function split(ScalarFunction|string $separator, ScalarFunction|int $limit = PHP_INT_MAX) : self + { + return new Split($this, $separator, $limit); + } + public function sprintf(ScalarFunction ...$params) : self { return new Sprintf($this, ...$params); diff --git a/src/core/etl/src/Flow/ETL/Function/Split.php b/src/core/etl/src/Flow/ETL/Function/Split.php index 36ef4ef4e..8153d7070 100644 --- a/src/core/etl/src/Flow/ETL/Function/Split.php +++ b/src/core/etl/src/Flow/ETL/Function/Split.php @@ -8,24 +8,24 @@ final class Split extends ScalarFunctionChain { - /** - * @param non-empty-string $separator - */ public function __construct( private readonly ScalarFunction $ref, - private readonly string $separator, - private readonly int $limit = PHP_INT_MAX, + private readonly ScalarFunction|string $separator, + private readonly ScalarFunction|int $limit = PHP_INT_MAX, ) { } public function eval(Row $row) : mixed { + $separator = $this->separator instanceof ScalarFunction ? $this->separator->eval($row) : $this->separator; + $limit = $this->limit instanceof ScalarFunction ? $this->limit->eval($row) : $this->limit; + $val = $this->ref->eval($row); - if (!\is_string($val)) { + if (!\is_string($val) || !\is_string($separator) || !\is_int($limit) || $limit < 1 || $separator === '') { return $val; } - return \explode($this->separator, $val, $this->limit); + return \explode($separator, $val, $limit); } }