Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Naoray committed Oct 30, 2023
1 parent 951d688 commit f23e7cb
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 37 deletions.
9 changes: 3 additions & 6 deletions src/Resources/CursorCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ abstract protected function createResourceObject();
*/
final public function next()
{
if (! $this->hasNext()) {
if (!$this->hasNext()) {
return null;
}

Expand All @@ -60,7 +60,7 @@ final public function next()
*/
final public function previous()
{
if (! $this->hasPrevious()) {
if (!$this->hasPrevious()) {
return null;
}

Expand Down Expand Up @@ -106,16 +106,13 @@ 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) {
yield $item;
}

if (($iterateBackwards && ! $page->hasPrevious()) || ! $page->hasNext()) {
if (($iterateBackwards && !$page->hasPrevious()) || !$page->hasNext()) {
break;
}

Expand Down
14 changes: 10 additions & 4 deletions src/Resources/LazyCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@

class LazyCollection implements IteratorAggregate
{
/**
* @var callable
*/
private $source;

public function __construct(callable $source)
/**
* @param callable $source
*/
public function __construct($source)
{
$this->source = $source;
}
Expand Down Expand Up @@ -69,7 +75,7 @@ public function first(callable $callback = null): mixed
$iterator = $this->getIterator();

if (is_null($callback)) {
if (! $iterator->valid()) {
if (!$iterator->valid()) {
return null;
}

Expand Down Expand Up @@ -112,7 +118,7 @@ public function take(int $limit): self
$iterator = $this->getIterator();

while ($limit--) {
if (! $iterator->valid()) {
if (!$iterator->valid()) {
break;
}

Expand All @@ -136,7 +142,7 @@ public function every(callable $callback): bool
$iterator = $this->getIterator();

foreach ($iterator as $key => $value) {
if (! $callback($value, $key)) {
if (!$callback($value, $key)) {
return false;
}
}
Expand Down
16 changes: 6 additions & 10 deletions src/Resources/MandateCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

class MandateCollection extends CursorCollection
{
public function getLazyCollectionName(): string
{
return LazyMandateCollection::class;
}

/**
* @return string
*/
Expand All @@ -26,15 +31,6 @@ protected function createResourceObject()
*/
public function whereStatus($status)
{
$collection = new self($this->client, 0, $this->_links);

foreach ($this as $item) {
if ($item->status === $status) {
$collection[] = $item;
$collection->count++;
}
}

return $collection;
return $this->filter(fn (Mandate $mandate) => $mandate->status === $status);
}
}
21 changes: 4 additions & 17 deletions tests/Mollie/API/Resources/LazyCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
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
{
private LazyCollection $collection;
/**
* @var LazyCollection
*/
private $collection;

public function setUp(): void
{
Expand Down Expand Up @@ -83,18 +84,4 @@ 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);
}
}

0 comments on commit f23e7cb

Please sign in to comment.