From 7439be84dc0912cf46d0058d6ee180603a4b31ba Mon Sep 17 00:00:00 2001 From: Deeka Wong Date: Thu, 23 May 2024 13:47:31 +0800 Subject: [PATCH] Added `getOrPut` and `getOrSet` into `Hyperf\Collection\Collection`. (#6784) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: 李铭昕 <715557344@qq.com> --- CHANGELOG-3.1.md | 1 + src/collection/src/Collection.php | 32 +++++++++++++++++++++++++ src/collection/tests/CollectionTest.php | 31 ++++++++++++++++++++++++ 3 files changed, 64 insertions(+) diff --git a/CHANGELOG-3.1.md b/CHANGELOG-3.1.md index c6d323b27..db9cc3fbf 100644 --- a/CHANGELOG-3.1.md +++ b/CHANGELOG-3.1.md @@ -8,6 +8,7 @@ - [#6774](https://github.com/hyperf/hyperf/pull/6774) Support Lateral Join for `Hyperf\Database\Query\Builder` - [#6781](https://github.com/hyperf/hyperf/pull/6781) Added some methods to `Hyperf\Collection\Arr`. - [#6783](https://github.com/hyperf/hyperf/pull/6783) Support `insertOrIgnoreUsing` for `Hyperf\Database\Query\Builder`. +- [#6784](https://github.com/hyperf/hyperf/pull/6784) Added `getOrPut` and `getOrSet` into `Hyperf\Collection\Collection`. ## Optimized diff --git a/src/collection/src/Collection.php b/src/collection/src/Collection.php index 11026ac45..c6b560c96 100644 --- a/src/collection/src/Collection.php +++ b/src/collection/src/Collection.php @@ -423,6 +423,38 @@ class Collection implements Enumerable, ArrayAccess return value($default); } + /** + * Get an item from the collection by key or add it to collection if it does not exist. + * + * @template TGetOrPutValue + * + * @param (Closure(): TGetOrPutValue)|TGetOrPutValue $value + * @return TGetOrPutValue|TValue + */ + public function getOrPut(int|string $key, mixed $value): mixed + { + if (array_key_exists($key, $this->items)) { + return $this->items[$key]; + } + + $this->offsetSet($key, $value = value($value)); + + return $value; + } + + /** + * Get an item from the collection by key or add it to collection if it does not exist. + * + * @template TGetOrPutValue + * + * @param (Closure(): TGetOrPutValue)|TGetOrPutValue $value + * @return TGetOrPutValue|TValue + */ + public function getOrSet(int|string $key, mixed $value): mixed + { + return $this->getOrPut($key, $value); + } + /** * Group an associative array by a field or using a callback. * @param mixed $groupBy diff --git a/src/collection/tests/CollectionTest.php b/src/collection/tests/CollectionTest.php index d7d8319a9..5dfd98eba 100644 --- a/src/collection/tests/CollectionTest.php +++ b/src/collection/tests/CollectionTest.php @@ -948,4 +948,35 @@ class CollectionTest extends TestCase $duplicates = $collection::make([new $collection(['laravel']), $expected, $expected, [], '2', '2'])->duplicatesStrict()->all(); $this->assertSame([2 => $expected, 5 => '2'], $duplicates); } + + public function testGetOrPut() + { + $data = new Collection(['name' => 'taylor', 'email' => 'foo']); + + $this->assertSame('taylor', $data->getOrPut('name', null)); + $this->assertSame('foo', $data->getOrPut('email', null)); + $this->assertSame('male', $data->getOrPut('gender', 'male')); + + $this->assertSame('taylor', $data->get('name')); + $this->assertSame('foo', $data->get('email')); + $this->assertSame('male', $data->get('gender')); + + $data = new Collection(['name' => 'taylor', 'email' => 'foo']); + + $this->assertSame('taylor', $data->getOrPut('name', function () { + return null; + })); + + $this->assertSame('foo', $data->getOrPut('email', function () { + return null; + })); + + $this->assertSame('male', $data->getOrPut('gender', function () { + return 'male'; + })); + + $this->assertSame('taylor', $data->get('name')); + $this->assertSame('foo', $data->get('email')); + $this->assertSame('male', $data->get('gender')); + } }