-
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 CSV RowsNormalizer to automate writing deeply nested data struc…
…tures to CSV without manual casting
- Loading branch information
1 parent
a7c3654
commit 32be7f8
Showing
5 changed files
with
77 additions
and
44 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
31 changes: 31 additions & 0 deletions
31
src/adapter/etl-adapter-csv/src/Flow/ETL/Adapter/CSV/RowsNormalizer.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,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Flow\ETL\Adapter\CSV; | ||
|
||
use Flow\ETL\Adapter\CSV\RowsNormalizer\EntryNormalizer; | ||
use Flow\ETL\Rows; | ||
|
||
final class RowsNormalizer | ||
{ | ||
public function __construct(private readonly EntryNormalizer $entryNormalizer) | ||
{ | ||
} | ||
|
||
/** | ||
* @return \Generator<array<null|bool|float|int|string>> | ||
*/ | ||
public function normalize(Rows $rows) : \Generator | ||
{ | ||
foreach ($rows as $row) { | ||
$normalizedRow = []; | ||
|
||
foreach ($row->entries() as $entry) { | ||
$normalizedRow[] = $this->entryNormalizer->normalize($entry); | ||
} | ||
|
||
yield $normalizedRow; | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/adapter/etl-adapter-csv/src/Flow/ETL/Adapter/CSV/RowsNormalizer/EntryNormalizer.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,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Flow\ETL\Adapter\CSV\RowsNormalizer; | ||
|
||
use function Flow\ETL\DSL\type_json; | ||
use Flow\ETL\PHP\Type\Caster; | ||
use Flow\ETL\Row\Entry; | ||
|
||
final class EntryNormalizer | ||
{ | ||
public function __construct( | ||
private readonly Caster $caster, | ||
private readonly string $dateTimeFormat = \DateTimeInterface::ATOM | ||
) { | ||
} | ||
|
||
public function normalize(Entry $entry) : string|float|int|bool|null | ||
{ | ||
return match ($entry::class) { | ||
Entry\UuidEntry::class, | ||
Entry\XMLElementEntry::class, | ||
Entry\XMLEntry::class => $entry->toString(), | ||
Entry\DateTimeEntry::class => $entry->value()?->format($this->dateTimeFormat), | ||
Entry\EnumEntry::class => $entry->value()?->name, | ||
Entry\ArrayEntry::class, | ||
Entry\ListEntry::class, | ||
Entry\MapEntry::class, | ||
Entry\StructureEntry::class, | ||
Entry\JsonEntry::class, | ||
Entry\ObjectEntry::class => $this->caster->to(type_json())->value($entry->value()), | ||
default => $entry->value(), | ||
}; | ||
} | ||
} |
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