diff --git a/src/Endpoints/BalanceEndpoint.php b/src/Endpoints/BalanceEndpoint.php index 50dcbf42..5f44e41a 100644 --- a/src/Endpoints/BalanceEndpoint.php +++ b/src/Endpoints/BalanceEndpoint.php @@ -2,11 +2,11 @@ namespace Mollie\Api\Endpoints; -use Generator; use Mollie\Api\Exceptions\ApiException; use Mollie\Api\Resources\Balance; use Mollie\Api\Resources\BalanceCollection; use Mollie\Api\Resources\BaseCollection; +use Mollie\Api\Resources\LazyCollection; class BalanceEndpoint extends CollectionEndpointAbstract { @@ -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..4f244713 100644 --- a/src/Endpoints/BalanceTransactionEndpoint.php +++ b/src/Endpoints/BalanceTransactionEndpoint.php @@ -4,10 +4,10 @@ namespace Mollie\Api\Endpoints; -use Generator; use Mollie\Api\Resources\Balance; use Mollie\Api\Resources\BalanceTransaction; use Mollie\Api\Resources\BalanceTransactionCollection; +use Mollie\Api\Resources\LazyCollection; class BalanceTransactionEndpoint extends CollectionEndpointAbstract { @@ -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..c40c179c 100644 --- a/src/Endpoints/ChargebackEndpoint.php +++ b/src/Endpoints/ChargebackEndpoint.php @@ -2,10 +2,10 @@ namespace Mollie\Api\Endpoints; -use Generator; use Mollie\Api\Exceptions\ApiException; use Mollie\Api\Resources\Chargeback; use Mollie\Api\Resources\ChargebackCollection; +use Mollie\Api\Resources\LazyCollection; class ChargebackEndpoint extends CollectionEndpointAbstract { @@ -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..9d7ba66b 100644 --- a/src/Endpoints/ClientEndpoint.php +++ b/src/Endpoints/ClientEndpoint.php @@ -2,10 +2,10 @@ namespace Mollie\Api\Endpoints; -use Generator; use Mollie\Api\Exceptions\ApiException; use Mollie\Api\Resources\Client; use Mollie\Api\Resources\ClientCollection; +use Mollie\Api\Resources\LazyCollection; class ClientEndpoint extends CollectionEndpointAbstract { @@ -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..06b1dec2 100644 --- a/src/Endpoints/CollectionEndpointAbstract.php +++ b/src/Endpoints/CollectionEndpointAbstract.php @@ -2,10 +2,10 @@ namespace Mollie\Api\Endpoints; -use Generator; use Mollie\Api\Exceptions\ApiException; use Mollie\Api\Resources\BaseCollection; use Mollie\Api\Resources\CursorCollection; +use Mollie\Api\Resources\LazyCollection; use Mollie\Api\Resources\ResourceFactory; abstract class CollectionEndpointAbstract extends EndpointAbstract @@ -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..61c8e689 100644 --- a/src/Endpoints/CustomerEndpoint.php +++ b/src/Endpoints/CustomerEndpoint.php @@ -2,10 +2,10 @@ namespace Mollie\Api\Endpoints; -use Generator; use Mollie\Api\Exceptions\ApiException; use Mollie\Api\Resources\Customer; use Mollie\Api\Resources\CustomerCollection; +use Mollie\Api\Resources\LazyCollection; class CustomerEndpoint extends CollectionEndpointAbstract { @@ -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..1730cb24 100644 --- a/src/Endpoints/CustomerPaymentsEndpoint.php +++ b/src/Endpoints/CustomerPaymentsEndpoint.php @@ -2,8 +2,8 @@ namespace Mollie\Api\Endpoints; -use Generator; use Mollie\Api\Resources\Customer; +use Mollie\Api\Resources\LazyCollection; 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..e577abd9 100644 --- a/src/Endpoints/InvoiceEndpoint.php +++ b/src/Endpoints/InvoiceEndpoint.php @@ -2,10 +2,10 @@ namespace Mollie\Api\Endpoints; -use Generator; use Mollie\Api\Exceptions\ApiException; use Mollie\Api\Resources\Invoice; use Mollie\Api\Resources\InvoiceCollection; +use Mollie\Api\Resources\LazyCollection; class InvoiceEndpoint extends CollectionEndpointAbstract { @@ -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..d6dee89f 100644 --- a/src/Endpoints/MandateEndpoint.php +++ b/src/Endpoints/MandateEndpoint.php @@ -2,8 +2,8 @@ namespace Mollie\Api\Endpoints; -use Generator; use Mollie\Api\Resources\Customer; +use Mollie\Api\Resources\LazyCollection; 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..91f7bc34 100644 --- a/src/Endpoints/OrderEndpoint.php +++ b/src/Endpoints/OrderEndpoint.php @@ -2,8 +2,8 @@ namespace Mollie\Api\Endpoints; -use Generator; use Mollie\Api\Exceptions\ApiException; +use Mollie\Api\Resources\LazyCollection; 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..61b2a6d2 100644 --- a/src/Endpoints/PaymentCaptureEndpoint.php +++ b/src/Endpoints/PaymentCaptureEndpoint.php @@ -2,9 +2,9 @@ namespace Mollie\Api\Endpoints; -use Generator; use Mollie\Api\Resources\Capture; use Mollie\Api\Resources\CaptureCollection; +use Mollie\Api\Resources\LazyCollection; use Mollie\Api\Resources\Payment; class PaymentCaptureEndpoint extends CollectionEndpointAbstract @@ -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..5a232f88 100644 --- a/src/Endpoints/PaymentChargebackEndpoint.php +++ b/src/Endpoints/PaymentChargebackEndpoint.php @@ -2,9 +2,9 @@ namespace Mollie\Api\Endpoints; -use Generator; use Mollie\Api\Resources\Chargeback; use Mollie\Api\Resources\ChargebackCollection; +use Mollie\Api\Resources\LazyCollection; use Mollie\Api\Resources\Payment; class PaymentChargebackEndpoint extends CollectionEndpointAbstract @@ -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..79b1c6b6 100644 --- a/src/Endpoints/PaymentEndpoint.php +++ b/src/Endpoints/PaymentEndpoint.php @@ -2,8 +2,8 @@ namespace Mollie\Api\Endpoints; -use Generator; use Mollie\Api\Exceptions\ApiException; +use Mollie\Api\Resources\LazyCollection; use Mollie\Api\Resources\Payment; use Mollie\Api\Resources\PaymentCollection; use Mollie\Api\Resources\Refund; @@ -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..f27d09dd 100644 --- a/src/Endpoints/PaymentLinkEndpoint.php +++ b/src/Endpoints/PaymentLinkEndpoint.php @@ -2,8 +2,8 @@ namespace Mollie\Api\Endpoints; -use Generator; use Mollie\Api\Exceptions\ApiException; +use Mollie\Api\Resources\LazyCollection; use Mollie\Api\Resources\Payment; use Mollie\Api\Resources\PaymentLink; use Mollie\Api\Resources\PaymentLinkCollection; @@ -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..d430275c 100644 --- a/src/Endpoints/ProfileEndpoint.php +++ b/src/Endpoints/ProfileEndpoint.php @@ -2,9 +2,9 @@ namespace Mollie\Api\Endpoints; -use Generator; use Mollie\Api\Exceptions\ApiException; use Mollie\Api\Resources\CurrentProfile; +use Mollie\Api\Resources\LazyCollection; use Mollie\Api\Resources\Profile; use Mollie\Api\Resources\ProfileCollection; @@ -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..de03c477 100644 --- a/src/Endpoints/RefundEndpoint.php +++ b/src/Endpoints/RefundEndpoint.php @@ -2,8 +2,8 @@ namespace Mollie\Api\Endpoints; -use Generator; use Mollie\Api\Exceptions\ApiException; +use Mollie\Api\Resources\LazyCollection; 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..3e51443e 100644 --- a/src/Endpoints/SettlementCaptureEndpoint.php +++ b/src/Endpoints/SettlementCaptureEndpoint.php @@ -4,9 +4,9 @@ namespace Mollie\Api\Endpoints; -use Generator; use Mollie\Api\Resources\Capture; use Mollie\Api\Resources\CaptureCollection; +use Mollie\Api\Resources\LazyCollection; class SettlementCaptureEndpoint extends CollectionEndpointAbstract { @@ -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..585ccf2c 100644 --- a/src/Endpoints/SettlementChargebackEndpoint.php +++ b/src/Endpoints/SettlementChargebackEndpoint.php @@ -4,9 +4,9 @@ namespace Mollie\Api\Endpoints; -use Generator; use Mollie\Api\Resources\Chargeback; use Mollie\Api\Resources\ChargebackCollection; +use Mollie\Api\Resources\LazyCollection; class SettlementChargebackEndpoint extends CollectionEndpointAbstract { @@ -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..76c7cad8 100644 --- a/src/Endpoints/SettlementsEndpoint.php +++ b/src/Endpoints/SettlementsEndpoint.php @@ -2,8 +2,8 @@ namespace Mollie\Api\Endpoints; -use Generator; use Mollie\Api\Exceptions\ApiException; +use Mollie\Api\Resources\LazyCollection; 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..4f7a65c6 100644 --- a/src/Endpoints/TerminalEndpoint.php +++ b/src/Endpoints/TerminalEndpoint.php @@ -2,8 +2,8 @@ namespace Mollie\Api\Endpoints; -use Generator; use Mollie\Api\Exceptions\ApiException; +use Mollie\Api\Resources\LazyCollection; 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..0f83c7e5 100644 --- a/src/Resources/CursorCollection.php +++ b/src/Resources/CursorCollection.php @@ -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..0fc90f08 --- /dev/null +++ b/src/Resources/LazyCollection.php @@ -0,0 +1,198 @@ + + */ +class LazyCollection implements IteratorAggregate +{ + /** + * @var callable + */ + private $source; + + /** + * @param callable $source + */ + public function __construct($source) + { + $this->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 TKey $key + * @return TValue|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(TValue, TKey): bool) $callback + * @return self + */ + public function filter(callable $callback): self + { + return new self(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(TValue, TKey): bool)|null $callback + * @return TValue|null + */ + public function first(callable $callback = null) + { + $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. + * + * @template TMapValue + * + * @param callable(TValue, TKey): TMapValue $callback + * @return static + */ + public function map(callable $callback): self + { + return new self(function () use ($callback) { + foreach ($this as $key => $value) { + yield $key => $callback($value, $key); + } + }); + } + + /** + * Take the first {$limit} items. + * + * @param int $limit + * @return static + */ + public function take(int $limit): self + { + return new self(function () use ($limit) { + $iterator = $this->getIterator(); + + while ($limit--) { + if (! $iterator->valid()) { + break; + } + + yield $iterator->key() => $iterator->current(); + + if ($limit) { + $iterator->next(); + } + } + }); + } + + /** + * Determine if all items pass the given truth test. + * + * @param (callable(TValue, TKey): bool) $callback + * @return bool + */ + 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()); + } + + /** + * Get an iterator for the items. + * + * @return Iterator + */ + public function getIterator(): Iterator + { + return $this->makeIterator($this->source); + } + + /** + * Get an iterator for the given value. + * + * @template TIteratorKey of array-key + * @template TIteratorValue + * + * @param IteratorAggregate|(callable(): \Generator) $source + * @return Iterator + */ + protected function makeIterator($source): Iterator + { + if ($source instanceof IteratorAggregate) { + return $source->getIterator(); + } + + return $source(); + } +} diff --git a/tests/Mollie/API/Resources/CursorCollectionTest.php b/tests/Mollie/API/Resources/CursorCollectionTest.php new file mode 100644 index 00000000..d521d4dc --- /dev/null +++ b/tests/Mollie/API/Resources/CursorCollectionTest.php @@ -0,0 +1,219 @@ +createMock(MollieApiClient::class); + $mockedClient->expects($this->once()) + ->method('performHttpCallToFullUrl') + ->willReturn($this->arrayToObject([ + 'count' => 1, + '_links' => [ + 'self' => [ + 'href' => 'https://api.mollie.com/v2/orders?from=ord_stTC2WHAuS', + ], + ], + '_embedded' => [ + 'orders' => [ + ['id' => 'ord_stTC2WHAuS'], + ], + ], + ])); + + $collection = new OrderCollection( + $mockedClient, + 1, + $this->arrayToObject([ + 'next' => [ + 'href' => 'https://api.mollie.com/v2/orders?from=ord_stTC2WHAuS', + ], + ]) + ); + + $this->assertTrue($collection->hasNext()); + + $nextPage = $collection->next(); + + $this->assertEquals('ord_stTC2WHAuS', $nextPage[0]->id); + + $this->assertFalse($nextPage->hasNext()); + } + + public function testWillReturnNullIfNoNextResultIsAvailable() + { + $mockedClient = $this->createMock(MollieApiClient::class); + $collection = new OrderCollection( + $mockedClient, + 1, + (object) [] + ); + + $this->assertFalse($collection->hasNext()); + $this->assertNull($collection->next()); + } + + public function testCanGetPreviousCollectionResultWhenPreviousLinkIsAvailable() + { + $mockedClient = $this->createMock(MollieApiClient::class); + $mockedClient->expects($this->once()) + ->method('performHttpCallToFullUrl') + ->willReturn( + $this->arrayToObject([ + 'count' => 1, + '_links' => [ + 'self' => [ + 'href' => 'https://api.mollie.com/v2/orders?from=ord_stTC2WHAuS', + ], + ], + '_embedded' => [ + 'orders' => [ + ['id' => 'ord_stTC2WHAuS'], + ], + ], + ]) + ); + + $collection = new OrderCollection( + $mockedClient, + 1, + $this->arrayToObject([ + 'previous' => [ + 'href' => 'https://api.mollie.com/v2/orders?from=ord_stTC2WHAuS', + ], + ]) + ); + + $this->assertTrue($collection->hasPrevious()); + + $previousPage = $collection->previous(); + + $this->assertEquals('ord_stTC2WHAuS', $previousPage[0]->id); + + $this->assertFalse($previousPage->hasPrevious()); + } + + public function testWillReturnNullIfNoPreviousResultIsAvailable() + { + $mockedClient = $this->createMock(MollieApiClient::class); + $collection = new OrderCollection( + $mockedClient, + 1, + (object) [] + ); + + $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()); + } + + public function testAutoPaginatorCanHandleConsecutiveCalls() + { + $mockedClient = $this->createMock(MollieApiClient::class); + $mockedClient->expects($this->exactly(3)) + ->method('performHttpCallToFullUrl') + ->willReturnOnConsecutiveCalls( + $this->arrayToObject([ + 'count' => 1, + '_links' => [ + 'self' => [ + 'href' => 'https://api.mollie.com/v2/orders?from=ord_stTC2WHAuS', + ], + 'next' => [ + 'href' => 'https://api.mollie.com/v2/orders?from=ord_stTC2WHAuS', + ], + ], + '_embedded' => [ + 'orders' => [ + ['id' => 'ord_stTC2WHAuS'], + ], + ], + ]), + $this->arrayToObject([ + 'count' => 1, + '_links' => [ + 'self' => [ + 'href' => 'https://api.mollie.com/v2/orders?from=ord_stTC2WHAuF', + ], + 'next' => [ + 'href' => 'https://api.mollie.com/v2/orders?from=ord_stTC2WHAuF', + ], + ], + '_embedded' => [ + 'orders' => [ + ['id' => 'ord_stTC2WHAuF'], + ], + ], + ]), + $this->arrayToObject([ + 'count' => 1, + '_links' => [ + 'self' => [ + 'href' => 'https://api.mollie.com/v2/orders?from=ord_stTC2WHAuB', + ], + ], + '_embedded' => [ + 'orders' => [ + ['id' => 'ord_stTC2WHAuB'], + ], + ], + ]) + ); + + $collection = new OrderCollection( + $mockedClient, + 0, + $this->arrayToObject([ + 'next' => [ + 'href' => 'https://api.mollie.com/v2/orders?from=ord_stTC2WHAuS', + ], + ]) + ); + + $orderIds = []; + foreach ($collection->getAutoIterator() as $order) { + $orderIds[] = $order->id; + } + + $this->assertEquals(['ord_stTC2WHAuS', 'ord_stTC2WHAuF', 'ord_stTC2WHAuB'], $orderIds); + } + + /** + * Convert an array to an object recursively. + * + * @param mixed $data + * @return mixed + */ + private function arrayToObject($data) + { + if (! is_array($data)) { + return $data; + } + + $obj = new stdClass(); + + foreach ($data as $key => $value) { + $obj->$key = $this->arrayToObject($value); + } + + return $obj; + } +} diff --git a/tests/Mollie/API/Resources/LazyCollectionTest.php b/tests/Mollie/API/Resources/LazyCollectionTest.php new file mode 100644 index 00000000..726c76bc --- /dev/null +++ b/tests/Mollie/API/Resources/LazyCollectionTest.php @@ -0,0 +1,100 @@ +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; + })); + } + + public function testChainedUsage() + { + $result = $this->collection + ->filter(function ($value) { + return $value > 1; + })->map(function ($value) { + return $value * 2; + })->take(1); + + $this->assertEquals(1, $result->count()); + $this->assertEquals(4, $result->first()); + } +}