diff --git a/CHANGELOG.md b/CHANGELOG.md index 773df51..2d7090a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [1.1.0] - 2025-04-12 +### Added +- Add optional `key` argument to `AbstractCollection::add()` method + ## [1.0.2] - 2025-04-10 ### Changed - Increased unit test coverage to 100% diff --git a/README.md b/README.md index b294f7c..0d60541 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@
-Version 1.0.2 +Version 1.1.0 PHP Coverage 100.00% diff --git a/composer.json b/composer.json index 70bacd0..4d90cc8 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,7 @@ "scripts": { "test": "php -d xdebug.mode=coverage ./vendor/bin/phpunit --testdox --colors=always --coverage-html coverage --coverage-clover coverage/coverage.xml --testdox-html coverage/testdox.html && npx badger --phpunit ./coverage/coverage.xml && npx badger --version ./composer.json && npx badger --license ./composer.json" }, - "version": "1.0.2", + "version": "1.1.0", "license": "MIT", "require-dev": { "phpunit/phpunit": "^12.1" diff --git a/src/AbstractCollection.php b/src/AbstractCollection.php index fe12ddb..482c002 100644 --- a/src/AbstractCollection.php +++ b/src/AbstractCollection.php @@ -12,12 +12,18 @@ class AbstractCollection implements CollectionInterface { } - public function add(mixed $item): void + public function add(mixed $item, string $key = null): void { if (null === $item) { return; } + if ($key) { + $this->items[$key] = $item; + + return; + } + $this->items[] = $item; } diff --git a/tests/CollectionTest.php b/tests/CollectionTest.php index d740e71..d06c85f 100644 --- a/tests/CollectionTest.php +++ b/tests/CollectionTest.php @@ -11,10 +11,10 @@ use PHPUnit\Framework\TestCase; class CollectionTest extends TestCase { #[DataProvider('addDataProvider')] - public function testAdd(array $startingItems, mixed $itemToAdd, array $expectedResult): void + public function testAdd(array $startingItems, mixed $itemToAdd, array $expectedResult, string $key = null): void { $collection = new Collection($startingItems); - $collection->add($itemToAdd); + $collection->add($itemToAdd, $key); $this->assertEquals($expectedResult, $collection->toArray()); } @@ -52,17 +52,26 @@ class CollectionTest extends TestCase 'startingItems' => ['A', 'B', 'C'], 'itemToAdd' => 'D', 'expectedResult' => ['A', 'B', 'C', 'D'], + 'key' => null, ], [ 'startingItems' => [], 'itemToAdd' => 1, 'expectedResult' => [1], + 'key' => null, ], [ 'startingItems' => [], 'itemToAdd' => null, 'expectedResult' => [], + 'key' => null, ], + [ + 'startingItems' => ['A' => 'A', 'B' => 'B', 'C' => 'C'], + 'itemToAdd' => 'D', + 'expectedResult' => ['A' => 'A', 'B' => 'B', 'C' => 'C', 'D' => 'D'], + 'key' => 'D', + ] ]; }