From 3dacbba9088d02981e0ac2d201bb4070b50ae631 Mon Sep 17 00:00:00 2001 From: Joseph Bielawski Date: Fri, 20 Oct 2023 18:41:42 +0200 Subject: [PATCH] Move implementation of entry structure creation into the `NativeEntryFactory` --- src/core/etl/src/Flow/ETL/DSL/functions.php | 47 ----------------- .../ETL/Row/Factory/NativeEntryFactory.php | 51 +++++++++++++++++-- 2 files changed, 47 insertions(+), 51 deletions(-) diff --git a/src/core/etl/src/Flow/ETL/DSL/functions.php b/src/core/etl/src/Flow/ETL/DSL/functions.php index 1d8facb83..f73886cab 100644 --- a/src/core/etl/src/Flow/ETL/DSL/functions.php +++ b/src/core/etl/src/Flow/ETL/DSL/functions.php @@ -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); -} diff --git a/src/core/etl/src/Flow/ETL/Row/Factory/NativeEntryFactory.php b/src/core/etl/src/Flow/ETL/Row/Factory/NativeEntryFactory.php index 0c57bf3f6..f4ea14af4 100644 --- a/src/core/etl/src/Flow/ETL/Row/Factory/NativeEntryFactory.php +++ b/src/core/etl/src/Flow/ETL/Row/Factory/NativeEntryFactory.php @@ -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; @@ -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)) { @@ -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) { @@ -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) {