Skip to content
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

[9.x] Added callable support to operatorForWhere on Collection #41414

Merged
merged 3 commits into from
Mar 10, 2022
Merged
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
10 changes: 7 additions & 3 deletions src/Illuminate/Collections/Traits/EnumeratesValues.php
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ public function every($key, $operator = null, $value = null)
/**
* Get the first item by the given key value pair.
*
* @param string $key
* @param callable|string $key
* @param mixed $operator
* @param mixed $value
* @return TValue|null
Expand Down Expand Up @@ -548,7 +548,7 @@ public function unlessNotEmpty(callable $callback, callable $default = null)
/**
* Filter items by the given key value pair.
*
* @param string $key
* @param callable|string $key
* @param mixed $operator
* @param mixed $value
* @return static
Expand Down Expand Up @@ -995,13 +995,17 @@ protected function getArrayableItems($items)
/**
* Get an operator checker callback.
*
* @param string $key
* @param callable|string $key
* @param string|null $operator
* @param mixed $value
* @return \Closure
*/
protected function operatorForWhere($key, $operator = null, $value = null)
{
if (is_callable($key)) {
return $key;
}

if (func_num_args() === 1) {
$value = true;

Expand Down
15 changes: 15 additions & 0 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,11 @@ public function testFirstWhere($collection)
$this->assertSame('gasket', $data->firstWhere('material', 'rubber')['type']);
$this->assertNull($data->firstWhere('material', 'nonexistent'));
$this->assertNull($data->firstWhere('nonexistent', 'key'));

$this->assertSame('book', $data->firstWhere(fn ($value) => $value['material'] === 'paper')['type']);
$this->assertSame('gasket', $data->firstWhere(fn ($value) => $value['material'] === 'rubber')['type']);
$this->assertNull($data->firstWhere(fn ($value) => $value['material'] === 'nonexistent'));
$this->assertNull($data->firstWhere(fn ($value) => ($value['nonexistent'] ?? null) === 'key'));
}

/**
Expand Down Expand Up @@ -1000,6 +1005,16 @@ public function testWhere($collection)
$c->where('v', '>', $object)->values()->all()
);

$this->assertEquals(
[['v' => 3], ['v' => '3']],
$c->where(fn ($value) => $value['v'] == 3)->values()->all()
);

$this->assertEquals(
[['v' => 3]],
$c->where(fn ($value) => $value['v'] === 3)->values()->all()
);

$c = new $collection([['v' => 1], ['v' => $object]]);
$this->assertEquals(
[['v' => $object]],
Expand Down