Skip to content

Commit

Permalink
Added missing split function to ScalarFunctionChain
Browse files Browse the repository at this point in the history
  • Loading branch information
norberttech committed Aug 1, 2024
1 parent ee81c2a commit b888074
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
5 changes: 1 addition & 4 deletions src/core/etl/src/Flow/ETL/DSL/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
5 changes: 5 additions & 0 deletions src/core/etl/src/Flow/ETL/Function/ScalarFunctionChain.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
14 changes: 7 additions & 7 deletions src/core/etl/src/Flow/ETL/Function/Split.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit b888074

Please sign in to comment.