From 54f21372740ab12b81cba66c890fdf046d67eaec Mon Sep 17 00:00:00 2001 From: Jason McCreary Date: Tue, 7 Apr 2020 13:00:18 -0400 Subject: [PATCH] Add until to HigherOrder and LazyCollection --- src/Illuminate/Support/Collection.php | 26 ----------- .../Support/Traits/EnumeratesValues.php | 29 +++++++++++- tests/Support/SupportCollectionTest.php | 44 ++++++++++++++++--- 3 files changed, 66 insertions(+), 33 deletions(-) diff --git a/src/Illuminate/Support/Collection.php b/src/Illuminate/Support/Collection.php index 9de7bc9fe99e..32373cdc86f0 100644 --- a/src/Illuminate/Support/Collection.php +++ b/src/Illuminate/Support/Collection.php @@ -1333,30 +1333,4 @@ public function offsetUnset($key) { unset($this->items[$key]); } - - /** - * Take items in the collection until condition is met. - * - * @param mixed $key - * @return static - */ - public function until($value) - { - $passed = []; - - $callback = $this->useAsCallable($value) ? $value : - function ($item) use ($value) { - return $item === $value; - }; - - foreach ($this as $key => $item) { - if ($callback($item, $key)) { - break; - } - - $passed[$key] = $item; - } - - return new static($passed); - } } diff --git a/src/Illuminate/Support/Traits/EnumeratesValues.php b/src/Illuminate/Support/Traits/EnumeratesValues.php index 83cef4b41dd6..7d143a48fe80 100644 --- a/src/Illuminate/Support/Traits/EnumeratesValues.php +++ b/src/Illuminate/Support/Traits/EnumeratesValues.php @@ -36,6 +36,7 @@ * @property-read HigherOrderCollectionProxy $sortByDesc * @property-read HigherOrderCollectionProxy $sum * @property-read HigherOrderCollectionProxy $unique + * @property-read HigherOrderCollectionProxy $until */ trait EnumeratesValues { @@ -47,7 +48,7 @@ trait EnumeratesValues protected static $proxies = [ 'average', 'avg', 'contains', 'each', 'every', 'filter', 'first', 'flatMap', 'groupBy', 'keyBy', 'map', 'max', 'min', 'partition', - 'reject', 'some', 'sortBy', 'sortByDesc', 'sum', 'unique', + 'reject', 'some', 'sortBy', 'sortByDesc', 'sum', 'unique', 'until', ]; /** @@ -703,6 +704,32 @@ public function uniqueStrict($key = null) return $this->unique($key, true); } + /** + * Take items in the collection until condition is met. + * + * @param mixed $key + * @return static + */ + public function until($value) + { + $passed = []; + + $callback = $this->useAsCallable($value) ? $value : + function ($item) use ($value) { + return $item === $value; + }; + + foreach ($this as $key => $item) { + if ($callback($item, $key)) { + break; + } + + $passed[$key] = $item; + } + + return new static($passed); + } + /** * Collect the values into a collection. * diff --git a/tests/Support/SupportCollectionTest.php b/tests/Support/SupportCollectionTest.php index 5dfc524c7666..88f72af89fd7 100755 --- a/tests/Support/SupportCollectionTest.php +++ b/tests/Support/SupportCollectionTest.php @@ -4070,18 +4070,24 @@ public function testCollect($collection) ], $data->all()); } - public function testUntilUsingValue() + /** + * @dataProvider collectionClassProvider + */ + public function testUntilUsingValue($collection) { - $data = new Collection([1, 2, 3, 4]); + $data = new $collection([1, 2, 3, 4]); $data = $data->until(3); $this->assertSame([1, 2], $data->toArray()); } - public function testUntilUsingCallback() + /** + * @dataProvider collectionClassProvider + */ + public function testUntilUsingCallback($collection) { - $data = new Collection([1, 2, 3, 4]); + $data = new $collection([1, 2, 3, 4]); $data = $data->until(function ($item) { return $item >= 3; @@ -4090,9 +4096,12 @@ public function testUntilUsingCallback() $this->assertSame([1, 2], $data->toArray()); } - public function testUntilReturnsAllItemsForUnmetValue() + /** + * @dataProvider collectionClassProvider + */ + public function testUntilReturnsAllItemsForUnmetValue($collection) { - $data = new Collection([1, 2, 3, 4]); + $data = new $collection([1, 2, 3, 4]); $actual = $data->until(99); @@ -4105,6 +4114,24 @@ public function testUntilReturnsAllItemsForUnmetValue() $this->assertSame($data->toArray(), $actual->toArray()); } + /** + * @dataProvider collectionClassProvider + */ + public function testUntilCanBeProxied($collection) + { + $data = new $collection([ + new TestSupportCollectionHigherOrderItem('Adam'), + new TestSupportCollectionHigherOrderItem('Taylor'), + new TestSupportCollectionHigherOrderItem('Jason'), + ]); + + $actual = $data->until->is('Jason'); + + $this->assertCount(2, $actual); + $this->assertSame('Adam', $actual->get(0)->name); + $this->assertSame('Taylor', $actual->get(1)->name); + } + /** * Provides each collection class, respectively. * @@ -4132,6 +4159,11 @@ public function uppercase() { return $this->name = strtoupper($this->name); } + + public function is($name) + { + return $this->name === $name; + } } class TestAccessorEloquentTestStub