Skip to content

Commit 688da04

Browse files
authored
Merge pull request #34444 from JosephSilber/take-until-timeout
[8.x] Add `takeUntilTimeout` method to the lazy collection
2 parents 194cdb9 + 4a52e9a commit 688da04

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

src/Illuminate/Collections/LazyCollection.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use ArrayIterator;
66
use Closure;
7+
use DateTimeInterface;
78
use Illuminate\Support\Traits\EnumeratesValues;
89
use Illuminate\Support\Traits\Macroable;
910
use IteratorAggregate;
@@ -1213,6 +1214,21 @@ public function takeUntil($value)
12131214
});
12141215
}
12151216

1217+
/**
1218+
* Take items in the collection until a given point in time.
1219+
*
1220+
* @param \DateTimeInterface $timeout
1221+
* @return static
1222+
*/
1223+
public function takeUntilTimeout(DateTimeInterface $timeout)
1224+
{
1225+
$timeout = $timeout->getTimestamp();
1226+
1227+
return $this->takeWhile(function () use ($timeout) {
1228+
return $this->now() < $timeout;
1229+
});
1230+
}
1231+
12161232
/**
12171233
* Take items in the collection while the given condition is met.
12181234
*
@@ -1385,4 +1401,14 @@ protected function passthru($method, array $params)
13851401
yield from $this->collect()->$method(...$params);
13861402
});
13871403
}
1404+
1405+
/**
1406+
* Get the current time.
1407+
*
1408+
* @return int
1409+
*/
1410+
protected function now()
1411+
{
1412+
return time();
1413+
}
13881414
}

tests/Support/SupportLazyCollectionTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
namespace Illuminate\Tests\Support;
44

5+
use Carbon\Carbon;
56
use Illuminate\Support\Collection;
67
use Illuminate\Support\LazyCollection;
8+
use Mockery as m;
79
use PHPUnit\Framework\TestCase;
810

911
class SupportLazyCollectionTest extends TestCase
@@ -154,6 +156,36 @@ public function testRememberWithDuplicateKeys()
154156
$this->assertSame([['key', 1], ['key', 2]], $results);
155157
}
156158

159+
public function testTakeUntilTimeout()
160+
{
161+
$timeout = Carbon::now();
162+
163+
$mock = m::mock(LazyCollection::class.'[now]');
164+
165+
$results = $mock
166+
->times(10)
167+
->pipe(function ($collection) use ($mock, $timeout) {
168+
tap($collection)
169+
->mockery_init($mock->mockery_getContainer())
170+
->shouldAllowMockingProtectedMethods()
171+
->shouldReceive('now')
172+
->times(3)
173+
->andReturn(
174+
(clone $timeout)->sub(2, 'minute')->getTimestamp(),
175+
(clone $timeout)->sub(1, 'minute')->getTimestamp(),
176+
$timeout->getTimestamp()
177+
);
178+
179+
return $collection;
180+
})
181+
->takeUntilTimeout($timeout)
182+
->all();
183+
184+
$this->assertSame([1, 2], $results);
185+
186+
m::close();
187+
}
188+
157189
public function testTapEach()
158190
{
159191
$data = LazyCollection::times(10);

0 commit comments

Comments
 (0)