Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Add takeUntilTimeout method to the lazy collection #34444

Merged
merged 3 commits into from
Sep 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/Illuminate/Collections/LazyCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use ArrayIterator;
use Closure;
use DateTimeInterface;
use Illuminate\Support\Traits\EnumeratesValues;
use Illuminate\Support\Traits\Macroable;
use IteratorAggregate;
Expand Down Expand Up @@ -1213,6 +1214,21 @@ public function takeUntil($value)
});
}

/**
* Take items in the collection until a given point in time.
*
* @param \DateTimeInterface $timeout
* @return static
*/
public function takeUntilTimeout(DateTimeInterface $timeout)
{
$timeout = $timeout->getTimestamp();

return $this->takeWhile(function () use ($timeout) {
return $this->now() < $timeout;
});
}

/**
* Take items in the collection while the given condition is met.
*
Expand Down Expand Up @@ -1385,4 +1401,14 @@ protected function passthru($method, array $params)
yield from $this->collect()->$method(...$params);
});
}

/**
* Get the current time.
*
* @return int
*/
protected function now()
{
JosephSilber marked this conversation as resolved.
Show resolved Hide resolved
return time();
}
}
32 changes: 32 additions & 0 deletions tests/Support/SupportLazyCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace Illuminate\Tests\Support;

use Carbon\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\LazyCollection;
use Mockery as m;
use PHPUnit\Framework\TestCase;

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

public function testTakeUntilTimeout()
{
$timeout = Carbon::now();

$mock = m::mock(LazyCollection::class.'[now]');

$results = $mock
->times(10)
->pipe(function ($collection) use ($mock, $timeout) {
tap($collection)
->mockery_init($mock->mockery_getContainer())
->shouldAllowMockingProtectedMethods()
->shouldReceive('now')
->times(3)
->andReturn(
(clone $timeout)->sub(2, 'minute')->getTimestamp(),
(clone $timeout)->sub(1, 'minute')->getTimestamp(),
$timeout->getTimestamp()
);

return $collection;
})
->takeUntilTimeout($timeout)
->all();

$this->assertSame([1, 2], $results);

m::close();
}

public function testTapEach()
{
$data = LazyCollection::times(10);
Expand Down