From 4554dd1cd89d1a465bb220cd9a4b73ac0b8eeb24 Mon Sep 17 00:00:00 2001 From: Gustavo Freze Date: Tue, 24 Dec 2024 11:38:04 -0300 Subject: [PATCH] Fixes Collection to array mapping. (#8) * fix: Fixes Collection to array mapping. --- composer.json | 2 +- .../Mappers/Collection/ArrayMapper.php | 2 +- .../Mappers/Collection/CollectionMapper.php | 2 +- tests/Models/Collection.php | 151 ++++++++++++++++++ tests/Models/InvoiceSummaries.php | 11 -- tests/Models/InvoiceSummary.php | 12 -- 6 files changed, 154 insertions(+), 26 deletions(-) create mode 100644 tests/Models/Collection.php delete mode 100644 tests/Models/InvoiceSummaries.php delete mode 100644 tests/Models/InvoiceSummary.php diff --git a/composer.json b/composer.json index 0cd789b..94fd469 100644 --- a/composer.json +++ b/composer.json @@ -43,7 +43,7 @@ }, "require": { "php": "^8.3", - "tiny-blocks/collection": "1.7.0" + "tiny-blocks/collection": "1.8.0" }, "require-dev": { "phpmd/phpmd": "^2.15", diff --git a/src/Internal/Mappers/Collection/ArrayMapper.php b/src/Internal/Mappers/Collection/ArrayMapper.php index a8bc1e7..bee2d5a 100644 --- a/src/Internal/Mappers/Collection/ArrayMapper.php +++ b/src/Internal/Mappers/Collection/ArrayMapper.php @@ -16,7 +16,7 @@ public function map(mixed $value, KeyPreservation $keyPreservation): array if ($valueMapper->valueIsCollectible(value: $value)) { $collectionMapper = new CollectionMapper(valueMapper: $valueMapper); - $mappedValues = $collectionMapper->map(value: $value, keyPreservation: $keyPreservation); + return $collectionMapper->map(value: $value, keyPreservation: $keyPreservation); } $reflectionClass = new ReflectionClass($value); diff --git a/src/Internal/Mappers/Collection/CollectionMapper.php b/src/Internal/Mappers/Collection/CollectionMapper.php index c5f1a44..01455c8 100644 --- a/src/Internal/Mappers/Collection/CollectionMapper.php +++ b/src/Internal/Mappers/Collection/CollectionMapper.php @@ -21,6 +21,6 @@ public function map(Collectible $value, KeyPreservation $keyPreservation): array $mappedValues[$key] = $this->valueMapper->map(value: $element, keyPreservation: $keyPreservation); } - return $mappedValues; + return $keyPreservation->shouldPreserveKeys() ? $mappedValues : array_values($mappedValues); } } diff --git a/tests/Models/Collection.php b/tests/Models/Collection.php new file mode 100644 index 0000000..3f07d85 --- /dev/null +++ b/tests/Models/Collection.php @@ -0,0 +1,151 @@ +iterator = $iterator; + } + + public static function createFrom(iterable $elements): Collectible + { + return new Collection(iterator: $elements); + } + + public static function createFromEmpty(): Collectible + { + // TODO: Implement createFromEmpty() method. + } + + public function add(...$elements): Collectible + { + // TODO: Implement add() method. + } + + public function contains(mixed $element): bool + { + // TODO: Implement contains() method. + } + + public function count(): int + { + return iterator_count($this->iterator); + } + + public function each(Closure ...$actions): Collectible + { + // TODO: Implement each() method. + } + + public function equals(Collectible $other): bool + { + // TODO: Implement equals() method. + } + + public function filter(?Closure ...$predicates): Collectible + { + // TODO: Implement filter() method. + } + + public function findBy(Closure ...$predicates): mixed + { + // TODO: Implement findBy() method. + } + + public function first(mixed $defaultValueIfNotFound = null): mixed + { + // TODO: Implement first() method. + } + + public function flatten(): Collectible + { + // TODO: Implement flatten() method. + } + + public function getBy(int $index, mixed $defaultValueIfNotFound = null): mixed + { + // TODO: Implement getBy() method. + } + + public function getIterator(): Traversable + { + yield from $this->iterator; + } + + public function groupBy(Closure $grouping): Collectible + { + // TODO: Implement groupBy() method. + } + + public function isEmpty(): bool + { + // TODO: Implement isEmpty() method. + } + + public function joinToString(string $separator): string + { + // TODO: Implement joinToString() method. + } + + public function last(mixed $defaultValueIfNotFound = null): mixed + { + // TODO: Implement last() method. + } + + public function map(Closure ...$transformations): Collectible + { + // TODO: Implement map() method. + } + + public function remove(mixed $element): Collectible + { + // TODO: Implement remove() method. + } + + public function removeAll(?Closure $filter = null): Collectible + { + // TODO: Implement removeAll() method. + } + + public function reduce(Closure $aggregator, mixed $initial): mixed + { + // TODO: Implement reduce() method. + } + + public function sort( + \TinyBlocks\Collection\Order $order = \TinyBlocks\Collection\Order::ASCENDING_KEY, + ?Closure $predicate = null + ): Collectible { + // TODO: Implement sort() method. + } + + public function slice(int $index, int $length = -1): Collectible + { + // TODO: Implement slice() method. + } + + public function toArray(PreserveKeys $preserveKeys = PreserveKeys::PRESERVE): array + { + // TODO: Implement toArray() method. + } + + public function toJson(PreserveKeys $preserveKeys = PreserveKeys::PRESERVE): string + { + // TODO: Implement toJson() method. + } +} diff --git a/tests/Models/InvoiceSummaries.php b/tests/Models/InvoiceSummaries.php deleted file mode 100644 index f4b6a1d..0000000 --- a/tests/Models/InvoiceSummaries.php +++ /dev/null @@ -1,11 +0,0 @@ -