-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Automate normalization of rows in JsonLoader
- Loading branch information
1 parent
a7c3654
commit d87474d
Showing
4 changed files
with
81 additions
and
12 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
32 changes: 32 additions & 0 deletions
32
src/adapter/etl-adapter-json/src/Flow/ETL/Adapter/JSON/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,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Flow\ETL\Adapter\JSON; | ||
|
||
use Flow\ETL\Adapter\JSON\RowsNormalizer\EntryNormalizer; | ||
use Flow\ETL\Rows; | ||
|
||
final class RowsNormalizer | ||
{ | ||
public function __construct(private readonly EntryNormalizer $normalizer) | ||
{ | ||
|
||
} | ||
|
||
/** | ||
* @return \Generator<array<string, null|array|bool|float|int|string>> | ||
*/ | ||
public function normalize(Rows $rows) : \Generator | ||
{ | ||
foreach ($rows as $row) { | ||
$normalizedRow = []; | ||
|
||
foreach ($row->entries() as $entry) { | ||
$normalizedRow[$entry->name()] = $this->normalizer->normalize($entry); | ||
} | ||
|
||
yield $normalizedRow; | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/adapter/etl-adapter-json/src/Flow/ETL/Adapter/JSON/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,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Flow\ETL\Adapter\JSON\RowsNormalizer; | ||
|
||
use function Flow\ETL\DSL\type_array; | ||
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|array|null | ||
{ | ||
return match ($entry::class) { | ||
Entry\UuidEntry::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_array())->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