Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move implementation of entry structure creation into the NativeEntryFactory #619

Merged
merged 1 commit into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading