Skip to content

Commit

Permalink
Move implementation of entry structure creation into the `NativeEntry…
Browse files Browse the repository at this point in the history
…Factory` (#619)
  • Loading branch information
stloyd committed Oct 20, 2023
1 parent 5ddda9c commit 6308b88
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 51 deletions.
47 changes: 0 additions & 47 deletions src/core/etl/src/Flow/ETL/DSL/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -367,50 +367,3 @@ function array_to_rows(array $data, EntryFactory $entryFactory = new NativeEntry

return new Rows(...$rows);
}

/**
* @psalm-suppress MixedAssignment
*/
function array_is_structure(array $array) : bool
{
if (\array_is_list($array)) {
return false;
}

if (!\count($array)) {
return false;
}

foreach ($array as $key => $value) {
if (!\is_string($key)) {
return false;
}

if (\is_array($value)) {
if (!array_is_structure($value)) {
return false;
}
}
}

return true;
}

/**
* @psalm-suppress MixedArgumentTypeCoercion
* @psalm-suppress MixedAssignment
*/
function array_to_structure(string $name, array $array, EntryFactory $entry_factory = new NativeEntryFactory()) : Row\Entry\StructureEntry
{
$structureEntries = [];

foreach ($array as $key => $value) {
if (\is_array($value)) {
$structureEntries[] = array_to_structure($key, $value, $entry_factory);
} else {
$structureEntries[] = $entry_factory->create($key, $value);
}
}

return new Row\Entry\StructureEntry($name, ...$structureEntries);
}
51 changes: 47 additions & 4 deletions src/core/etl/src/Flow/ETL/Row/Factory/NativeEntryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

namespace Flow\ETL\Row\Factory;

use function Flow\ETL\DSL\array_is_structure;
use function Flow\ETL\DSL\array_to_structure;
use Flow\ETL\DSL\Entry as EntryDSL;
use Flow\ETL\Exception\InvalidArgumentException;
use Flow\ETL\Row;
Expand Down Expand Up @@ -108,8 +106,8 @@ public function create(string $entryName, mixed $value) : Entry
return new Row\Entry\ArrayEntry($entryName, $value);
}

if (array_is_structure($value)) {
return array_to_structure($entryName, $value);
if ($this->isStructure($value)) {
return $this->createStructureEntryFromArray($entryName, $value);
}

if (!\array_is_list($value)) {
Expand Down Expand Up @@ -171,6 +169,25 @@ public function create(string $entryName, mixed $value) : Entry
throw new InvalidArgumentException("{$type} can't be converted to any known Entry");
}

/**
* @psalm-suppress MixedArgumentTypeCoercion
* @psalm-suppress MixedAssignment
*/
private function createStructureEntryFromArray(string $entryName, array $array) : Row\Entry\StructureEntry
{
$structureEntries = [];

foreach ($array as $key => $value) {
if (\is_array($value)) {
$structureEntries[] = $this->createStructureEntryFromArray($key, $value);
} else {
$structureEntries[] = $this->create($key, $value);
}
}

return new Row\Entry\StructureEntry($entryName, ...$structureEntries);
}

private function fromDefinition(Schema\Definition $definition, mixed $value) : Entry
{
if ($definition->isNullable() && null === $value) {
Expand Down Expand Up @@ -320,6 +337,32 @@ private function isJson(string $string) : bool
}
}

/**
* @psalm-suppress MixedAssignment
*/
private function isStructure(array $array) : bool
{
if (\array_is_list($array)) {
return false;
}

if (!\count($array)) {
return false;
}

foreach ($array as $key => $value) {
if (!\is_string($key)) {
return false;
}

if (\is_array($value) && !$this->isStructure($value)) {
return false;
}
}

return true;
}

private function isUuid(string $string) : bool
{
if (\strlen($string) !== 36) {
Expand Down

0 comments on commit 6308b88

Please sign in to comment.