From 1ddf8a5b234eeb70f7d1584720ffdee8d12ae66d Mon Sep 17 00:00:00 2001 From: Krishan Koenig Date: Mon, 30 Oct 2023 12:53:02 +0100 Subject: [PATCH] add LazyCollection methods --- src/Endpoints/BalanceEndpoint.php | 6 +- src/Endpoints/BalanceTransactionEndpoint.php | 14 +- src/Endpoints/ChargebackEndpoint.php | 6 +- src/Endpoints/ClientEndpoint.php | 6 +- src/Endpoints/CollectionEndpointAbstract.php | 6 +- src/Endpoints/CustomerEndpoint.php | 6 +- src/Endpoints/CustomerPaymentsEndpoint.php | 10 +- src/Endpoints/InvoiceEndpoint.php | 6 +- src/Endpoints/MandateEndpoint.php | 10 +- src/Endpoints/OrderEndpoint.php | 6 +- src/Endpoints/PaymentCaptureEndpoint.php | 10 +- src/Endpoints/PaymentChargebackEndpoint.php | 10 +- src/Endpoints/PaymentEndpoint.php | 6 +- src/Endpoints/PaymentLinkEndpoint.php | 6 +- src/Endpoints/PaymentRefundEndpoint.php | 10 +- src/Endpoints/ProfileEndpoint.php | 6 +- src/Endpoints/RefundEndpoint.php | 6 +- src/Endpoints/SettlementCaptureEndpoint.php | 6 +- .../SettlementChargebackEndpoint.php | 6 +- src/Endpoints/SettlementPaymentEndpoint.php | 6 +- src/Endpoints/SettlementRefundEndpoint.php | 6 +- src/Endpoints/SettlementsEndpoint.php | 6 +- src/Endpoints/SubscriptionEndpoint.php | 14 +- src/Endpoints/TerminalEndpoint.php | 6 +- src/Resources/CursorCollection.php | 32 ++-- src/Resources/LazyCollection.php | 169 ++++++++++++++++++ .../API/Resources/CursorCollectionTest.php | 12 ++ .../API/Resources/LazyCollectionTest.php | 84 +++++++++ 28 files changed, 372 insertions(+), 105 deletions(-) create mode 100644 src/Resources/LazyCollection.php create mode 100644 tests/Mollie/API/Resources/LazyCollectionTest.php diff --git a/src/Endpoints/BalanceEndpoint.php b/src/Endpoints/BalanceEndpoint.php index 50dcbf42..ec2bdea4 100644 --- a/src/Endpoints/BalanceEndpoint.php +++ b/src/Endpoints/BalanceEndpoint.php @@ -2,7 +2,7 @@ namespace Mollie\Api\Endpoints; -use Generator; +use Mollie\Api\Resources\LazyCollection; use Mollie\Api\Exceptions\ApiException; use Mollie\Api\Resources\Balance; use Mollie\Api\Resources\BalanceCollection; @@ -89,9 +89,9 @@ public function page(?string $from = null, ?int $limit = null, array $parameters * @param array $parameters * @param bool $iterateBackwards Set to true for reverse order iteration (default is false). * - * @return Generator + * @return LazyCollection */ - public function iterator(?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): Generator + public function iterator(?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): LazyCollection { return $this->rest_iterator($from, $limit, $parameters, $iterateBackwards); } diff --git a/src/Endpoints/BalanceTransactionEndpoint.php b/src/Endpoints/BalanceTransactionEndpoint.php index e0238007..4b4c6105 100644 --- a/src/Endpoints/BalanceTransactionEndpoint.php +++ b/src/Endpoints/BalanceTransactionEndpoint.php @@ -4,7 +4,7 @@ namespace Mollie\Api\Endpoints; -use Generator; +use Mollie\Api\Resources\LazyCollection; use Mollie\Api\Resources\Balance; use Mollie\Api\Resources\BalanceTransaction; use Mollie\Api\Resources\BalanceTransactionCollection; @@ -58,9 +58,9 @@ public function listFor(Balance $balance, array $parameters = []) * @param array $parameters * @param bool $iterateBackwards Set to true for reverse order iteration (default is false). * - * @return Generator + * @return LazyCollection */ - public function iteratorFor(Balance $balance, array $parameters = [], bool $iterateBackwards = false): Generator + public function iteratorFor(Balance $balance, array $parameters = [], bool $iterateBackwards = false): LazyCollection { return $this->iteratorForId($balance->id, $parameters, $iterateBackwards); } @@ -88,9 +88,9 @@ public function listForId(string $balanceId, array $parameters = []) * @param array $parameters * @param bool $iterateBackwards Set to true for reverse order iteration (default is false). * - * @return Generator + * @return LazyCollection */ - public function iteratorForId(string $balanceId, array $parameters = [], bool $iterateBackwards = false): Generator + public function iteratorForId(string $balanceId, array $parameters = [], bool $iterateBackwards = false): LazyCollection { $this->parentId = $balanceId; @@ -118,9 +118,9 @@ public function listForPrimary(array $parameters = []) * @param array $parameters * @param bool $iterateBackwards Set to true for reverse order iteration (default is false). * - * @return Generator + * @return LazyCollection */ - public function iteratorForPrimary(array $parameters = [], bool $iterateBackwards = false): Generator + public function iteratorForPrimary(array $parameters = [], bool $iterateBackwards = false): LazyCollection { $this->parentId = "primary"; diff --git a/src/Endpoints/ChargebackEndpoint.php b/src/Endpoints/ChargebackEndpoint.php index d02d08ae..63480ec2 100644 --- a/src/Endpoints/ChargebackEndpoint.php +++ b/src/Endpoints/ChargebackEndpoint.php @@ -2,7 +2,7 @@ namespace Mollie\Api\Endpoints; -use Generator; +use Mollie\Api\Resources\LazyCollection; use Mollie\Api\Exceptions\ApiException; use Mollie\Api\Resources\Chargeback; use Mollie\Api\Resources\ChargebackCollection; @@ -57,9 +57,9 @@ public function page(?string $from = null, ?int $limit = null, array $parameters * @param array $parameters * @param bool $iterateBackwards Set to true for reverse order iteration (default is false). * - * @return Generator + * @return LazyCollection */ - public function iterator(?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): Generator + public function iterator(?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): LazyCollection { return $this->rest_iterator($from, $limit, $parameters, $iterateBackwards); } diff --git a/src/Endpoints/ClientEndpoint.php b/src/Endpoints/ClientEndpoint.php index cdad8b50..ab4cc976 100644 --- a/src/Endpoints/ClientEndpoint.php +++ b/src/Endpoints/ClientEndpoint.php @@ -2,7 +2,7 @@ namespace Mollie\Api\Endpoints; -use Generator; +use Mollie\Api\Resources\LazyCollection; use Mollie\Api\Exceptions\ApiException; use Mollie\Api\Resources\Client; use Mollie\Api\Resources\ClientCollection; @@ -76,9 +76,9 @@ public function page(?string $from = null, ?int $limit = null, array $parameters * @param array $parameters * @param bool $iterateBackwards Set to true for reverse order iteration (default is false). * - * @return Generator + * @return LazyCollection */ - public function iterator(?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): Generator + public function iterator(?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): LazyCollection { return $this->rest_iterator($from, $limit, $parameters, $iterateBackwards); } diff --git a/src/Endpoints/CollectionEndpointAbstract.php b/src/Endpoints/CollectionEndpointAbstract.php index 65987623..6fda8b4a 100644 --- a/src/Endpoints/CollectionEndpointAbstract.php +++ b/src/Endpoints/CollectionEndpointAbstract.php @@ -2,7 +2,7 @@ namespace Mollie\Api\Endpoints; -use Generator; +use Mollie\Api\Resources\LazyCollection; use Mollie\Api\Exceptions\ApiException; use Mollie\Api\Resources\BaseCollection; use Mollie\Api\Resources\CursorCollection; @@ -49,9 +49,9 @@ protected function rest_list(?string $from = null, ?int $limit = null, array $fi * @param int $limit * @param array $filters * @param bool $iterateBackwards Set to true for reverse order iteration (default is false). - * @return Generator + * @return LazyCollection */ - protected function rest_iterator(?string $from = null, ?int $limit = null, array $filters = [], bool $iterateBackwards = false): Generator + protected function rest_iterator(?string $from = null, ?int $limit = null, array $filters = [], bool $iterateBackwards = false): LazyCollection { /** @var CursorCollection $page */ $page = $this->rest_list($from, $limit, $filters); diff --git a/src/Endpoints/CustomerEndpoint.php b/src/Endpoints/CustomerEndpoint.php index 603ba1eb..4f268451 100644 --- a/src/Endpoints/CustomerEndpoint.php +++ b/src/Endpoints/CustomerEndpoint.php @@ -2,7 +2,7 @@ namespace Mollie\Api\Endpoints; -use Generator; +use Mollie\Api\Resources\LazyCollection; use Mollie\Api\Exceptions\ApiException; use Mollie\Api\Resources\Customer; use Mollie\Api\Resources\CustomerCollection; @@ -128,9 +128,9 @@ public function page(?string $from = null, ?int $limit = null, array $parameters * @param array $parameters * @param bool $iterateBackwards Set to true for reverse order iteration (default is false). * - * @return Generator + * @return LazyCollection */ - public function iterator(?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): Generator + public function iterator(?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): LazyCollection { return $this->rest_iterator($from, $limit, $parameters, $iterateBackwards); } diff --git a/src/Endpoints/CustomerPaymentsEndpoint.php b/src/Endpoints/CustomerPaymentsEndpoint.php index 0738dd65..f9e8ab47 100644 --- a/src/Endpoints/CustomerPaymentsEndpoint.php +++ b/src/Endpoints/CustomerPaymentsEndpoint.php @@ -2,7 +2,7 @@ namespace Mollie\Api\Endpoints; -use Generator; +use Mollie\Api\Resources\LazyCollection; use Mollie\Api\Resources\Customer; use Mollie\Api\Resources\Payment; use Mollie\Api\Resources\PaymentCollection; @@ -89,9 +89,9 @@ public function listFor(Customer $customer, ?string $from = null, ?int $limit = * @param array $parameters * @param bool $iterateBackwards Set to true for reverse order iteration (default is false). * - * @return Generator + * @return LazyCollection */ - public function iteratorFor(Customer $customer, ?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): Generator + public function iteratorFor(Customer $customer, ?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): LazyCollection { return $this->iteratorForId($customer->id, $from, $limit, $parameters, $iterateBackwards); } @@ -121,9 +121,9 @@ public function listForId($customerId, ?string $from = null, ?int $limit = null, * @param array $parameters * @param bool $iterateBackwards Set to true for reverse order iteration (default is false). * - * @return Generator + * @return LazyCollection */ - public function iteratorForId(string $customerId, ?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): Generator + public function iteratorForId(string $customerId, ?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): LazyCollection { $this->parentId = $customerId; diff --git a/src/Endpoints/InvoiceEndpoint.php b/src/Endpoints/InvoiceEndpoint.php index 3f5af151..66208916 100644 --- a/src/Endpoints/InvoiceEndpoint.php +++ b/src/Endpoints/InvoiceEndpoint.php @@ -2,7 +2,7 @@ namespace Mollie\Api\Endpoints; -use Generator; +use Mollie\Api\Resources\LazyCollection; use Mollie\Api\Exceptions\ApiException; use Mollie\Api\Resources\Invoice; use Mollie\Api\Resources\InvoiceCollection; @@ -86,9 +86,9 @@ public function all(array $parameters = []) * @param array $parameters * @param bool $iterateBackwards Set to true for reverse order iteration (default is false). * - * @return Generator + * @return LazyCollection */ - public function iterator(?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): Generator + public function iterator(?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): LazyCollection { return $this->rest_iterator($from, $limit, $parameters, $iterateBackwards); } diff --git a/src/Endpoints/MandateEndpoint.php b/src/Endpoints/MandateEndpoint.php index 75302515..9884cd43 100644 --- a/src/Endpoints/MandateEndpoint.php +++ b/src/Endpoints/MandateEndpoint.php @@ -2,7 +2,7 @@ namespace Mollie\Api\Endpoints; -use Generator; +use Mollie\Api\Resources\LazyCollection; use Mollie\Api\Resources\Customer; use Mollie\Api\Resources\Mandate; use Mollie\Api\Resources\MandateCollection; @@ -113,9 +113,9 @@ public function listFor(Customer $customer, $from = null, $limit = null, array $ * @param array $parameters * @param bool $iterateBackwards Set to true for reverse order iteration (default is false). * - * @return Generator + * @return LazyCollection */ - public function iteratorFor(Customer $customer, ?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): Generator + public function iteratorFor(Customer $customer, ?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): LazyCollection { return $this->iteratorForId($customer->id, $from, $limit, $parameters, $iterateBackwards); } @@ -145,9 +145,9 @@ public function listForId($customerId, $from = null, $limit = null, array $param * @param array $parameters * @param bool $iterateBackwards Set to true for reverse order iteration (default is false). * - * @return Generator + * @return LazyCollection */ - public function iteratorForId(string $customerId, ?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): Generator + public function iteratorForId(string $customerId, ?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): LazyCollection { $this->parentId = $customerId; diff --git a/src/Endpoints/OrderEndpoint.php b/src/Endpoints/OrderEndpoint.php index ee1eaaee..df637b4e 100644 --- a/src/Endpoints/OrderEndpoint.php +++ b/src/Endpoints/OrderEndpoint.php @@ -2,7 +2,7 @@ namespace Mollie\Api\Endpoints; -use Generator; +use Mollie\Api\Resources\LazyCollection; use Mollie\Api\Exceptions\ApiException; use Mollie\Api\Resources\Order; use Mollie\Api\Resources\OrderCollection; @@ -137,9 +137,9 @@ public function page(?string $from = null, ?int $limit = null, array $parameters * @param array $parameters * @param bool $iterateBackwards Set to true for reverse order iteration (default is false). * - * @return Generator + * @return LazyCollection */ - public function iterator(?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): Generator + public function iterator(?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): LazyCollection { return $this->rest_iterator($from, $limit, $parameters, $iterateBackwards); } diff --git a/src/Endpoints/PaymentCaptureEndpoint.php b/src/Endpoints/PaymentCaptureEndpoint.php index 1671f095..8f2aef61 100644 --- a/src/Endpoints/PaymentCaptureEndpoint.php +++ b/src/Endpoints/PaymentCaptureEndpoint.php @@ -2,7 +2,7 @@ namespace Mollie\Api\Endpoints; -use Generator; +use Mollie\Api\Resources\LazyCollection; use Mollie\Api\Resources\Capture; use Mollie\Api\Resources\CaptureCollection; use Mollie\Api\Resources\Payment; @@ -115,9 +115,9 @@ public function listFor(Payment $payment, array $parameters = []) * @param array $parameters * @param bool $iterateBackwards Set to true for reverse order iteration (default is false). * - * @return Generator + * @return LazyCollection */ - public function iteratorFor(Payment $payment, ?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): Generator + public function iteratorFor(Payment $payment, ?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): LazyCollection { return $this->iteratorForId($payment->id, $from, $limit, $parameters, $iterateBackwards); } @@ -145,9 +145,9 @@ public function listForId($paymentId, array $parameters = []) * @param array $parameters * @param bool $iterateBackwards Set to true for reverse order iteration (default is false). * - * @return Generator + * @return LazyCollection */ - public function iteratorForId(string $paymentId, ?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): Generator + public function iteratorForId(string $paymentId, ?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): LazyCollection { $this->parentId = $paymentId; diff --git a/src/Endpoints/PaymentChargebackEndpoint.php b/src/Endpoints/PaymentChargebackEndpoint.php index 42da9b03..347a62c0 100644 --- a/src/Endpoints/PaymentChargebackEndpoint.php +++ b/src/Endpoints/PaymentChargebackEndpoint.php @@ -2,7 +2,7 @@ namespace Mollie\Api\Endpoints; -use Generator; +use Mollie\Api\Resources\LazyCollection; use Mollie\Api\Resources\Chargeback; use Mollie\Api\Resources\ChargebackCollection; use Mollie\Api\Resources\Payment; @@ -83,9 +83,9 @@ public function listFor(Payment $payment, array $parameters = []) * @param array $parameters * @param bool $iterateBackwards Set to true for reverse order iteration (default is false). * - * @return Generator + * @return LazyCollection */ - public function iteratorFor(Payment $payment, ?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): Generator + public function iteratorFor(Payment $payment, ?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): LazyCollection { return $this->iteratorForId($payment->id, $from, $limit, $parameters, $iterateBackwards); } @@ -113,9 +113,9 @@ public function listForId($paymentId, array $parameters = []) * @param array $parameters * @param bool $iterateBackwards Set to true for reverse order iteration (default is false). * - * @return Generator + * @return LazyCollection */ - public function iteratorForId(string $paymentId, ?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): Generator + public function iteratorForId(string $paymentId, ?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): LazyCollection { $this->parentId = $paymentId; diff --git a/src/Endpoints/PaymentEndpoint.php b/src/Endpoints/PaymentEndpoint.php index 10ad9d34..15080e7e 100644 --- a/src/Endpoints/PaymentEndpoint.php +++ b/src/Endpoints/PaymentEndpoint.php @@ -2,7 +2,7 @@ namespace Mollie\Api\Endpoints; -use Generator; +use Mollie\Api\Resources\LazyCollection; use Mollie\Api\Exceptions\ApiException; use Mollie\Api\Resources\Payment; use Mollie\Api\Resources\PaymentCollection; @@ -149,9 +149,9 @@ public function page($from = null, $limit = null, array $parameters = []) * @param array $parameters * @param bool $iterateBackwards Set to true for reverse order iteration (default is false). * - * @return Generator + * @return LazyCollection */ - public function iterator(?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): Generator + public function iterator(?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): LazyCollection { return $this->rest_iterator($from, $limit, $parameters, $iterateBackwards); } diff --git a/src/Endpoints/PaymentLinkEndpoint.php b/src/Endpoints/PaymentLinkEndpoint.php index 22adf5cd..1951040f 100644 --- a/src/Endpoints/PaymentLinkEndpoint.php +++ b/src/Endpoints/PaymentLinkEndpoint.php @@ -2,7 +2,7 @@ namespace Mollie\Api\Endpoints; -use Generator; +use Mollie\Api\Resources\LazyCollection; use Mollie\Api\Exceptions\ApiException; use Mollie\Api\Resources\Payment; use Mollie\Api\Resources\PaymentLink; @@ -94,9 +94,9 @@ public function page($from = null, $limit = null, array $parameters = []) * @param array $parameters * @param bool $iterateBackwards Set to true for reverse order iteration (default is false). * - * @return Generator + * @return LazyCollection */ - public function iterator(?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): Generator + public function iterator(?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): LazyCollection { return $this->rest_iterator($from, $limit, $parameters, $iterateBackwards); } diff --git a/src/Endpoints/PaymentRefundEndpoint.php b/src/Endpoints/PaymentRefundEndpoint.php index 9ea4a168..68524ec8 100644 --- a/src/Endpoints/PaymentRefundEndpoint.php +++ b/src/Endpoints/PaymentRefundEndpoint.php @@ -2,7 +2,7 @@ namespace Mollie\Api\Endpoints; -use Generator; +use Mollie\Api\Resources\LazyCollection; use Mollie\Api\Resources\Payment; use Mollie\Api\Resources\Refund; use Mollie\Api\Resources\RefundCollection; @@ -83,9 +83,9 @@ public function listFor(Payment $payment, array $parameters = []) * @param array $parameters * @param bool $iterateBackwards Set to true for reverse order iteration (default is false). * - * @return Generator + * @return LazyCollection */ - public function iteratorFor(Payment $payment, ?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): Generator + public function iteratorFor(Payment $payment, ?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): LazyCollection { return $this->iteratorForId($payment->id, $from, $limit, $parameters, $iterateBackwards); } @@ -113,9 +113,9 @@ public function listForId($paymentId, array $parameters = []) * @param array $parameters * @param bool $iterateBackwards Set to true for reverse order iteration (default is false). * - * @return Generator + * @return LazyCollection */ - public function iteratorForId(string $paymentId, ?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): Generator + public function iteratorForId(string $paymentId, ?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): LazyCollection { $this->parentId = $paymentId; diff --git a/src/Endpoints/ProfileEndpoint.php b/src/Endpoints/ProfileEndpoint.php index 45d382df..55bcee2f 100644 --- a/src/Endpoints/ProfileEndpoint.php +++ b/src/Endpoints/ProfileEndpoint.php @@ -2,7 +2,7 @@ namespace Mollie\Api\Endpoints; -use Generator; +use Mollie\Api\Resources\LazyCollection; use Mollie\Api\Exceptions\ApiException; use Mollie\Api\Resources\CurrentProfile; use Mollie\Api\Resources\Profile; @@ -150,9 +150,9 @@ public function page($from = null, $limit = null, array $parameters = []) * @param array $parameters * @param bool $iterateBackwards Set to true for reverse order iteration (default is false). * - * @return Generator + * @return LazyCollection */ - public function iterator(?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): Generator + public function iterator(?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): LazyCollection { return $this->rest_iterator($from, $limit, $parameters, $iterateBackwards); } diff --git a/src/Endpoints/RefundEndpoint.php b/src/Endpoints/RefundEndpoint.php index 720d14c5..019ae558 100644 --- a/src/Endpoints/RefundEndpoint.php +++ b/src/Endpoints/RefundEndpoint.php @@ -2,7 +2,7 @@ namespace Mollie\Api\Endpoints; -use Generator; +use Mollie\Api\Resources\LazyCollection; use Mollie\Api\Exceptions\ApiException; use Mollie\Api\Resources\Refund; use Mollie\Api\Resources\RefundCollection; @@ -57,9 +57,9 @@ public function page($from = null, $limit = null, array $parameters = []) * @param array $parameters * @param bool $iterateBackwards Set to true for reverse order iteration (default is false). * - * @return Generator + * @return LazyCollection */ - public function iterator(?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): Generator + public function iterator(?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): LazyCollection { return $this->rest_iterator($from, $limit, $parameters, $iterateBackwards); } diff --git a/src/Endpoints/SettlementCaptureEndpoint.php b/src/Endpoints/SettlementCaptureEndpoint.php index 03786563..7ad672e0 100644 --- a/src/Endpoints/SettlementCaptureEndpoint.php +++ b/src/Endpoints/SettlementCaptureEndpoint.php @@ -4,7 +4,7 @@ namespace Mollie\Api\Endpoints; -use Generator; +use Mollie\Api\Resources\LazyCollection; use Mollie\Api\Resources\Capture; use Mollie\Api\Resources\CaptureCollection; @@ -52,9 +52,9 @@ public function pageForId(string $settlementId, string $from = null, int $limit * @param array $parameters * @param bool $iterateBackwards Set to true for reverse order iteration (default is false). * - * @return Generator + * @return LazyCollection */ - public function iteratorForId(string $settlementId, ?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): Generator + public function iteratorForId(string $settlementId, ?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): LazyCollection { $this->parentId = $settlementId; diff --git a/src/Endpoints/SettlementChargebackEndpoint.php b/src/Endpoints/SettlementChargebackEndpoint.php index 6dc5a735..f8032247 100644 --- a/src/Endpoints/SettlementChargebackEndpoint.php +++ b/src/Endpoints/SettlementChargebackEndpoint.php @@ -4,7 +4,7 @@ namespace Mollie\Api\Endpoints; -use Generator; +use Mollie\Api\Resources\LazyCollection; use Mollie\Api\Resources\Chargeback; use Mollie\Api\Resources\ChargebackCollection; @@ -55,9 +55,9 @@ public function pageForId(string $settlementId, string $from = null, int $limit * @param array $parameters * @param bool $iterateBackwards Set to true for reverse order iteration (default is false). * - * @return Generator + * @return LazyCollection */ - public function iteratorForId(string $settlementId, ?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): Generator + public function iteratorForId(string $settlementId, ?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): LazyCollection { $this->parentId = $settlementId; diff --git a/src/Endpoints/SettlementPaymentEndpoint.php b/src/Endpoints/SettlementPaymentEndpoint.php index 5dc6b4e4..ed9b10b5 100644 --- a/src/Endpoints/SettlementPaymentEndpoint.php +++ b/src/Endpoints/SettlementPaymentEndpoint.php @@ -2,7 +2,7 @@ namespace Mollie\Api\Endpoints; -use Generator; +use Mollie\Api\Resources\LazyCollection; use Mollie\Api\Resources\Payment; use Mollie\Api\Resources\PaymentCollection; @@ -53,9 +53,9 @@ public function pageForId($settlementId, $from = null, $limit = null, array $par * @param array $parameters * @param bool $iterateBackwards Set to true for reverse order iteration (default is false). * - * @return Generator + * @return LazyCollection */ - public function iteratorForId(string $settlementId, ?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): Generator + public function iteratorForId(string $settlementId, ?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): LazyCollection { $this->parentId = $settlementId; diff --git a/src/Endpoints/SettlementRefundEndpoint.php b/src/Endpoints/SettlementRefundEndpoint.php index dc11f782..4f751da7 100644 --- a/src/Endpoints/SettlementRefundEndpoint.php +++ b/src/Endpoints/SettlementRefundEndpoint.php @@ -4,7 +4,7 @@ namespace Mollie\Api\Endpoints; -use Generator; +use Mollie\Api\Resources\LazyCollection; use Mollie\Api\Resources\Refund; use Mollie\Api\Resources\RefundCollection; @@ -55,9 +55,9 @@ public function pageForId(string $settlementId, string $from = null, int $limit * @param array $parameters * @param bool $iterateBackwards Set to true for reverse order iteration (default is false). * - * @return Generator + * @return LazyCollection */ - public function iteratorForId(string $settlementId, ?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): Generator + public function iteratorForId(string $settlementId, ?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): LazyCollection { $this->parentId = $settlementId; diff --git a/src/Endpoints/SettlementsEndpoint.php b/src/Endpoints/SettlementsEndpoint.php index 2de54067..da2269c1 100644 --- a/src/Endpoints/SettlementsEndpoint.php +++ b/src/Endpoints/SettlementsEndpoint.php @@ -2,7 +2,7 @@ namespace Mollie\Api\Endpoints; -use Generator; +use Mollie\Api\Resources\LazyCollection; use Mollie\Api\Exceptions\ApiException; use Mollie\Api\Resources\Settlement; use Mollie\Api\Resources\SettlementCollection; @@ -94,9 +94,9 @@ public function page($from = null, $limit = null, array $parameters = []) * @param array $parameters * @param bool $iterateBackwards Set to true for reverse order iteration (default is false). * - * @return Generator + * @return LazyCollection */ - public function iterator(?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): Generator + public function iterator(?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): LazyCollection { return $this->rest_iterator($from, $limit, $parameters, $iterateBackwards); } diff --git a/src/Endpoints/SubscriptionEndpoint.php b/src/Endpoints/SubscriptionEndpoint.php index 06930027..8e2d8122 100644 --- a/src/Endpoints/SubscriptionEndpoint.php +++ b/src/Endpoints/SubscriptionEndpoint.php @@ -2,9 +2,9 @@ namespace Mollie\Api\Endpoints; -use Generator; use Mollie\Api\Exceptions\ApiException; use Mollie\Api\Resources\Customer; +use Mollie\Api\Resources\LazyCollection; use Mollie\Api\Resources\ResourceFactory; use Mollie\Api\Resources\Subscription; use Mollie\Api\Resources\SubscriptionCollection; @@ -148,9 +148,9 @@ public function listFor(Customer $customer, $from = null, $limit = null, array $ * @param array $parameters * @param bool $iterateBackwards Set to true for reverse order iteration (default is false). * - * @return Generator + * @return LazyCollection */ - public function iteratorFor(Customer $customer, ?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): Generator + public function iteratorFor(Customer $customer, ?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): LazyCollection { return $this->iteratorForId($customer->id, $from, $limit, $parameters, $iterateBackwards); } @@ -180,9 +180,9 @@ public function listForId($customerId, $from = null, $limit = null, array $param * @param array $parameters * @param bool $iterateBackwards Set to true for reverse order iteration (default is false). * - * @return Generator + * @return LazyCollection */ - public function iteratorForId(string $customerId, ?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): Generator + public function iteratorForId(string $customerId, ?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): LazyCollection { $this->parentId = $customerId; @@ -253,9 +253,9 @@ public function page($from = null, $limit = null, array $parameters = []) * @param array $parameters * @param bool $iterateBackwards Set to true for reverse order iteration (default is false). * - * @return Generator + * @return LazyCollection */ - public function iterator(?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): Generator + public function iterator(?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): LazyCollection { $page = $this->page($from, $limit, $parameters); diff --git a/src/Endpoints/TerminalEndpoint.php b/src/Endpoints/TerminalEndpoint.php index dbd9ff12..b30bdb34 100644 --- a/src/Endpoints/TerminalEndpoint.php +++ b/src/Endpoints/TerminalEndpoint.php @@ -2,7 +2,7 @@ namespace Mollie\Api\Endpoints; -use Generator; +use Mollie\Api\Resources\LazyCollection; use Mollie\Api\Exceptions\ApiException; use Mollie\Api\Resources\Terminal; use Mollie\Api\Resources\TerminalCollection; @@ -79,9 +79,9 @@ public function page($from = null, $limit = null, array $parameters = []) * @param array $parameters * @param bool $iterateBackwards Set to true for reverse order iteration (default is false). * - * @return Generator + * @return LazyCollection */ - public function iterator(?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): Generator + public function iterator(?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): LazyCollection { return $this->rest_iterator($from, $limit, $parameters, $iterateBackwards); } diff --git a/src/Resources/CursorCollection.php b/src/Resources/CursorCollection.php index 2a20e54e..a17f6df3 100644 --- a/src/Resources/CursorCollection.php +++ b/src/Resources/CursorCollection.php @@ -37,7 +37,7 @@ abstract protected function createResourceObject(); */ final public function next() { - if (! $this->hasNext()) { + if (!$this->hasNext()) { return null; } @@ -60,7 +60,7 @@ final public function next() */ final public function previous() { - if (! $this->hasPrevious()) { + if (!$this->hasPrevious()) { return null; } @@ -100,24 +100,26 @@ public function hasPrevious() * * @param bool $iterateBackwards * - * @return Generator + * @return LazyCollection */ - public function getAutoIterator(bool $iterateBackwards = false): Generator + public function getAutoIterator(bool $iterateBackwards = false): LazyCollection { $page = $this; - while (true) { - foreach ($page as $item) { - yield $item; - } + return new LazyCollection(function () use ($page, $iterateBackwards): Generator { + while (true) { + foreach ($page as $item) { + yield $item; + } - if (($iterateBackwards && ! $page->hasPrevious()) || ! $page->hasNext()) { - break; - } + if (($iterateBackwards && !$page->hasPrevious()) || !$page->hasNext()) { + break; + } - $page = $iterateBackwards - ? $page->previous() - : $page->next(); - } + $page = $iterateBackwards + ? $page->previous() + : $page->next(); + } + }); } } diff --git a/src/Resources/LazyCollection.php b/src/Resources/LazyCollection.php new file mode 100644 index 00000000..655744ef --- /dev/null +++ b/src/Resources/LazyCollection.php @@ -0,0 +1,169 @@ +source = $source; + } + + /** + * Get all items in the collection. + * + * @return array + */ + public function all(): array + { + return iterator_to_array($this->getIterator()); + } + + /** + * Get an item from the collection by key. + * + * @param int|string $key + * @return mixed|null + */ + public function get($key) + { + foreach ($this as $outerKey => $outerValue) { + if ($outerKey == $key) { + return $outerValue; + } + } + + return null; + } + + /** + * Run a filter over each of the items. + * + * @param (callable(value, key): bool) $callback + * @return static + */ + public function filter(callable $callback): static + { + return new static(function () use ($callback) { + foreach ($this as $key => $value) { + if ($callback($value, $key)) { + yield $key => $value; + } + } + }); + } + + /** + * Get the first item from the collection passing the given truth test. + * + * @param (callable(value): bool)|null $callback + * @return mixed|null + */ + public function first(callable $callback = null): mixed + { + $iterator = $this->getIterator(); + + if (is_null($callback)) { + if (!$iterator->valid()) { + return null; + } + + return $iterator->current(); + } + + foreach ($iterator as $key => $value) { + if ($callback($value, $key)) { + return $value; + } + } + + return null; + } + + /** + * Run a map over each of the items. + * + * @param callable(value, key): mixed $callback + * @return static + */ + public function map(callable $callback): static + { + return new static(function () use ($callback) { + foreach ($this as $key => $value) { + yield $key => $callback($value, $key); + } + }); + } + + /** + * Take the first or last {$limit} items. + * + * @param int $limit + * @return static + */ + public function take(int $limit): static + { + return new static(function () use ($limit) { + $iterator = $this->getIterator(); + + while ($limit--) { + if (!$iterator->valid()) { + break; + } + + yield $iterator->key() => $iterator->current(); + + if ($limit) { + $iterator->next(); + } + } + }); + } + + public function every(callable $callback): bool + { + $iterator = $this->getIterator(); + + foreach ($iterator as $key => $value) { + if (!$callback($value, $key)) { + return false; + } + } + + return true; + } + + /** + * Count the number of items in the collection. + * + * @return int + */ + public function count(): int + { + return iterator_count($this->getIterator()); + } + + public function getIterator(): Traversable + { + return $this->makeIterator($this->source); + } + + protected function makeIterator($source): Traversable + { + if ($source instanceof IteratorAggregate) { + return $source->getIterator(); + } + + if (is_array($source)) { + return new ArrayIterator($source); + } + + return $source(); + } +} diff --git a/tests/Mollie/API/Resources/CursorCollectionTest.php b/tests/Mollie/API/Resources/CursorCollectionTest.php index 2eec4c3c..8c54e973 100644 --- a/tests/Mollie/API/Resources/CursorCollectionTest.php +++ b/tests/Mollie/API/Resources/CursorCollectionTest.php @@ -3,6 +3,7 @@ namespace Tests\Mollie\API\Resources; use Mollie\Api\MollieApiClient; +use Mollie\Api\Resources\LazyCollection; use Mollie\Api\Resources\OrderCollection; use PHPUnit\Framework\TestCase; @@ -109,4 +110,15 @@ public function testWillReturnNullIfNoPreviousResultIsAvailable() $this->assertFalse($collection->hasPrevious()); $this->assertNull($collection->previous()); } + + public function testAutoPaginatorReturnsLazyCollection() + { + $collection = new OrderCollection( + $this->createMock(MollieApiClient::class), + 1, + (object) [] + ); + + $this->assertInstanceOf(LazyCollection::class, $collection->getAutoIterator()); + } } diff --git a/tests/Mollie/API/Resources/LazyCollectionTest.php b/tests/Mollie/API/Resources/LazyCollectionTest.php new file mode 100644 index 00000000..ecf323be --- /dev/null +++ b/tests/Mollie/API/Resources/LazyCollectionTest.php @@ -0,0 +1,84 @@ +collection = new LazyCollection(function () { + yield 1; + yield 2; + yield 3; + }); + } + + public function testCanCreateACollectionFromGeneratorFunction() + { + $this->assertEquals(3, $this->collection->count()); + $this->assertEquals(1, $this->collection->get(0)); + $this->assertEquals(2, $this->collection->get(1)); + $this->assertEquals(3, $this->collection->get(2)); + } + + public function testFilter() + { + $filtered = $this->collection->filter(function ($value) { + return $value > 1; + }); + + $this->assertEquals(2, $filtered->count()); + } + + public function testAll() + { + $this->assertEquals([1, 2, 3], $this->collection->all()); + } + + public function testFirst() + { + $this->assertEquals(1, $this->collection->first()); + $this->assertEquals(3, $this->collection->first(function ($value) { + return $value === 3; + })); + } + + public function testMap() + { + $mapped = $this->collection->map(function ($value) { + return $value * 2; + }); + + + $mapped->every(function ($value, $key) { + $this->assertEquals($value, $this->collection->get($key) * 2); + + return true; + }); + } + + public function testTake() + { + $taken = $this->collection->take(2); + + $this->assertEquals(2, $taken->count()); + } + + public function testEvery() + { + $this->assertTrue($this->collection->every(function ($value) { + return $value > 0; + })); + + $this->assertFalse($this->collection->every(function ($value) { + return $value > 1; + })); + } +}