Skip to content

Commit 1e70a82

Browse files
committed
fix conflicts
2 parents ad3a118 + a4ffe03 commit 1e70a82

6 files changed

+82
-13
lines changed

Collection.php

+34-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use ArrayAccess;
66
use ArrayIterator;
7+
use Illuminate\Collections\ItemNotFoundException;
8+
use Illuminate\Collections\MultipleItemsFoundException;
79
use Illuminate\Support\Traits\EnumeratesValues;
810
use Illuminate\Support\Traits\Macroable;
911
use stdClass;
@@ -260,7 +262,7 @@ public function diffKeysUsing($items, callable $callback)
260262
/**
261263
* Retrieve duplicate items from the collection.
262264
*
263-
* @param callable|null $callback
265+
* @param callable|string|null $callback
264266
* @param bool $strict
265267
* @return static
266268
*/
@@ -288,7 +290,7 @@ public function duplicates($callback = null, $strict = false)
288290
/**
289291
* Retrieve duplicate items from the collection using strict comparison.
290292
*
291-
* @param callable|null $callback
293+
* @param callable|string|null $callback
292294
* @return static
293295
*/
294296
public function duplicatesStrict($callback = null)
@@ -1050,6 +1052,36 @@ public function splitIn($numberOfGroups)
10501052
return $this->chunk(ceil($this->count() / $numberOfGroups));
10511053
}
10521054

1055+
/**
1056+
* Get the first item in the collection, but only if exactly one item exists. Otherwise, throw an exception.
1057+
*
1058+
* @param mixed $key
1059+
* @param mixed $operator
1060+
* @param mixed $value
1061+
* @return mixed
1062+
*
1063+
* @throws \Illuminate\Collections\ItemNotFoundException
1064+
* @throws \Illuminate\Collections\MultipleItemsFoundException
1065+
*/
1066+
public function sole($key = null, $operator = null, $value = null)
1067+
{
1068+
$filter = func_num_args() > 1
1069+
? $this->operatorForWhere(...func_get_args())
1070+
: $key;
1071+
1072+
$items = $this->when($filter)->filter($filter);
1073+
1074+
if ($items->isEmpty()) {
1075+
throw new ItemNotFoundException;
1076+
}
1077+
1078+
if ($items->count() > 1) {
1079+
throw new MultipleItemsFoundException;
1080+
}
1081+
1082+
return $items->first();
1083+
}
1084+
10531085
/**
10541086
* Chunk the collection into chunks of the given size.
10551087
*

Enumerable.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ public function diffKeysUsing($items, callable $callback);
211211
/**
212212
* Retrieve duplicate items.
213213
*
214-
* @param callable|null $callback
214+
* @param callable|string|null $callback
215215
* @param bool $strict
216216
* @return static
217217
*/
@@ -220,7 +220,7 @@ public function duplicates($callback = null, $strict = false);
220220
/**
221221
* Retrieve duplicate items using strict comparison.
222222
*
223-
* @param callable|null $callback
223+
* @param callable|string|null $callback
224224
* @return static
225225
*/
226226
public function duplicatesStrict($callback = null);

ItemNotFoundException.php

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Illuminate\Collections;
4+
5+
use RuntimeException;
6+
7+
class ItemNotFoundException extends RuntimeException
8+
{
9+
}

LazyCollection.php

+27-2
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ public function diffKeysUsing($items, callable $callback)
316316
/**
317317
* Retrieve duplicate items.
318318
*
319-
* @param callable|null $callback
319+
* @param callable|string|null $callback
320320
* @param bool $strict
321321
* @return static
322322
*/
@@ -328,7 +328,7 @@ public function duplicates($callback = null, $strict = false)
328328
/**
329329
* Retrieve duplicate items using strict comparison.
330330
*
331-
* @param callable|null $callback
331+
* @param callable|string|null $callback
332332
* @return static
333333
*/
334334
public function duplicatesStrict($callback = null)
@@ -1010,6 +1010,31 @@ public function split($numberOfGroups)
10101010
return $this->passthru('split', func_get_args());
10111011
}
10121012

1013+
/**
1014+
* Get the first item in the collection, but only if exactly one item exists. Otherwise, throw an exception.
1015+
*
1016+
* @param mixed $key
1017+
* @param mixed $operator
1018+
* @param mixed $value
1019+
* @return mixed
1020+
*
1021+
* @throws \Illuminate\Collections\ItemNotFoundException
1022+
* @throws \Illuminate\Collections\MultipleItemsFoundException
1023+
*/
1024+
public function sole($key = null, $operator = null, $value = null)
1025+
{
1026+
$filter = func_num_args() > 1
1027+
? $this->operatorForWhere(...func_get_args())
1028+
: $key;
1029+
1030+
return $this
1031+
->when($filter)
1032+
->filter($filter)
1033+
->take(2)
1034+
->collect()
1035+
->sole();
1036+
}
1037+
10131038
/**
10141039
* Chunk the collection into chunks of the given size.
10151040
*

MultipleItemsFoundException.php

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Illuminate\Collections;
4+
5+
use RuntimeException;
6+
7+
class MultipleItemsFoundException extends RuntimeException
8+
{
9+
}

Traits/EnumeratesValues.php

+1-7
Original file line numberDiff line numberDiff line change
@@ -740,13 +740,7 @@ public function reduce(callable $callback, $initial = null)
740740
*/
741741
public function reduceWithKeys(callable $callback, $initial = null)
742742
{
743-
$result = $initial;
744-
745-
foreach ($this as $key => $value) {
746-
$result = $callback($result, $value, $key);
747-
}
748-
749-
return $result;
743+
return $this->reduce($callback, $initial);
750744
}
751745

752746
/**

0 commit comments

Comments
 (0)