Skip to content

[12.x] Adds firstValue() to Collection #55289

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/Illuminate/Collections/Traits/EnumeratesValues.php
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,33 @@ public function firstWhere($key, $operator = null, $value = null)
return $this->first($this->operatorForWhere(...func_get_args()));
}

/**
* Returns the first value that is not null from the given callback.
*
* @template TCallbackValue
* @template TValueDefault
*
* @param callable(TValue, TKey): TCallbackValue|null $callback
* @param TValueDefault|(\Closure(): TValueDefault) $default
* @return TCallbackValue|TValueDefault
*/
public function firstValue($callback, $default = null)
{
$value = null;

$this->each(function ($item, $key) use (&$value, $callback) {
$result = $callback($item, $key);

if (! is_null($result)) {
$value = $result;

return false;
}
});

return $value ?? value($default);
}

/**
* Get a single key's value from the first matching item in the collection.
*
Expand Down
52 changes: 52 additions & 0 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,58 @@ public function testFirstWithDefaultAndWithoutCallback($collection)
$this->assertSame('foo', $result);
}

#[DataProvider('collectionClassProvider')]
public function testFirstValueReturnsValue($collection)
{
$data = new $collection([
['index' => 1, 'value' => 'foo'],
['index' => 2, 'value' => 'bar'],
['index' => 3, 'value' => 'baz'],
]);

$result = $data->firstValue(function ($item) {
if ($item['index'] === 3) {
return $item['value'];
}
});

$this->assertSame('baz', $result);

$result = $data->firstValue(function ($item) {
if ($item['index'] === 4) {
return $item['value'];
}
});

$this->assertNull($result);
}

#[DataProvider('collectionClassProvider')]
public function testFirstValueWithDefaultReturnsValue($collection)
{
$data = new $collection([
['index' => 1, 'value' => 'foo'],
['index' => 2, 'value' => 'bar'],
['index' => 3, 'value' => 'baz'],
]);

$result = $data->firstValue(function ($item) {
if ($item['index'] === 3) {
return $item['value'];
}
}, 'default');

$this->assertSame('baz', $result);

$result = $data->firstValue(function ($item) {
if ($item['index'] === 4) {
return $item['value'];
}
}, 'default');

$this->assertSame('default', $result);
}

#[DataProvider('collectionClassProvider')]
public function testSoleReturnsFirstItemInCollectionIfOnlyOneExists($collection)
{
Expand Down
Loading