From 208c9db4c5b68009f587f07dfc6616c2fbcd66e7 Mon Sep 17 00:00:00 2001 From: "John Paul E. Balandan, CPA" Date: Sat, 7 Sep 2024 00:45:59 +0800 Subject: [PATCH] Add `keys` operation --- src/Nexus/Collection/Collection.php | 9 +++++ src/Nexus/Collection/CollectionInterface.php | 2 ++ src/Nexus/Collection/Operation/Keys.php | 36 +++++++++++++++++++ .../Collection/AbstractCollectionTestCase.php | 13 +++++++ 4 files changed, 60 insertions(+) create mode 100644 src/Nexus/Collection/Operation/Keys.php diff --git a/src/Nexus/Collection/Collection.php b/src/Nexus/Collection/Collection.php index d7a3d5b..e5d3a4f 100644 --- a/src/Nexus/Collection/Collection.php +++ b/src/Nexus/Collection/Collection.php @@ -98,6 +98,15 @@ public function getIterator(): \Traversable yield from $this->innerIterator->getIterator(); } + public function keys(): CollectionInterface + { + return new self(static function (iterable $collection): iterable { + foreach ($collection as $key => $_) { + yield $key; + } + }, [$this]); + } + public function values(): CollectionInterface { return new self(static function (iterable $collection): iterable { diff --git a/src/Nexus/Collection/CollectionInterface.php b/src/Nexus/Collection/CollectionInterface.php index 04ac70b..c3ef1bd 100644 --- a/src/Nexus/Collection/CollectionInterface.php +++ b/src/Nexus/Collection/CollectionInterface.php @@ -19,12 +19,14 @@ * * @extends \IteratorAggregate * @extends Operation\All + * @extends Operation\Keys * @extends Operation\Values */ interface CollectionInterface extends \Countable, \IteratorAggregate, Operation\All, + Operation\Keys, Operation\Values { /** diff --git a/src/Nexus/Collection/Operation/Keys.php b/src/Nexus/Collection/Operation/Keys.php new file mode 100644 index 0000000..e88245b --- /dev/null +++ b/src/Nexus/Collection/Operation/Keys.php @@ -0,0 +1,36 @@ + + * + * For the full copyright and license information, please view + * the LICENSE file that was distributed with this source code. + */ + +namespace Nexus\Collection\Operation; + +use Nexus\Collection\CollectionInterface; + +/** + * @template TKey + * @template T + */ +interface Keys +{ + /** + * Returns a new collection from keys of the original collection as + * the new values. + * + * ``` + * Collection::wrap(['banana' => 1, 'apple' => 2])->keys(); + * => Collection(['banana', 'apple']) + * ``` + * + * @return CollectionInterface + */ + public function keys(): CollectionInterface; +} diff --git a/tests/Collection/AbstractCollectionTestCase.php b/tests/Collection/AbstractCollectionTestCase.php index ec742f8..1b6f60f 100644 --- a/tests/Collection/AbstractCollectionTestCase.php +++ b/tests/Collection/AbstractCollectionTestCase.php @@ -55,6 +55,19 @@ public function testCount(): void self::assertCount(2, $this->collection([1, 2])); } + public function testKeys(): void + { + $collection = $this->collection(static function (): \Generator { + yield 'bananas' => 5; + + yield 'apples' => 4; + + yield 'oranges' => 7; + }); + + self::assertSame(['bananas', 'apples', 'oranges'], $collection->keys()->all()); + } + public function testValues(): void { $collection = $this->collection(static function (): \Generator {