-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added DataFrame::until method (#808)
- Loading branch information
1 parent
9cdeb22
commit e26c8eb
Showing
3 changed files
with
102 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/core/etl/src/Flow/ETL/Transformer/UntilTransformer.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Flow\ETL\Transformer; | ||
|
||
use Flow\ETL\Exception\LimitReachedException; | ||
use Flow\ETL\FlowContext; | ||
use Flow\ETL\Function\ScalarFunction; | ||
use Flow\ETL\Rows; | ||
use Flow\ETL\Transformer; | ||
|
||
/** | ||
* @implements Transformer<array{function: ScalarFunction}> | ||
*/ | ||
final class UntilTransformer implements Transformer | ||
{ | ||
private bool $limitReached = false; | ||
|
||
public function __construct(private readonly ScalarFunction $function) | ||
{ | ||
} | ||
|
||
public function __serialize() : array | ||
{ | ||
return [ | ||
'function' => $this->function, | ||
]; | ||
} | ||
|
||
public function __unserialize(array $data) : void | ||
{ | ||
$this->function = $data['function']; | ||
} | ||
|
||
public function transform(Rows $rows, FlowContext $context) : Rows | ||
{ | ||
if ($this->limitReached) { | ||
throw new LimitReachedException(0); | ||
} | ||
|
||
$nextRows = []; | ||
|
||
foreach ($rows as $row) { | ||
if (!$this->function->eval($row)) { | ||
$this->limitReached = true; | ||
} else { | ||
$nextRows[] = $row; | ||
} | ||
} | ||
|
||
return new Rows(...$nextRows); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters