diff --git a/.github/workflows/auto-assign.yml b/.github/workflows/auto-assign.yml index 6a9bba4..b1c4e50 100644 --- a/.github/workflows/auto-assign.yml +++ b/.github/workflows/auto-assign.yml @@ -1,9 +1,12 @@ -name: Auto assign issues +name: Auto assign issues and pull requests on: issues: types: - opened + pull_request: + types: + - opened jobs: run: @@ -12,11 +15,11 @@ jobs: issues: write pull-requests: write steps: - - name: Assign issues - uses: gustavofreze/auto-assign@1.0.0 + - name: Assign issues and pull requests + uses: gustavofreze/auto-assign@1.1.4 with: assignees: '${{ secrets.ASSIGNEES }}' github_token: '${{ secrets.GITHUB_TOKEN }}' allow_self_assign: 'true' allow_no_assignees: 'true' - assignment_options: 'ISSUE' \ No newline at end of file + assignment_options: 'ISSUE,PULL_REQUEST' \ No newline at end of file diff --git a/Makefile b/Makefile index 93f6f00..3ce60f5 100644 --- a/Makefile +++ b/Makefile @@ -22,4 +22,4 @@ show-reports: clean: @sudo chown -R ${USER}:${USER} ${PWD} - @rm -rf report vendor .phpunit.cache .lock + @rm -rf report vendor .phpunit.cache *.lock diff --git a/README.md b/README.md index 0540a21..ebc45e2 100644 --- a/README.md +++ b/README.md @@ -64,14 +64,14 @@ namespace Example; use TinyBlocks\Collection\Collection; use TinyBlocks\Collection\Order; -use TinyBlocks\Collection\PreserveKeys; +use TinyBlocks\Mapper\KeyPreservation; $collection = Collection::createFrom(elements: [1, 2, 3, 4, 5]) ->add(elements: [6, 7]) ->filter(predicates: fn(int $value): bool => $value > 3) ->sort(order: Order::ASCENDING_VALUE) ->map(transformations: fn(int $value): int => $value * 2) - ->toArray(preserveKeys: PreserveKeys::DISCARD); + ->toArray(keyPreservation: KeyPreservation::DISCARD); # Output: [8, 10, 12, 14] ``` @@ -320,9 +320,9 @@ These methods allow the Collection's elements to be transformed or converted int By default, `PreserveKeys::PRESERVE` is used. ```php - use TinyBlocks\Collection\PreserveKeys; + use TinyBlocks\Mapper\KeyPreservation; - $collection->toArray(preserveKeys: PreserveKeys::DISCARD); + $collection->toArray(preserveKeys: KeyPreservation::DISCARD); ``` #### Convert to JSON @@ -337,9 +337,9 @@ These methods allow the Collection's elements to be transformed or converted int By default, `PreserveKeys::PRESERVE` is used. ```php - use TinyBlocks\Collection\PreserveKeys; + use TinyBlocks\Mapper\KeyPreservation; - $collection->toJson(preserveKeys: PreserveKeys::DISCARD); + $collection->toJson(preserveKeys: KeyPreservation::DISCARD); ```
diff --git a/composer.json b/composer.json index 2913319..2cdafa3 100644 --- a/composer.json +++ b/composer.json @@ -45,7 +45,7 @@ }, "require": { "php": "^8.3", - "tiny-blocks/serializer": "^3" + "tiny-blocks/mapper": "^1.0" }, "require-dev": { "phpmd/phpmd": "^2.15", diff --git a/src/Collectible.php b/src/Collectible.php index c11c4d3..ab19eaf 100644 --- a/src/Collectible.php +++ b/src/Collectible.php @@ -7,6 +7,7 @@ use Closure; use Countable; use IteratorAggregate; +use TinyBlocks\Mapper\KeyPreservation; use Traversable; /** @@ -224,27 +225,27 @@ public function slice(int $index, int $length = -1): Collectible; * Converts the Collection to an array. * * The key preservation behavior should be provided from the `PreserveKeys` enum: - * - {@see PreserveKeys::PRESERVE}: Preserves the array keys. - * - {@see PreserveKeys::DISCARD}: Discards the array keys. + * - {@see KeyPreservation::PRESERVE}: Preserves the array keys. + * - {@see KeyPreservation::DISCARD}: Discards the array keys. * * By default, `PreserveKeys::PRESERVE` is used. * - * @param PreserveKeys $preserveKeys The option to preserve or discard array keys. + * @param KeyPreservation $keyPreservation The option to preserve or discard array keys. * @return array The resulting array. */ - public function toArray(PreserveKeys $preserveKeys = PreserveKeys::PRESERVE): array; + public function toArray(KeyPreservation $keyPreservation = KeyPreservation::PRESERVE): array; /** * Converts the Collection to a JSON string. * * The key preservation behavior should be provided from the `PreserveKeys` enum: - * - {@see PreserveKeys::PRESERVE}: Preserves the array keys. - * - {@see PreserveKeys::DISCARD}: Discards the array keys. + * - {@see KeyPreservation::PRESERVE}: Preserves the array keys. + * - {@see KeyPreservation::DISCARD}: Discards the array keys. * * By default, `PreserveKeys::PRESERVE` is used. * - * @param PreserveKeys $preserveKeys The option to preserve or discard array keys. + * @param KeyPreservation $keyPreservation The option to preserve or discard array keys. * @return string The resulting JSON string. */ - public function toJson(PreserveKeys $preserveKeys = PreserveKeys::PRESERVE): string; + public function toJson(KeyPreservation $keyPreservation = KeyPreservation::PRESERVE): string; } diff --git a/src/Collection.php b/src/Collection.php index 4cbd4b3..687edd7 100644 --- a/src/Collection.php +++ b/src/Collection.php @@ -21,12 +21,12 @@ use TinyBlocks\Collection\Internal\Operations\Transform\GroupBy; use TinyBlocks\Collection\Internal\Operations\Transform\JoinToString; use TinyBlocks\Collection\Internal\Operations\Transform\Map; -use TinyBlocks\Collection\Internal\Operations\Transform\MapToArray; -use TinyBlocks\Collection\Internal\Operations\Transform\MapToJson; use TinyBlocks\Collection\Internal\Operations\Write\Add; use TinyBlocks\Collection\Internal\Operations\Write\Create; use TinyBlocks\Collection\Internal\Operations\Write\Remove; use TinyBlocks\Collection\Internal\Operations\Write\RemoveAll; +use TinyBlocks\Mapper\IterableMappability; +use TinyBlocks\Mapper\IterableMapper; use Traversable; /** @@ -34,8 +34,10 @@ * filtering, mapping, and transforming elements. Internally uses iterators to apply operations * lazily and efficiently. */ -class Collection implements Collectible +class Collection implements Collectible, IterableMapper { + use IterableMappability; + private LazyIterator $iterator; private function __construct(LazyIterator $iterator) @@ -169,14 +171,4 @@ public function slice(int $index, int $length = -1): static ) ); } - - public function toArray(PreserveKeys $preserveKeys = PreserveKeys::PRESERVE): array - { - return MapToArray::from(elements: $this->iterator->getIterator(), preserveKeys: $preserveKeys)->toArray(); - } - - public function toJson(PreserveKeys $preserveKeys = PreserveKeys::PRESERVE): string - { - return MapToJson::from(elements: $this->iterator->getIterator(), preserveKeys: $preserveKeys)->toJson(); - } } diff --git a/src/Internal/Operations/Transform/MapToArray.php b/src/Internal/Operations/Transform/MapToArray.php deleted file mode 100644 index a4d1941..0000000 --- a/src/Internal/Operations/Transform/MapToArray.php +++ /dev/null @@ -1,29 +0,0 @@ -serializer = new IterableSerializer(iterable: $elements); - } - - public static function from(iterable $elements, PreserveKeys $preserveKeys): MapToArray - { - return new MapToArray(elements: $elements, preserveKeys: $preserveKeys); - } - - public function toArray(): array - { - return $this->serializer->toArray(serializeKeys: $this->preserveKeys->toSerializeKeys()); - } -} diff --git a/src/Internal/Operations/Transform/MapToJson.php b/src/Internal/Operations/Transform/MapToJson.php deleted file mode 100644 index 16982b1..0000000 --- a/src/Internal/Operations/Transform/MapToJson.php +++ /dev/null @@ -1,29 +0,0 @@ -serializer = new IterableSerializer(iterable: $elements); - } - - public static function from(iterable $elements, PreserveKeys $preserveKeys): MapToJson - { - return new MapToJson(elements: $elements, preserveKeys: $preserveKeys); - } - - public function toJson(): string - { - return $this->serializer->toJson(serializeKeys: $this->preserveKeys->toSerializeKeys()); - } -} diff --git a/src/PreserveKeys.php b/src/PreserveKeys.php deleted file mode 100644 index 917df61..0000000 --- a/src/PreserveKeys.php +++ /dev/null @@ -1,28 +0,0 @@ -toArray(preserveKeys: PreserveKeys::DISCARD)); + self::assertSame([4], $actual->toArray(keyPreservation: KeyPreservation::DISCARD)); } #[DataProvider('elementsDataProvider')] @@ -43,7 +43,7 @@ public function testFilterAppliesDefaultArrayFilter(iterable $elements, iterable $actual = $collection->filter(); /** @Then the filtered collection should contain only truthy elements */ - self::assertSame(array_values((array)$expected), $actual->toArray(preserveKeys: PreserveKeys::DISCARD)); + self::assertSame(array_values((array)$expected), $actual->toArray(keyPreservation: KeyPreservation::DISCARD)); } #[DataProvider('elementsDataProviderWithKeys')] @@ -70,12 +70,12 @@ public static function elementsDataProvider(): iterable yield 'Array with boolean values' => [ 'elements' => [false, true, false, true], - 'expected' => [1 => true, 3 => true] + 'expected' => [true, true] ]; yield 'Array with null and numbers' => [ 'elements' => [null, 1, 2, 0], - 'expected' => [1 => 1, 2 => 2] + 'expected' => [1, 2] ]; yield 'Array with only falsy values' => [ @@ -85,7 +85,7 @@ public static function elementsDataProvider(): iterable yield 'Array with objects and truthy values' => [ 'elements' => [$bitcoin, 1, 'valid string'], - 'expected' => [$bitcoin->toArray(), 1, 'valid string'] + 'expected' => [$bitcoin->toArray(keyPreservation: KeyPreservation::DISCARD), 1, 'valid string'] ]; }