File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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 */
Original file line number Diff line number Diff line change 1515use ArrayObject ;
1616use Hyperf \Collection \Arr ;
1717use Hyperf \Collection \Collection ;
18+ use Hyperf \Collection \ItemNotFoundException ;
19+ use Hyperf \Collection \MultipleItemsFoundException ;
1820use Hyperf \Coroutine \Coroutine ;
1921use Hyperf \Di \Resolver \ResolverDispatcher ;
2022use 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}
You can’t perform that action at this time.
0 commit comments