Skip to content

Commit 4be698f

Browse files
committed
Merge branch 'master' into 3.2-merge
2 parents 6f98515 + 8f43bd7 commit 4be698f

2 files changed

Lines changed: 52 additions & 0 deletions

File tree

src/Arr.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,27 @@ public static function shuffleAssoc(array $array, ?int $seed = null): array
598598
return $random;
599599
}
600600

601+
/**
602+
* Get the first item in the collection, but only if exactly one item exists. Otherwise, throw an exception.
603+
*
604+
* @param array $array
605+
*
606+
* @throws ItemNotFoundException
607+
* @throws MultipleItemsFoundException
608+
*/
609+
public static function sole($array, ?callable $callback = null)
610+
{
611+
if ($callback) {
612+
$array = static::where($array, $callback);
613+
}
614+
615+
return match (count($array)) {
616+
0 => throw new ItemNotFoundException(),
617+
1 => static::first($array),
618+
default => throw new MultipleItemsFoundException(count($array)),
619+
};
620+
}
621+
601622
/**
602623
* Sort the array using the given callback or "dot" notation.
603624
*/

tests/ArrTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
use ArrayObject;
1616
use Hyperf\Collection\Arr;
1717
use Hyperf\Collection\Collection;
18+
use Hyperf\Collection\ItemNotFoundException;
19+
use Hyperf\Collection\MultipleItemsFoundException;
1820
use Hyperf\Coroutine\Coroutine;
1921
use Hyperf\Di\Resolver\ResolverDispatcher;
2022
use InvalidArgumentException;
@@ -1058,4 +1060,33 @@ public function testPush(): void
10581060
$array = ['foo' => ['bar' => false]];
10591061
Arr::push($array, 'foo.bar', 'baz');
10601062
}
1063+
1064+
public function testSoleReturnsFirstItemInCollectionIfOnlyOneExists()
1065+
{
1066+
$this->assertSame('foo', Arr::sole(['foo']));
1067+
1068+
$array = [
1069+
['name' => 'foo'],
1070+
['name' => 'bar'],
1071+
];
1072+
1073+
$this->assertSame(
1074+
['name' => 'foo'],
1075+
Arr::sole($array, fn (array $value) => $value['name'] === 'foo')
1076+
);
1077+
}
1078+
1079+
public function testSoleThrowsExceptionIfNoItemsExist()
1080+
{
1081+
$this->expectException(ItemNotFoundException::class);
1082+
1083+
Arr::sole(['foo'], fn (string $value) => $value === 'baz');
1084+
}
1085+
1086+
public function testSoleThrowsExceptionIfMoreThanOneItemExists()
1087+
{
1088+
$this->expectExceptionObject(new MultipleItemsFoundException(2));
1089+
1090+
Arr::sole(['baz', 'foo', 'baz'], fn (string $value) => $value === 'baz');
1091+
}
10611092
}

0 commit comments

Comments
 (0)