Skip to content

Commit

Permalink
feat(eloquent): IteratorImpl (= all iterators) will implement `Coun…
Browse files Browse the repository at this point in the history
…table`.
  • Loading branch information
LastDragon-ru committed Dec 4, 2022
1 parent 8601470 commit f5a1373
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function __construct(Builder $builder, string $column = null) {
// it just adds conditional to the main query, and this leads to an
// infinite loop.
if ($this->hasUnions()) {
throw new InvalidArgumentException('Queries with UNION is not supported.');
throw new InvalidArgumentException('Query with UNION is not supported.');
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class ChunkedChangeSafeIteratorTest extends TestCase {
* @covers ::getIterator
* @covers ::getOffset
* @covers ::getIndex
* @covers ::count
*/
public function testGetIterator(): void {
TestObject::factory()->create(['value' => '1']);
Expand Down Expand Up @@ -74,6 +75,7 @@ public function testGetIterator(): void {

self::assertEquals(count($expected), $iterator->getIndex());
self::assertEquals(count($expected), $iterator->getOffset());
self::assertEquals(4, count($iterator));

$spyBefore
->shouldHaveBeenCalled()
Expand Down Expand Up @@ -133,7 +135,7 @@ public function testGetIteratorEloquentDefaults(string $column): void {
* @covers ::getIterator
*/
public function testGetIteratorUnion(): void {
self::expectExceptionObject(new InvalidArgumentException('Queries with UNION is not supported.'));
self::expectExceptionObject(new InvalidArgumentException('Query with UNION is not supported.'));

new ChunkedChangeSafeIterator(TestObject::query()->union(TestObject::query()->toBase()));
}
Expand Down
2 changes: 2 additions & 0 deletions packages/eloquent/src/Iterators/ChunkedIteratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class ChunkedIteratorTest extends TestCase {
* @covers ::getIterator
* @covers ::getOffset
* @covers ::getIndex
* @covers ::count
*/
public function testGetIterator(): void {
TestObject::factory()->create(['value' => '1']);
Expand Down Expand Up @@ -50,6 +51,7 @@ public function testGetIterator(): void {
self::assertEquals(2, count($log) - $count);
self::assertEquals(count($expected), $iterator->getIndex());
self::assertEquals(count($expected), $iterator->getOffset());
self::assertEquals(3, count($iterator));

$spyBefore
->shouldHaveBeenCalled()
Expand Down
8 changes: 7 additions & 1 deletion packages/eloquent/src/Iterators/IteratorImpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
namespace LastDragon_ru\LaraASP\Eloquent\Iterators;

use Closure;
use Countable;
use EmptyIterator;
use Generator;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Collection;
use LastDragon_ru\LaraASP\Core\Observer\Dispatcher;

use function max;
use function min;

/**
Expand All @@ -18,7 +20,7 @@
*
* @internal
*/
abstract class IteratorImpl implements Iterator {
abstract class IteratorImpl implements Iterator, Countable {
/**
* @var Dispatcher<Collection<array-key,TItem>>
*/
Expand Down Expand Up @@ -191,4 +193,8 @@ protected function getDefaultOffset(): ?int {
protected function getBuilder(): Builder {
return $this->builder;
}

public function count(): int {
return max(0, $this->getBuilder()->toBase()->count());
}
}

0 comments on commit f5a1373

Please sign in to comment.