File tree Expand file tree Collapse file tree 2 files changed +62
-1
lines changed Expand file tree Collapse file tree 2 files changed +62
-1
lines changed Original file line number Diff line number Diff line change @@ -1326,11 +1326,37 @@ public function offsetSet($key, $value)
13261326 /**
13271327 * Unset the item at a given offset.
13281328 *
1329- * @param string $key
1329+ * @param string $key
13301330 * @return void
13311331 */
13321332 public function offsetUnset ($ key )
13331333 {
13341334 unset($ this ->items [$ key ]);
13351335 }
1336+
1337+ /**
1338+ * Take items in the collection until condition is met.
1339+ *
1340+ * @param mixed $key
1341+ * @return static
1342+ */
1343+ public function until ($ value )
1344+ {
1345+ $ passed = [];
1346+
1347+ $ callback = $ this ->useAsCallable ($ value ) ? $ value :
1348+ function ($ item ) use ($ value ) {
1349+ return $ item === $ value ;
1350+ };
1351+
1352+ foreach ($ this as $ key => $ item ) {
1353+ if ($ callback ($ item , $ key )) {
1354+ break ;
1355+ }
1356+
1357+ $ passed [$ key ] = $ item ;
1358+ }
1359+
1360+ return new static ($ passed );
1361+ }
13361362}
Original file line number Diff line number Diff line change @@ -4070,6 +4070,41 @@ public function testCollect($collection)
40704070 ], $ data ->all ());
40714071 }
40724072
4073+ public function testUntilUsingValue ()
4074+ {
4075+ $ data = new Collection ([1 , 2 , 3 , 4 ]);
4076+
4077+ $ data = $ data ->until (3 );
4078+
4079+ $ this ->assertSame ([1 , 2 ], $ data ->toArray ());
4080+ }
4081+
4082+ public function testUntilUsingCallback ()
4083+ {
4084+ $ data = new Collection ([1 , 2 , 3 , 4 ]);
4085+
4086+ $ data = $ data ->until (function ($ item ) {
4087+ return $ item >= 3 ;
4088+ });
4089+
4090+ $ this ->assertSame ([1 , 2 ], $ data ->toArray ());
4091+ }
4092+
4093+ public function testUntilReturnsAllItemsForUnmetValue ()
4094+ {
4095+ $ data = new Collection ([1 , 2 , 3 , 4 ]);
4096+
4097+ $ actual = $ data ->until (99 );
4098+
4099+ $ this ->assertSame ($ data ->toArray (), $ actual ->toArray ());
4100+
4101+ $ actual = $ data ->until (function ($ item ) {
4102+ return $ item >= 99 ;
4103+ });
4104+
4105+ $ this ->assertSame ($ data ->toArray (), $ actual ->toArray ());
4106+ }
4107+
40734108 /**
40744109 * Provides each collection class, respectively.
40754110 *
You can’t perform that action at this time.
0 commit comments