-
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 to_branch loader * Added Closure interface to BranchingLoader
- Loading branch information
1 parent
a66a3cc
commit 406b35c
Showing
3 changed files
with
107 additions
and
0 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
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,42 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Flow\ETL\Loader; | ||
|
||
use Flow\ETL\FlowContext; | ||
use Flow\ETL\Function\ScalarFunction; | ||
use Flow\ETL\Loader; | ||
use Flow\ETL\Rows; | ||
use Flow\ETL\Transformer\ScalarFunctionFilterTransformer; | ||
|
||
final class BranchingLoader implements Closure, Loader, OverridingLoader | ||
{ | ||
public function __construct( | ||
private readonly ScalarFunction $condition, | ||
private readonly Loader $loader | ||
) { | ||
} | ||
|
||
public function closure(FlowContext $context) : void | ||
{ | ||
if ($this->loader instanceof Closure) { | ||
$this->loader->closure($context); | ||
} | ||
} | ||
|
||
public function load(Rows $rows, FlowContext $context) : void | ||
{ | ||
$this->loader->load( | ||
(new ScalarFunctionFilterTransformer($this->condition))->transform($rows, $context), | ||
$context | ||
); | ||
} | ||
|
||
public function loaders() : array | ||
{ | ||
return [ | ||
$this->loader, | ||
]; | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
src/core/etl/tests/Flow/ETL/Tests/Integration/DataFrame/BranchingTest.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,60 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Flow\ETL\Tests\Integration\DataFrame; | ||
|
||
use function Flow\ETL\DSL\df; | ||
use function Flow\ETL\DSL\from_array; | ||
use function Flow\ETL\DSL\lit; | ||
use function Flow\ETL\DSL\ref; | ||
use function Flow\ETL\DSL\to_branch; | ||
use function Flow\ETL\DSL\to_memory; | ||
use Flow\ETL\Memory\ArrayMemory; | ||
use Flow\ETL\Tests\Integration\IntegrationTestCase; | ||
|
||
final class BranchingTest extends IntegrationTestCase | ||
{ | ||
public function test_branching() : void | ||
{ | ||
df() | ||
->read(from_array([ | ||
['id' => 1, 'group' => 'A'], | ||
['id' => 2, 'group' => 'B'], | ||
['id' => 3, 'group' => 'A'], | ||
['id' => 4, 'group' => 'B'], | ||
['id' => 5, 'group' => 'A'], | ||
['id' => 6, 'group' => 'C'], | ||
])) | ||
->write( | ||
to_branch( | ||
ref('group')->equals(lit('A')), | ||
to_memory($memoryA = new ArrayMemory()), | ||
) | ||
) | ||
->write( | ||
to_branch( | ||
ref('group')->isIn(lit(['B', 'C'])), | ||
to_memory($memoryBC = new ArrayMemory()), | ||
) | ||
) | ||
->run(); | ||
|
||
$this->assertSame( | ||
[ | ||
['id' => 1, 'group' => 'A'], | ||
['id' => 3, 'group' => 'A'], | ||
['id' => 5, 'group' => 'A'], | ||
], | ||
$memoryA->dump(), | ||
); | ||
$this->assertSame( | ||
[ | ||
['id' => 2, 'group' => 'B'], | ||
['id' => 4, 'group' => 'B'], | ||
['id' => 6, 'group' => 'C'], | ||
], | ||
$memoryBC->dump(), | ||
); | ||
} | ||
} |