-
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 ArrayLoader with usage example (#1226)
* Added ArrayLoader with usage example * CS Fixes
- Loading branch information
1 parent
29e875e
commit e680520
Showing
16 changed files
with
389 additions
and
271 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use function Flow\ETL\DSL\{data_frame, from_array, to_array}; | ||
|
||
require __DIR__ . '/../../../autoload.php'; | ||
|
||
$array = []; | ||
|
||
data_frame() | ||
->read(from_array([ | ||
['id' => 1], | ||
['id' => 2], | ||
['id' => 3], | ||
['id' => 4], | ||
['id' => 5], | ||
])) | ||
->collect() | ||
->write(to_array($array)) | ||
->run(); | ||
|
||
\file_put_contents(__DIR__ . '/output.txt', \var_export($array, true)); |
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,2 @@ | ||
Write datasets directly to an associative array. Please pay attention to the memory usage when using array loader. | ||
Large datasets may cause memory overflow. |
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,22 @@ | ||
array ( | ||
0 => | ||
array ( | ||
'id' => 1, | ||
), | ||
1 => | ||
array ( | ||
'id' => 2, | ||
), | ||
2 => | ||
array ( | ||
'id' => 3, | ||
), | ||
3 => | ||
array ( | ||
'id' => 4, | ||
), | ||
4 => | ||
array ( | ||
'id' => 5, | ||
), | ||
) |
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 @@ | ||
1 |
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 |
---|---|---|
@@ -1 +1 @@ | ||
1 | ||
2 |
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
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,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Flow\ETL\Loader; | ||
|
||
use Flow\ETL\{FlowContext, Loader, Rows}; | ||
|
||
final class ArrayLoader implements Loader | ||
{ | ||
/** | ||
* @param-out array<array<mixed>> $array | ||
*/ | ||
public function __construct(private array &$array) | ||
{ | ||
} | ||
|
||
public function load(Rows $rows, FlowContext $context) : void | ||
{ | ||
$this->array = \array_merge( | ||
$this->array, | ||
$rows->toArray() | ||
); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/core/etl/tests/Flow/ETL/Tests/Unit/Loader/ArrayLoaderTest.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,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Flow\ETL\Tests\Unit\Loader; | ||
|
||
use function Flow\ETL\DSL\{flow_context, int_entry, row, rows, str_entry, to_array}; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class ArrayLoaderTest extends TestCase | ||
{ | ||
public function test_loads_rows_data_into_memory() : void | ||
{ | ||
$rows1 = rows( | ||
row(int_entry('number', 1), str_entry('name', 'one')), | ||
row(int_entry('number', 2), str_entry('name', 'two')), | ||
); | ||
|
||
$rows2 = rows( | ||
row(int_entry('number', 3), str_entry('name', 'three')), | ||
row(int_entry('number', 4), str_entry('name', 'four')), | ||
); | ||
|
||
$array = []; | ||
|
||
$loader = to_array($array); | ||
$loader->load($rows1, flow_context()); | ||
$loader->load($rows2, flow_context()); | ||
|
||
self::assertEquals($rows1->merge($rows2)->toArray(), $array); | ||
} | ||
} |
Oops, something went wrong.