diff --git a/src/Resources/CursorCollection.php b/src/Resources/CursorCollection.php index 0f83c7e5..3168bd72 100644 --- a/src/Resources/CursorCollection.php +++ b/src/Resources/CursorCollection.php @@ -106,6 +106,8 @@ public function getAutoIterator(bool $iterateBackwards = false): LazyCollection { $page = $this; + var_dump(get_class($page)); + die; return new LazyCollection(function () use ($page, $iterateBackwards): Generator { while (true) { foreach ($page as $item) { diff --git a/src/Resources/LazyCollection.php b/src/Resources/LazyCollection.php index f555f086..a23eafe7 100644 --- a/src/Resources/LazyCollection.php +++ b/src/Resources/LazyCollection.php @@ -2,9 +2,8 @@ namespace Mollie\Api\Resources; -use ArrayIterator; +use Iterator; use IteratorAggregate; -use Traversable; class LazyCollection implements IteratorAggregate { @@ -48,7 +47,7 @@ public function get($key) * @param (callable(value, key): bool) $callback * @return static */ - public function filter(callable $callback): static + public function filter(callable $callback): self { return new static(function () use ($callback) { foreach ($this as $key => $value) { @@ -92,7 +91,7 @@ public function first(callable $callback = null): mixed * @param callable(value, key): mixed $callback * @return static */ - public function map(callable $callback): static + public function map(callable $callback): self { return new static(function () use ($callback) { foreach ($this as $key => $value) { @@ -107,7 +106,7 @@ public function map(callable $callback): static * @param int $limit * @return static */ - public function take(int $limit): static + public function take(int $limit): self { return new static(function () use ($limit) { $iterator = $this->getIterator(); @@ -126,6 +125,12 @@ public function take(int $limit): static }); } + /** + * Determine if all items pass the given truth test. + * + * @param callable $callback + * @return boolean + */ public function every(callable $callback): bool { $iterator = $this->getIterator(); @@ -149,21 +154,33 @@ public function count(): int return iterator_count($this->getIterator()); } - public function getIterator(): Traversable + /** + * Get an iterator for the items. + * + * @return Iterator + */ + public function getIterator(): Iterator { return $this->makeIterator($this->source); } - protected function makeIterator($source): Traversable + /** + * Get an iterator for the given value. + * + * @param callable|IteratorAggregate $source + * @return Iterator + */ + protected function makeIterator($source): Iterator { if ($source instanceof IteratorAggregate) { return $source->getIterator(); } - if (is_array($source)) { - return new ArrayIterator($source); - } - return $source(); } + + public function __call($name, $arguments) + { + var_dump($this->getIterator()); + } } diff --git a/tests/Mollie/API/Resources/CursorCollectionTest.php b/tests/Mollie/API/Resources/CursorCollectionTest.php index 8c54e973..a22672ad 100644 --- a/tests/Mollie/API/Resources/CursorCollectionTest.php +++ b/tests/Mollie/API/Resources/CursorCollectionTest.php @@ -4,7 +4,9 @@ use Mollie\Api\MollieApiClient; use Mollie\Api\Resources\LazyCollection; +use Mollie\Api\Resources\MandateCollection; use Mollie\Api\Resources\OrderCollection; +use Mollie\Api\Types\MandateStatus; use PHPUnit\Framework\TestCase; class CursorCollectionTest extends TestCase @@ -113,12 +115,14 @@ public function testWillReturnNullIfNoPreviousResultIsAvailable() public function testAutoPaginatorReturnsLazyCollection() { - $collection = new OrderCollection( + $collection = new MandateCollection( $this->createMock(MollieApiClient::class), 1, (object) [] ); - $this->assertInstanceOf(LazyCollection::class, $collection->getAutoIterator()); + $this->assertInstanceOf(LazyCollection::class, $collection = $collection->getAutoIterator()); + + $collection->whereStatus(MandateStatus::STATUS_PENDING); } } diff --git a/tests/Mollie/API/Resources/LazyCollectionTest.php b/tests/Mollie/API/Resources/LazyCollectionTest.php index ecf323be..a935da73 100644 --- a/tests/Mollie/API/Resources/LazyCollectionTest.php +++ b/tests/Mollie/API/Resources/LazyCollectionTest.php @@ -3,6 +3,8 @@ namespace Tests\Mollie\API\Resources; use Mollie\Api\Resources\LazyCollection; +use Mollie\Api\Resources\MandateCollection; +use Mollie\Api\Types\MandateStatus; use PHPUnit\Framework\TestCase; class LazyCollectionTest extends TestCase @@ -81,4 +83,18 @@ public function testEvery() return $value > 1; })); } + + public function testItRemainsAccessToOriginalCollectionMethods() + { + $collection = new MandateCollection( + $this->createMock(\Mollie\Api\MollieApiClient::class), + 1, + (object) [] + ); + $collection[0] = (object) ['id' => 'mdt_12345', 'status' => MandateStatus::STATUS_VALID]; + $collection[1] = (object) ['id' => 'mdt_12346', 'status' => MandateStatus::STATUS_PENDING]; + + $pendingMandates = $collection->getAutoIterator()->whereStatus(MandateStatus::STATUS_PENDING); + $this->assertCount(1, $pendingMandates); + } }