Skip to content

Commit

Permalink
[9.x] Added callable support to operatorForWhere on Collection (#41414)
Browse files Browse the repository at this point in the history
* [9.x] Added callable support to operatorForWhere on Collection

Added `callable` support to `operatorForWhere`.

Currently only Collection methods `where` and `whereFirst` are used without `callable` support.

Adding callable to `operatorForWhere` allow complex usages as:

```php
$city = $cities->firstWhere(fn ($city) => $city->state->available && ($city->state->id === $stateId));
```
or
```php
$state = $states->firstWhere(fn ($state) => (bool)$state->cities->firstWhere('id', $cityId));
```

* Added callable test to `where` and `firstWhere` collection methods

* Fixed firstWhere callable test typo
  • Loading branch information
eusonlito committed Mar 10, 2022
1 parent 5eac9f3 commit eee6f5f
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions 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

0 comments on commit eee6f5f

Please sign in to comment.