Skip to content

Commit 2d3b63c

Browse files
committed
Added operator suport for the sole() method.
1 parent 3c4f386 commit 2d3b63c

File tree

3 files changed

+31
-17
lines changed

3 files changed

+31
-17
lines changed

src/Illuminate/Collections/Collection.php

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,25 +1056,31 @@ public function splitIn($numberOfGroups)
10561056
* Get the first item in the collection, but only if exactly
10571057
* item exists. Otherwise, throw an exception.
10581058
*
1059-
* @param callable|null $callback
1059+
* @param mixed $key
1060+
* @param mixed $operator
1061+
* @param mixed $value
10601062
* @return mixed
10611063
*
10621064
* @throws \Illuminate\Collections\ItemNotFoundException
10631065
* @throws \Illuminate\Collections\MultipleItemsFoundException
10641066
*/
1065-
public function sole(callable $callback = null)
1067+
public function sole($key = null, $operator = null, $value = null)
10661068
{
1067-
$items = $this->when($callback)->filter($callback);
1069+
if (func_num_args() <= 1) {
1070+
$items = $this->when($key)->filter($key);
10681071

1069-
if ($items->isEmpty()) {
1070-
throw new ItemNotFoundException;
1071-
}
1072+
if ($items->isEmpty()) {
1073+
throw new ItemNotFoundException;
1074+
}
1075+
1076+
if ($items->count() > 1) {
1077+
throw new MultipleItemsFoundException;
1078+
}
10721079

1073-
if ($items->count() > 1) {
1074-
throw new MultipleItemsFoundException;
1080+
return $items->first();
10751081
}
10761082

1077-
return $items->first();
1083+
return $this->sole($this->operatorForWhere(...func_get_args()));
10781084
}
10791085

10801086
/**

src/Illuminate/Collections/LazyCollection.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,20 +1014,26 @@ public function split($numberOfGroups)
10141014
* Get the first item in the collection, but only if exactly
10151015
* item exists. Otherwise, throw an exception.
10161016
*
1017-
* @param callable|null $callback
1017+
* @param mixed $key
1018+
* @param mixed $operator
1019+
* @param mixed $value
10181020
* @return mixed
10191021
*
10201022
* @throws \Illuminate\Collections\ItemNotFoundException
10211023
* @throws \Illuminate\Collections\MultipleItemsFoundException
10221024
*/
1023-
public function sole(callable $callback = null)
1025+
public function sole($key = null, $operator = null, $value = null)
10241026
{
1025-
return $this
1026-
->when($callback)
1027-
->filter($callback)
1028-
->take(2)
1029-
->collect()
1030-
->sole();
1027+
if (func_num_args() <= 1) {
1028+
return $this
1029+
->when($key)
1030+
->filter($key)
1031+
->take(2)
1032+
->collect()
1033+
->sole();
1034+
}
1035+
1036+
return $this->sole($this->operatorForWhere(...func_get_args()));
10311037
}
10321038

10331039
/**

tests/Support/SupportCollectionTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ public function testSoleReturnsFirstItemInCollectionIfOnlyOneExists($collection)
7979
]);
8080

8181
$this->assertSame(['name' => 'foo'], $collection->where('name', 'foo')->sole());
82+
$this->assertSame(['name' => 'foo'], $collection->sole('name', '=', 'foo'));
83+
$this->assertSame(['name' => 'foo'], $collection->sole('name', 'foo'));
8284
}
8385

8486
/**

0 commit comments

Comments
 (0)