Added `Hyperf\Collection\Arr::sole()` method by huangdijia · Pull Request #7600 · hyperf/hyperf
Expand Up
@@ -15,6 +15,8 @@
use ArrayObject;
use Hyperf\Collection\Arr;
use Hyperf\Collection\Collection;
use Hyperf\Collection\ItemNotFoundException;
use Hyperf\Collection\MultipleItemsFoundException;
use Hyperf\Coroutine\Coroutine;
use Hyperf\Di\Resolver\ResolverDispatcher;
use InvalidArgumentException;
Expand Down
Expand Up
@@ -1006,4 +1008,33 @@ public function testPush(): void
$array = ['foo' => ['bar' => false]];
Arr::push($array, 'foo.bar', 'baz');
}
public function testSoleReturnsFirstItemInCollectionIfOnlyOneExists() { $this->assertSame('foo', Arr::sole(['foo']));
$array = [ ['name' => 'foo'], ['name' => 'bar'], ];
$this->assertSame( ['name' => 'foo'], Arr::sole($array, fn (array $value) => $value['name'] === 'foo') ); }
public function testSoleThrowsExceptionIfNoItemsExist() { $this->expectException(ItemNotFoundException::class);
Arr::sole(['foo'], fn (string $value) => $value === 'baz'); }
public function testSoleThrowsExceptionIfMoreThanOneItemExists() { $this->expectExceptionObject(new MultipleItemsFoundException(2));
Arr::sole(['baz', 'foo', 'baz'], fn (string $value) => $value === 'baz'); } }
public function testSoleReturnsFirstItemInCollectionIfOnlyOneExists() { $this->assertSame('foo', Arr::sole(['foo']));
$array = [ ['name' => 'foo'], ['name' => 'bar'], ];
$this->assertSame( ['name' => 'foo'], Arr::sole($array, fn (array $value) => $value['name'] === 'foo') ); }
public function testSoleThrowsExceptionIfNoItemsExist() { $this->expectException(ItemNotFoundException::class);
Arr::sole(['foo'], fn (string $value) => $value === 'baz'); }
public function testSoleThrowsExceptionIfMoreThanOneItemExists() { $this->expectExceptionObject(new MultipleItemsFoundException(2));
Arr::sole(['baz', 'foo', 'baz'], fn (string $value) => $value === 'baz'); } }