From b4d4f0b143f97cfe7badaa92b5e97c5f400d6459 Mon Sep 17 00:00:00 2001 From: Krishan Koenig Date: Wed, 25 Oct 2023 12:30:44 +0200 Subject: [PATCH 01/26] wip --- src/Endpoints/CollectionEndpointAbstract.php | 31 ++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/Endpoints/CollectionEndpointAbstract.php b/src/Endpoints/CollectionEndpointAbstract.php index b11ee985..6c8e77ec 100644 --- a/src/Endpoints/CollectionEndpointAbstract.php +++ b/src/Endpoints/CollectionEndpointAbstract.php @@ -2,8 +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\ResourceFactory; abstract class CollectionEndpointAbstract extends EndpointAbstract @@ -36,6 +38,35 @@ protected function rest_list($from = null, $limit = null, array $filters = []) return $collection; } + /** + * Iterate over all objects from the REST API. + * + * @param string $from The first resource ID you want to include in your list. + * @param int $limit + * @param array $filters + * @param boolean $iterateBackwards Whether to iterate backwards or forward. + * @return Generator + */ + protected function rest_iterator($from = null, $limit = null, array $filters = [], bool $iterateBackwards = false): Generator + { + /** @var CursorCollection $page */ + $page = $this->rest_list($from, $limit, $filters); + + while (true) { + foreach ($page as $item) { + yield $item; + } + + if (($iterateBackwards && !$page->hasPrevious()) || !$page->hasNext()) { + break; + } + + $page = $iterateBackwards + ? $page->previous() + : $page->next(); + } + } + /** * Get the collection object that is used by this API endpoint. Every API endpoint uses one type of collection object. * From 645a31ef6d30d5edf45e337ab33ce75a766417fd Mon Sep 17 00:00:00 2001 From: Krishan Koenig Date: Thu, 26 Oct 2023 11:01:57 +0200 Subject: [PATCH 02/26] move test to right location --- .../SettlementRefundEndpointTest.php | 92 +++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 tests/Mollie/API/Endpoints/SettlementRefundEndpointTest.php diff --git a/tests/Mollie/API/Endpoints/SettlementRefundEndpointTest.php b/tests/Mollie/API/Endpoints/SettlementRefundEndpointTest.php new file mode 100644 index 00000000..261432c5 --- /dev/null +++ b/tests/Mollie/API/Endpoints/SettlementRefundEndpointTest.php @@ -0,0 +1,92 @@ +mockApiCall( + new Request( + 'GET', + '/v2/settlements/stl_jDk30akdN/refunds?limit=5&foo=bar' + ), + new Response( + 200, + [], + '{ + "_embedded": { + "refunds": [ + { + "resource": "refund", + "id": "re_3aKhkUNigy", + "amount": { + "value": "10.00", + "currency": "EUR" + }, + "status": "refunded", + "createdAt": "2018-08-30T07:59:02+00:00", + "description": "Order #33", + "paymentId": "tr_maJaG2j8OM", + "settlementAmount": { + "value": "-10.00", + "currency": "EUR" + }, + "settlementId": "stl_jDk30akdN", + "_links": { + "self": { + "href": "https://api.mollie.com/v2/payments/tr_maJaG2j8OM/refunds/re_3aKhkUNigy", + "type": "application/hal+json" + }, + "payment": { + "href": "https://api.mollie.com/v2/payments/tr_maJaG2j8OM", + "type": "application/hal+json" + }, + "settlement": { + "href": "https://api.mollie.com/v2/settlements/stl_jDk30akdN", + "type": "application/hal+json" + } + } + }, + { } + ] + }, + "count": 1, + "_links": { + "documentation": { + "href": "https://docs.mollie.com/reference/v2/settlements-api/list-settlement-refunds", + "type": "text/html" + }, + "self": { + "href": "https://api.mollie.com/v2/settlements/stl_jDk30akdN/refunds?limit=50", + "type": "application/hal+json" + }, + "previous": null, + "next": null + } + }' + ) + ); + + $settlement = new Settlement($this->apiClient); + $settlement->id = 'stl_jDk30akdN'; + + $refunds = $settlement->refunds(5, ['foo' => 'bar']); + + $this->assertInstanceOf(RefundCollection::class, $refunds); + $this->assertCount(2, $refunds); + + $refund = $refunds[0]; + $this->assertInstanceOf(Refund::class, $refund); + $this->assertEquals("re_3aKhkUNigy", $refund->id); + } +} From f558f5f0f616ac586e8c70b45eb14dcfa62ad5a1 Mon Sep 17 00:00:00 2001 From: Krishan Koenig Date: Thu, 26 Oct 2023 11:57:44 +0200 Subject: [PATCH 03/26] add iterator to all cursor collection compatible endpoints --- src/Endpoints/BalanceEndpoint.php | 18 +- src/Endpoints/BalanceTransactionEndpoint.php | 46 ++++ src/Endpoints/ChargebackEndpoint.php | 18 +- src/Endpoints/ClientEndpoint.php | 18 +- src/Endpoints/CollectionEndpointAbstract.php | 25 ++- src/Endpoints/CustomerEndpoint.php | 20 +- src/Endpoints/CustomerPaymentsEndpoint.php | 39 +++- src/Endpoints/InvoiceEndpoint.php | 16 ++ src/Endpoints/MandateEndpoint.php | 35 +++ src/Endpoints/OrderEndpoint.php | 22 +- src/Endpoints/PaymentCaptureEndpoint.php | 35 +++ src/Endpoints/PaymentChargebackEndpoint.php | 35 +++ src/Endpoints/PaymentEndpoint.php | 20 +- src/Endpoints/PaymentLinkEndpoint.php | 18 +- src/Endpoints/PaymentRefundEndpoint.php | 35 +++ src/Endpoints/ProfileEndpoint.php | 18 +- src/Endpoints/RefundEndpoint.php | 16 ++ src/Endpoints/SettlementCaptureEndpoint.php | 19 ++ .../SettlementChargebackEndpoint.php | 19 ++ src/Endpoints/SettlementPaymentEndpoint.php | 19 ++ src/Endpoints/SettlementRefundEndpoint.php | 19 ++ src/Endpoints/SettlementsEndpoint.php | 16 ++ src/Endpoints/ShipmentEndpoint.php | 2 +- src/Endpoints/SubscriptionEndpoint.php | 54 ++++- src/Endpoints/TerminalEndpoint.php | 16 ++ .../API/Endpoints/BalanceEndpointTest.php | 113 ++++++++++ .../BalanceTransactionEndpointTest.php | 158 ++++++++++++++ .../API/Endpoints/ChargebackEndpointTest.php | 98 +++++++++ .../API/Endpoints/CustomerEndpointTest.php | 47 ++++ .../Endpoints/CustomerPaymentEndpointTest.php | 14 +- .../API/Endpoints/InvoiceEndpointTest.php | 98 +++++++++ .../API/Endpoints/OrderEndpointTest.php | 115 ++++++---- .../API/Endpoints/PaymentEndpointTest.php | 167 ++++++++++++-- .../API/Endpoints/RefundEndpointTest.php | 66 ++++++ .../API/Endpoints/SettlementEndpointTest.php | 205 ++++++++++++++++++ .../API/Endpoints/TerminalEndpointTest.php | 109 ++++++++++ tests/SettlementRefundEndpointTest.php | 91 -------- 37 files changed, 1705 insertions(+), 174 deletions(-) delete mode 100644 tests/SettlementRefundEndpointTest.php diff --git a/src/Endpoints/BalanceEndpoint.php b/src/Endpoints/BalanceEndpoint.php index b1a9d69a..a0582c8b 100644 --- a/src/Endpoints/BalanceEndpoint.php +++ b/src/Endpoints/BalanceEndpoint.php @@ -2,6 +2,7 @@ namespace Mollie\Api\Endpoints; +use Generator; use Mollie\Api\Exceptions\ApiException; use Mollie\Api\Resources\Balance; use Mollie\Api\Resources\BalanceCollection; @@ -45,7 +46,7 @@ protected function getResourceObject() public function get(string $balanceId, array $parameters = []) { if (empty($balanceId) || strpos($balanceId, self::RESOURCE_ID_PREFIX) !== 0) { - throw new ApiException("Invalid balance ID: '{$balanceId}'. A balance ID should start with '".self::RESOURCE_ID_PREFIX."'."); + throw new ApiException("Invalid balance ID: '{$balanceId}'. A balance ID should start with '" . self::RESOURCE_ID_PREFIX . "'."); } return parent::rest_read($balanceId, $parameters); @@ -79,4 +80,19 @@ public function page(?string $from = null, ?int $limit = null, array $parameters { return $this->rest_list($from, $limit, $parameters); } + + /** + * Create an iterator for iterating over balances retrieved from Mollie. + * + * @param string $from The first Balance ID you want to include in your list. + * @param int $limit + * @param array $parameters + * @param boolean $iterateBackwards Set to true for reverse order iteration (default is false). + * + * @return Generator + */ + public function iterator(?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): Generator + { + return $this->rest_iterator($from, $limit, $parameters, $iterateBackwards); + } } diff --git a/src/Endpoints/BalanceTransactionEndpoint.php b/src/Endpoints/BalanceTransactionEndpoint.php index ed0cbb09..5dcec331 100644 --- a/src/Endpoints/BalanceTransactionEndpoint.php +++ b/src/Endpoints/BalanceTransactionEndpoint.php @@ -4,6 +4,7 @@ namespace Mollie\Api\Endpoints; +use Generator; use Mollie\Api\Resources\Balance; use Mollie\Api\Resources\BalanceTransaction; use Mollie\Api\Resources\BalanceTransactionCollection; @@ -50,6 +51,20 @@ public function listFor(Balance $balance, array $parameters = []) return $this->listForId($balance->id, $parameters); } + /** + * Create an iterator for iterating over balance transactions for the given balance retrieved from Mollie. + * + * @param Balance $balance + * @param array $parameters + * @param boolean $iterateBackwards Set to true for reverse order iteration (default is false). + * + * @return Generator + */ + public function iteratorFor(Balance $balance, array $parameters = [], bool $iterateBackwards = false): Generator + { + return $this->iteratorForId($balance->id, $parameters, $iterateBackwards); + } + /** * List the transactions for a specific Balance ID. * @@ -66,6 +81,22 @@ public function listForId(string $balanceId, array $parameters = []) return parent::rest_list(null, null, $parameters); } + /** + * Create an iterator for iterating over balance transactions for the given balance id retrieved from Mollie. + * + * @param string $balanceId + * @param array $parameters + * @param boolean $iterateBackwards Set to true for reverse order iteration (default is false). + * + * @return Generator + */ + public function iteratorForId(string $balanceId, array $parameters = [], bool $iterateBackwards = false): Generator + { + $this->parentId = $balanceId; + + return $this->rest_iterator(null, null, $parameters, $iterateBackwards); + } + /** * List the transactions for the primary Balance. * @@ -80,4 +111,19 @@ public function listForPrimary(array $parameters = []) return parent::rest_list(null, null, $parameters); } + + /** + * Create an iterator for iterating over transactions for the primary balance retrieved from Mollie. + * + * @param array $parameters + * @param boolean $iterateBackwards Set to true for reverse order iteration (default is false). + * + * @return Generator + */ + public function iteratorForPrimary(array $parameters = [], bool $iterateBackwards = false): Generator + { + $this->parentId = "primary"; + + return $this->rest_iterator(null, null, $parameters, $iterateBackwards); + } } diff --git a/src/Endpoints/ChargebackEndpoint.php b/src/Endpoints/ChargebackEndpoint.php index dd5045ab..68efd372 100644 --- a/src/Endpoints/ChargebackEndpoint.php +++ b/src/Endpoints/ChargebackEndpoint.php @@ -2,6 +2,7 @@ namespace Mollie\Api\Endpoints; +use Generator; use Mollie\Api\Exceptions\ApiException; use Mollie\Api\Resources\Chargeback; use Mollie\Api\Resources\ChargebackCollection; @@ -43,8 +44,23 @@ protected function getResourceCollectionObject($count, $_links) * @return ChargebackCollection * @throws ApiException */ - public function page($from = null, $limit = null, array $parameters = []) + public function page(?string $from = null, ?int $limit = null, array $parameters = []) { return $this->rest_list($from, $limit, $parameters); } + + /** + * Create an iterator for iterating over chargeback retrieved from Mollie. + * + * @param string $from The first chargevback ID you want to include in your list. + * @param int $limit + * @param array $parameters + * @param boolean $iterateBackwards Set to true for reverse order iteration (default is false). + * + * @return Generator + */ + public function iterator(?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): Generator + { + return $this->rest_iterator($from, $limit, $parameters, $iterateBackwards); + } } diff --git a/src/Endpoints/ClientEndpoint.php b/src/Endpoints/ClientEndpoint.php index 86891785..5e3efb94 100644 --- a/src/Endpoints/ClientEndpoint.php +++ b/src/Endpoints/ClientEndpoint.php @@ -2,6 +2,7 @@ namespace Mollie\Api\Endpoints; +use Generator; use Mollie\Api\Exceptions\ApiException; use Mollie\Api\Resources\Client; use Mollie\Api\Resources\ClientCollection; @@ -62,8 +63,23 @@ public function get($clientId, array $parameters = []) * @return ClientCollection * @throws ApiException */ - public function page($from = null, $limit = null, array $parameters = []) + public function page(?string $from = null, ?int $limit = null, array $parameters = []) { return $this->rest_list($from, $limit, $parameters); } + + /** + * Create an iterator for iterating over clients retrieved from Mollie. + * + * @param string $from The first client ID you want to include in your list. + * @param int $limit + * @param array $parameters + * @param boolean $iterateBackwards Set to true for reverse order iteration (default is false). + * + * @return Generator + */ + public function iterator(?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): Generator + { + return $this->rest_iterator($from, $limit, $parameters, $iterateBackwards); + } } diff --git a/src/Endpoints/CollectionEndpointAbstract.php b/src/Endpoints/CollectionEndpointAbstract.php index 6c8e77ec..c36f54bc 100644 --- a/src/Endpoints/CollectionEndpointAbstract.php +++ b/src/Endpoints/CollectionEndpointAbstract.php @@ -20,7 +20,7 @@ abstract class CollectionEndpointAbstract extends EndpointAbstract * @return mixed * @throws ApiException */ - protected function rest_list($from = null, $limit = null, array $filters = []) + protected function rest_list(?string $from = null, ?int $limit = null, array $filters = []) { $filters = array_merge(["from" => $from, "limit" => $limit], $filters); @@ -39,19 +39,36 @@ protected function rest_list($from = null, $limit = null, array $filters = []) } /** - * Iterate over all objects from the REST API. + * Create a generator for iterating over a resource's collection using REST API calls. + * + * This function fetches paginated data from a RESTful resource endpoint and returns a generator + * that allows you to iterate through the items in the collection one by one. It supports forward + * and backward iteration, pagination, and filtering. * * @param string $from The first resource ID you want to include in your list. * @param int $limit * @param array $filters - * @param boolean $iterateBackwards Whether to iterate backwards or forward. + * @param boolean $iterateBackwards Set to true for reverse order iteration (default is false). * @return Generator */ - protected function rest_iterator($from = null, $limit = null, array $filters = [], bool $iterateBackwards = false): Generator + protected function rest_iterator(?string $from = null, ?int $limit = null, array $filters = [], bool $iterateBackwards = false): Generator { /** @var CursorCollection $page */ $page = $this->rest_list($from, $limit, $filters); + return $this->iterate($page, $iterateBackwards); + } + + /** + * Iterate over a CursorCollection and yield its elements. + * + * @param CursorCollection $page + * @param boolean $iterateBackwards + * + * @return Generator + */ + protected function iterate(CursorCollection $page, bool $iterateBackwards = false): Generator + { while (true) { foreach ($page as $item) { yield $item; diff --git a/src/Endpoints/CustomerEndpoint.php b/src/Endpoints/CustomerEndpoint.php index ae04d8f4..5e48a1b4 100644 --- a/src/Endpoints/CustomerEndpoint.php +++ b/src/Endpoints/CustomerEndpoint.php @@ -2,6 +2,7 @@ namespace Mollie\Api\Endpoints; +use Generator; use Mollie\Api\Exceptions\ApiException; use Mollie\Api\Resources\Customer; use Mollie\Api\Resources\CustomerCollection; @@ -81,7 +82,7 @@ public function get($customerId, array $parameters = []) public function update($customerId, array $data = []) { if (empty($customerId) || strpos($customerId, self::RESOURCE_ID_PREFIX) !== 0) { - throw new ApiException("Invalid order ID: '{$customerId}'. An order ID should start with '".self::RESOURCE_ID_PREFIX."'."); + throw new ApiException("Invalid order ID: '{$customerId}'. An order ID should start with '" . self::RESOURCE_ID_PREFIX . "'."); } return parent::rest_update($customerId, $data); @@ -114,8 +115,23 @@ public function delete($customerId, array $data = []) * @return CustomerCollection * @throws ApiException */ - public function page($from = null, $limit = null, array $parameters = []) + public function page(?string $from = null, ?int $limit = null, array $parameters = []) { return $this->rest_list($from, $limit, $parameters); } + + /** + * Create an iterator for iterating over customers retrieved from Mollie. + * + * @param string $from The first customer ID you want to include in your list. + * @param int $limit + * @param array $parameters + * @param boolean $iterateBackwards Set to true for reverse order iteration (default is false). + * + * @return Generator + */ + public function iterator(?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): Generator + { + return $this->rest_iterator($from, $limit, $parameters, $iterateBackwards); + } } diff --git a/src/Endpoints/CustomerPaymentsEndpoint.php b/src/Endpoints/CustomerPaymentsEndpoint.php index 3961ea6f..ea231b88 100644 --- a/src/Endpoints/CustomerPaymentsEndpoint.php +++ b/src/Endpoints/CustomerPaymentsEndpoint.php @@ -2,6 +2,7 @@ namespace Mollie\Api\Endpoints; +use Generator; use Mollie\Api\Resources\Customer; use Mollie\Api\Resources\Payment; use Mollie\Api\Resources\PaymentCollection; @@ -74,11 +75,27 @@ public function createForId($customerId, array $options = [], array $filters = [ * @return PaymentCollection * @throws \Mollie\Api\Exceptions\ApiException */ - public function listFor(Customer $customer, $from = null, $limit = null, array $parameters = []) + public function listFor(Customer $customer, ?string $from = null, ?int $limit = null, array $parameters = []) { return $this->listForId($customer->id, $from, $limit, $parameters); } + /** + * Create an iterator for iterating over payments for the given customer, retrieved from Mollie. + * + * @param Customer $customer + * @param string $from The first resource ID you want to include in your list. + * @param int $limit + * @param array $parameters + * @param boolean $iterateBackwards Set to true for reverse order iteration (default is false). + * + * @return Generator + */ + public function iteratorFor(Customer $customer, ?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): Generator + { + return $this->iteratorForId($customer->id, $from, $limit, $parameters, $iterateBackwards); + } + /** * @param string $customerId * @param string $from The first resource ID you want to include in your list. @@ -88,10 +105,28 @@ public function listFor(Customer $customer, $from = null, $limit = null, array $ * @return \Mollie\Api\Resources\PaymentCollection * @throws \Mollie\Api\Exceptions\ApiException */ - public function listForId($customerId, $from = null, $limit = null, array $parameters = []) + public function listForId($customerId, ?string $from = null, ?int $limit = null, array $parameters = []) { $this->parentId = $customerId; return parent::rest_list($from, $limit, $parameters); } + + /** + * Create an iterator for iterating over payments for the given customer id, retrieved from Mollie. + * + * @param string $customerId + * @param string $from The first resource ID you want to include in your list. + * @param int $limit + * @param array $parameters + * @param boolean $iterateBackwards Set to true for reverse order iteration (default is false). + * + * @return Generator + */ + public function iteratorForId(string $customerId, ?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): Generator + { + $this->parentId = $customerId; + + return $this->rest_iterator($from, $limit, $parameters, $iterateBackwards); + } } diff --git a/src/Endpoints/InvoiceEndpoint.php b/src/Endpoints/InvoiceEndpoint.php index a228fd28..afd7750d 100644 --- a/src/Endpoints/InvoiceEndpoint.php +++ b/src/Endpoints/InvoiceEndpoint.php @@ -2,6 +2,7 @@ namespace Mollie\Api\Endpoints; +use Generator; use Mollie\Api\Exceptions\ApiException; use Mollie\Api\Resources\Invoice; use Mollie\Api\Resources\InvoiceCollection; @@ -76,4 +77,19 @@ public function all(array $parameters = []) { return $this->page(null, null, $parameters); } + + /** + * Create an iterator for iterating over invoices retrieved from Mollie. + * + * @param string $from The first resource ID you want to include in your list. + * @param int $limit + * @param array $parameters + * @param boolean $iterateBackwards Set to true for reverse order iteration (default is false). + * + * @return Generator + */ + public function iterator(?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): Generator + { + return $this->rest_iterator($from, $limit, $parameters, $iterateBackwards); + } } diff --git a/src/Endpoints/MandateEndpoint.php b/src/Endpoints/MandateEndpoint.php index 046eaf2e..160f4b74 100644 --- a/src/Endpoints/MandateEndpoint.php +++ b/src/Endpoints/MandateEndpoint.php @@ -2,6 +2,7 @@ namespace Mollie\Api\Endpoints; +use Generator; use Mollie\Api\Resources\Customer; use Mollie\Api\Resources\Mandate; use Mollie\Api\Resources\MandateCollection; @@ -103,6 +104,22 @@ public function listFor(Customer $customer, $from = null, $limit = null, array $ return $this->listForId($customer->id, $from, $limit, $parameters); } + /** + * Create an iterator for iterating over mandates for the given customer, retrieved from Mollie. + * + * @param Customer $customer + * @param string $from The first resource ID you want to include in your list. + * @param int $limit + * @param array $parameters + * @param boolean $iterateBackwards Set to true for reverse order iteration (default is false). + * + * @return Generator + */ + public function iteratorFor(Customer $customer, ?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): Generator + { + return $this->iteratorForId($customer->id, $from, $limit, $parameters, $iterateBackwards); + } + /** * @param string $customerId * @param null $from @@ -119,6 +136,24 @@ public function listForId($customerId, $from = null, $limit = null, array $param return parent::rest_list($from, $limit, $parameters); } + /** + * Create an iterator for iterating over mandates for the given customer id, retrieved from Mollie. + * + * @param string $customerId + * @param string $from The first resource ID you want to include in your list. + * @param int $limit + * @param array $parameters + * @param boolean $iterateBackwards Set to true for reverse order iteration (default is false). + * + * @return Generator + */ + public function iteratorForId(string $customerId, ?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): Generator + { + $this->parentId = $customerId; + + return $this->rest_iterator($from, $limit, $parameters, $iterateBackwards); + } + /** * @param Customer $customer * @param string $mandateId diff --git a/src/Endpoints/OrderEndpoint.php b/src/Endpoints/OrderEndpoint.php index 605544a3..102b44ea 100644 --- a/src/Endpoints/OrderEndpoint.php +++ b/src/Endpoints/OrderEndpoint.php @@ -2,6 +2,7 @@ namespace Mollie\Api\Endpoints; +use Generator; use Mollie\Api\Exceptions\ApiException; use Mollie\Api\Resources\Order; use Mollie\Api\Resources\OrderCollection; @@ -68,7 +69,7 @@ public function create(array $data = [], array $filters = []) public function update($orderId, array $data = []) { if (empty($orderId) || strpos($orderId, self::RESOURCE_ID_PREFIX) !== 0) { - throw new ApiException("Invalid order ID: '{$orderId}'. An order ID should start with '".self::RESOURCE_ID_PREFIX."'."); + throw new ApiException("Invalid order ID: '{$orderId}'. An order ID should start with '" . self::RESOURCE_ID_PREFIX . "'."); } return parent::rest_update($orderId, $data); @@ -87,7 +88,7 @@ public function update($orderId, array $data = []) public function get($orderId, array $parameters = []) { if (empty($orderId) || strpos($orderId, self::RESOURCE_ID_PREFIX) !== 0) { - throw new ApiException("Invalid order ID: '{$orderId}'. An order ID should start with '".self::RESOURCE_ID_PREFIX."'."); + throw new ApiException("Invalid order ID: '{$orderId}'. An order ID should start with '" . self::RESOURCE_ID_PREFIX . "'."); } return parent::rest_read($orderId, $parameters); @@ -123,8 +124,23 @@ public function cancel($orderId, $parameters = []) * @return OrderCollection * @throws ApiException */ - public function page($from = null, $limit = null, array $parameters = []) + public function page(?string $from = null, ?int $limit = null, array $parameters = []) { return $this->rest_list($from, $limit, $parameters); } + + /** + * Create an iterator for iterating over orders retrieved from Mollie. + * + * @param string $from The first order ID you want to include in your list. + * @param int $limit + * @param array $parameters + * @param boolean $iterateBackwards Set to true for reverse order iteration (default is false). + * + * @return Generator + */ + public function iterator(?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): Generator + { + return $this->rest_iterator($from, $limit, $parameters, $iterateBackwards); + } } diff --git a/src/Endpoints/PaymentCaptureEndpoint.php b/src/Endpoints/PaymentCaptureEndpoint.php index 6766235a..d7bd6f4e 100644 --- a/src/Endpoints/PaymentCaptureEndpoint.php +++ b/src/Endpoints/PaymentCaptureEndpoint.php @@ -2,6 +2,7 @@ namespace Mollie\Api\Endpoints; +use Generator; use Mollie\Api\Resources\Capture; use Mollie\Api\Resources\CaptureCollection; use Mollie\Api\Resources\Payment; @@ -105,6 +106,22 @@ public function listFor(Payment $payment, array $parameters = []) return $this->listForId($payment->id, $parameters); } + /** + * Create an iterator for iterating over captures for the given payment, retrieved from Mollie. + * + * @param Payment $payment + * @param string $from The first resource ID you want to include in your list. + * @param int $limit + * @param array $parameters + * @param boolean $iterateBackwards Set to true for reverse order iteration (default is false). + * + * @return Generator + */ + public function iteratorFor(Payment $payment, ?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): Generator + { + return $this->iteratorForId($payment->id, $from, $limit, $parameters, $iterateBackwards); + } + /** * @param string $paymentId * @param array $parameters @@ -118,4 +135,22 @@ public function listForId($paymentId, array $parameters = []) return parent::rest_list(null, null, $parameters); } + + /** + * Create an iterator for iterating over captures for the given payment id, retrieved from Mollie. + * + * @param string $paymentId + * @param string $from The first resource ID you want to include in your list. + * @param int $limit + * @param array $parameters + * @param boolean $iterateBackwards Set to true for reverse order iteration (default is false). + * + * @return Generator + */ + public function iteratorForId(string $paymentId, ?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): Generator + { + $this->parentId = $paymentId; + + return $this->rest_iterator($from, $limit, $parameters, $iterateBackwards); + } } diff --git a/src/Endpoints/PaymentChargebackEndpoint.php b/src/Endpoints/PaymentChargebackEndpoint.php index e27ddeb5..e2f1d33a 100644 --- a/src/Endpoints/PaymentChargebackEndpoint.php +++ b/src/Endpoints/PaymentChargebackEndpoint.php @@ -2,6 +2,7 @@ namespace Mollie\Api\Endpoints; +use Generator; use Mollie\Api\Resources\Chargeback; use Mollie\Api\Resources\ChargebackCollection; use Mollie\Api\Resources\Payment; @@ -73,6 +74,22 @@ public function listFor(Payment $payment, array $parameters = []) return $this->listForId($payment->id, $parameters); } + /** + * Create an iterator for iterating over chargebacks for the given payment, retrieved from Mollie. + * + * @param Payment $payment + * @param string $from The first resource ID you want to include in your list. + * @param int $limit + * @param array $parameters + * @param boolean $iterateBackwards Set to true for reverse order iteration (default is false). + * + * @return Generator + */ + public function iteratorFor(Payment $payment, ?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): Generator + { + return $this->iteratorForId($payment->id, $from, $limit, $parameters, $iterateBackwards); + } + /** * @param string $paymentId * @param array $parameters @@ -86,4 +103,22 @@ public function listForId($paymentId, array $parameters = []) return parent::rest_list(null, null, $parameters); } + + /** + * Create an iterator for iterating over chargebacks for the given payment id, retrieved from Mollie. + * + * @param string $paymentId + * @param string $from The first resource ID you want to include in your list. + * @param int $limit + * @param array $parameters + * @param boolean $iterateBackwards Set to true for reverse order iteration (default is false). + * + * @return Generator + */ + public function iteratorForId(string $paymentId, ?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): Generator + { + $this->parentId = $paymentId; + + return $this->rest_iterator($from, $limit, $parameters, $iterateBackwards); + } } diff --git a/src/Endpoints/PaymentEndpoint.php b/src/Endpoints/PaymentEndpoint.php index a840863e..a68032cc 100644 --- a/src/Endpoints/PaymentEndpoint.php +++ b/src/Endpoints/PaymentEndpoint.php @@ -2,6 +2,7 @@ namespace Mollie\Api\Endpoints; +use Generator; use Mollie\Api\Exceptions\ApiException; use Mollie\Api\Resources\Payment; use Mollie\Api\Resources\PaymentCollection; @@ -66,7 +67,7 @@ public function create(array $data = [], array $filters = []) public function update($paymentId, array $data = []) { if (empty($paymentId) || strpos($paymentId, self::RESOURCE_ID_PREFIX) !== 0) { - throw new ApiException("Invalid payment ID: '{$paymentId}'. A payment ID should start with '".self::RESOURCE_ID_PREFIX."'."); + throw new ApiException("Invalid payment ID: '{$paymentId}'. A payment ID should start with '" . self::RESOURCE_ID_PREFIX . "'."); } return parent::rest_update($paymentId, $data); @@ -85,7 +86,7 @@ public function update($paymentId, array $data = []) public function get($paymentId, array $parameters = []) { if (empty($paymentId) || strpos($paymentId, self::RESOURCE_ID_PREFIX) !== 0) { - throw new ApiException("Invalid payment ID: '{$paymentId}'. A payment ID should start with '".self::RESOURCE_ID_PREFIX."'."); + throw new ApiException("Invalid payment ID: '{$paymentId}'. A payment ID should start with '" . self::RESOURCE_ID_PREFIX . "'."); } return parent::rest_read($paymentId, $parameters); @@ -140,6 +141,21 @@ public function page($from = null, $limit = null, array $parameters = []) return $this->rest_list($from, $limit, $parameters); } + /** + * Create an iterator for iterating over payments retrieved from Mollie. + * + * @param string $from The first resource ID you want to include in your list. + * @param int $limit + * @param array $parameters + * @param boolean $iterateBackwards Set to true for reverse order iteration (default is false). + * + * @return Generator + */ + public function iterator(?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): Generator + { + return $this->rest_iterator($from, $limit, $parameters, $iterateBackwards); + } + /** * Issue a refund for the given payment. * diff --git a/src/Endpoints/PaymentLinkEndpoint.php b/src/Endpoints/PaymentLinkEndpoint.php index 8f3f3bef..6e98c053 100644 --- a/src/Endpoints/PaymentLinkEndpoint.php +++ b/src/Endpoints/PaymentLinkEndpoint.php @@ -2,6 +2,7 @@ namespace Mollie\Api\Endpoints; +use Generator; use Mollie\Api\Exceptions\ApiException; use Mollie\Api\Resources\Payment; use Mollie\Api\Resources\PaymentLink; @@ -64,7 +65,7 @@ public function create(array $data = [], array $filters = []) public function get($paymentLinkId, array $parameters = []) { if (empty($paymentLinkId) || strpos($paymentLinkId, self::RESOURCE_ID_PREFIX) !== 0) { - throw new ApiException("Invalid payment link ID: '{$paymentLinkId}'. A payment link ID should start with '".self::RESOURCE_ID_PREFIX."'."); + throw new ApiException("Invalid payment link ID: '{$paymentLinkId}'. A payment link ID should start with '" . self::RESOURCE_ID_PREFIX . "'."); } return parent::rest_read($paymentLinkId, $parameters); @@ -84,4 +85,19 @@ public function page($from = null, $limit = null, array $parameters = []) { return $this->rest_list($from, $limit, $parameters); } + + /** + * Create an iterator for iterating over payment links retrieved from Mollie. + * + * @param string $from The first resource ID you want to include in your list. + * @param int $limit + * @param array $parameters + * @param boolean $iterateBackwards Set to true for reverse order iteration (default is false). + * + * @return Generator + */ + public function iterator(?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): Generator + { + return $this->rest_iterator($from, $limit, $parameters, $iterateBackwards); + } } diff --git a/src/Endpoints/PaymentRefundEndpoint.php b/src/Endpoints/PaymentRefundEndpoint.php index 3dfa2469..28c2ee36 100644 --- a/src/Endpoints/PaymentRefundEndpoint.php +++ b/src/Endpoints/PaymentRefundEndpoint.php @@ -2,6 +2,7 @@ namespace Mollie\Api\Endpoints; +use Generator; use Mollie\Api\Resources\Payment; use Mollie\Api\Resources\Refund; use Mollie\Api\Resources\RefundCollection; @@ -73,6 +74,22 @@ public function listFor(Payment $payment, array $parameters = []) return $this->listForId($payment->id, $parameters); } + /** + * Create an iterator for iterating over refunds for the given payment, retrieved from Mollie. + * + * @param Payment $payment + * @param string $from The first resource ID you want to include in your list. + * @param int $limit + * @param array $parameters + * @param boolean $iterateBackwards Set to true for reverse order iteration (default is false). + * + * @return Generator + */ + public function iteratorFor(Payment $payment, ?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): Generator + { + return $this->iteratorForId($payment->id, $from, $limit, $parameters, $iterateBackwards); + } + /** * @param string $paymentId * @param array $parameters @@ -87,6 +104,24 @@ public function listForId($paymentId, array $parameters = []) return parent::rest_list(null, null, $parameters); } + /** + * Create an iterator for iterating over refunds for the given payment id, retrieved from Mollie. + * + * @param string $paymentId + * @param string $from The first resource ID you want to include in your list. + * @param int $limit + * @param array $parameters + * @param boolean $iterateBackwards Set to true for reverse order iteration (default is false). + * + * @return Generator + */ + public function iteratorForId(string $paymentId, ?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): Generator + { + $this->parentId = $paymentId; + + return $this->rest_iterator($from, $limit, $parameters, $iterateBackwards); + } + /** * Creates a refund for a specific payment. diff --git a/src/Endpoints/ProfileEndpoint.php b/src/Endpoints/ProfileEndpoint.php index 498d442a..07647743 100644 --- a/src/Endpoints/ProfileEndpoint.php +++ b/src/Endpoints/ProfileEndpoint.php @@ -2,6 +2,7 @@ namespace Mollie\Api\Endpoints; +use Generator; use Mollie\Api\Exceptions\ApiException; use Mollie\Api\Resources\CurrentProfile; use Mollie\Api\Resources\Profile; @@ -88,7 +89,7 @@ public function get($profileId, array $parameters = []) public function update($profileId, array $data = []) { if (empty($profileId) || strpos($profileId, self::RESOURCE_ID_PREFIX) !== 0) { - throw new ApiException("Invalid profile id: '{$profileId}'. An profile id should start with '".self::RESOURCE_ID_PREFIX."'."); + throw new ApiException("Invalid profile id: '{$profileId}'. An profile id should start with '" . self::RESOURCE_ID_PREFIX . "'."); } return parent::rest_update($profileId, $data); @@ -140,4 +141,19 @@ public function page($from = null, $limit = null, array $parameters = []) { return $this->rest_list($from, $limit, $parameters); } + + /** + * Create an iterator for iterating over profiles retrieved from Mollie. + * + * @param string $from The first resource ID you want to include in your list. + * @param int $limit + * @param array $parameters + * @param boolean $iterateBackwards Set to true for reverse order iteration (default is false). + * + * @return Generator + */ + public function iterator(?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): Generator + { + return $this->rest_iterator($from, $limit, $parameters, $iterateBackwards); + } } diff --git a/src/Endpoints/RefundEndpoint.php b/src/Endpoints/RefundEndpoint.php index 385e21c0..616f15af 100644 --- a/src/Endpoints/RefundEndpoint.php +++ b/src/Endpoints/RefundEndpoint.php @@ -2,6 +2,7 @@ namespace Mollie\Api\Endpoints; +use Generator; use Mollie\Api\Exceptions\ApiException; use Mollie\Api\Resources\Refund; use Mollie\Api\Resources\RefundCollection; @@ -47,4 +48,19 @@ public function page($from = null, $limit = null, array $parameters = []) { return $this->rest_list($from, $limit, $parameters); } + + /** + * Create an iterator for iterating over refunds retrieved from Mollie. + * + * @param string $from The first resource ID you want to include in your list. + * @param int $limit + * @param array $parameters + * @param boolean $iterateBackwards Set to true for reverse order iteration (default is false). + * + * @return Generator + */ + public function iterator(?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): Generator + { + return $this->rest_iterator($from, $limit, $parameters, $iterateBackwards); + } } diff --git a/src/Endpoints/SettlementCaptureEndpoint.php b/src/Endpoints/SettlementCaptureEndpoint.php index 0c46d330..4c08136a 100644 --- a/src/Endpoints/SettlementCaptureEndpoint.php +++ b/src/Endpoints/SettlementCaptureEndpoint.php @@ -4,6 +4,7 @@ namespace Mollie\Api\Endpoints; +use Generator; use Mollie\Api\Resources\Capture; use Mollie\Api\Resources\CaptureCollection; @@ -41,4 +42,22 @@ public function pageForId(string $settlementId, string $from = null, int $limit return $this->rest_list($from, $limit, $parameters); } + + /** + * Create an iterator for iterating over captures for the given settlement id, retrieved from Mollie. + * + * @param string $settlementId + * @param string $from The first resource ID you want to include in your list. + * @param int $limit + * @param array $parameters + * @param boolean $iterateBackwards Set to true for reverse order iteration (default is false). + * + * @return Generator + */ + public function iteratorForId(string $settlementId, ?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): Generator + { + $this->parentId = $settlementId; + + return $this->rest_iterator($from, $limit, $parameters, $iterateBackwards); + } } diff --git a/src/Endpoints/SettlementChargebackEndpoint.php b/src/Endpoints/SettlementChargebackEndpoint.php index 850d0256..2992c322 100644 --- a/src/Endpoints/SettlementChargebackEndpoint.php +++ b/src/Endpoints/SettlementChargebackEndpoint.php @@ -4,6 +4,7 @@ namespace Mollie\Api\Endpoints; +use Generator; use Mollie\Api\Resources\Chargeback; use Mollie\Api\Resources\ChargebackCollection; @@ -44,4 +45,22 @@ public function pageForId(string $settlementId, string $from = null, int $limit return $this->rest_list($from, $limit, $parameters); } + + /** + * Create an iterator for iterating over chargeback for the given settlement id, retrieved from Mollie. + * + * @param string $settlementId + * @param string $from The first resource ID you want to include in your list. + * @param int $limit + * @param array $parameters + * @param boolean $iterateBackwards Set to true for reverse order iteration (default is false). + * + * @return Generator + */ + public function iteratorForId(string $settlementId, ?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): Generator + { + $this->parentId = $settlementId; + + return $this->rest_iterator($from, $limit, $parameters, $iterateBackwards); + } } diff --git a/src/Endpoints/SettlementPaymentEndpoint.php b/src/Endpoints/SettlementPaymentEndpoint.php index 6ac8304e..9766bd1d 100644 --- a/src/Endpoints/SettlementPaymentEndpoint.php +++ b/src/Endpoints/SettlementPaymentEndpoint.php @@ -2,6 +2,7 @@ namespace Mollie\Api\Endpoints; +use Generator; use Mollie\Api\Resources\Payment; use Mollie\Api\Resources\PaymentCollection; @@ -42,4 +43,22 @@ public function pageForId($settlementId, $from = null, $limit = null, array $par return $this->rest_list($from, $limit, $parameters); } + + /** + * Create an iterator for iterating over payments for the given settlement id, retrieved from Mollie. + * + * @param string $settlementId + * @param string $from The first resource ID you want to include in your list. + * @param int $limit + * @param array $parameters + * @param boolean $iterateBackwards Set to true for reverse order iteration (default is false). + * + * @return Generator + */ + public function iteratorForId(string $settlementId, ?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): Generator + { + $this->parentId = $settlementId; + + return $this->rest_iterator($from, $limit, $parameters, $iterateBackwards); + } } diff --git a/src/Endpoints/SettlementRefundEndpoint.php b/src/Endpoints/SettlementRefundEndpoint.php index 31e9c1d1..4984fe23 100644 --- a/src/Endpoints/SettlementRefundEndpoint.php +++ b/src/Endpoints/SettlementRefundEndpoint.php @@ -4,6 +4,7 @@ namespace Mollie\Api\Endpoints; +use Generator; use Mollie\Api\Resources\Refund; use Mollie\Api\Resources\RefundCollection; @@ -44,4 +45,22 @@ public function pageForId(string $settlementId, string $from = null, int $limit return $this->rest_list($from, $limit, $parameters); } + + /** + * Create an iterator for iterating over refunds for the given settlement id, retrieved from Mollie. + * + * @param string $settlementId + * @param string $from The first resource ID you want to include in your list. + * @param int $limit + * @param array $parameters + * @param boolean $iterateBackwards Set to true for reverse order iteration (default is false). + * + * @return Generator + */ + public function iteratorForId(string $settlementId, ?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): Generator + { + $this->parentId = $settlementId; + + return $this->rest_iterator($from, $limit, $parameters, $iterateBackwards); + } } diff --git a/src/Endpoints/SettlementsEndpoint.php b/src/Endpoints/SettlementsEndpoint.php index a4d49e85..6fc4c570 100644 --- a/src/Endpoints/SettlementsEndpoint.php +++ b/src/Endpoints/SettlementsEndpoint.php @@ -2,6 +2,7 @@ namespace Mollie\Api\Endpoints; +use Generator; use Mollie\Api\Exceptions\ApiException; use Mollie\Api\Resources\Settlement; use Mollie\Api\Resources\SettlementCollection; @@ -84,4 +85,19 @@ public function page($from = null, $limit = null, array $parameters = []) { return $this->rest_list($from, $limit, $parameters); } + + /** + * Create an iterator for iterating over settlements retrieved from Mollie. + * + * @param string $from The first resource ID you want to include in your list. + * @param int $limit + * @param array $parameters + * @param boolean $iterateBackwards Set to true for reverse order iteration (default is false). + * + * @return Generator + */ + public function iterator(?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): Generator + { + return $this->rest_iterator($from, $limit, $parameters, $iterateBackwards); + } } diff --git a/src/Endpoints/ShipmentEndpoint.php b/src/Endpoints/ShipmentEndpoint.php index 6456ab7c..c62bcb36 100644 --- a/src/Endpoints/ShipmentEndpoint.php +++ b/src/Endpoints/ShipmentEndpoint.php @@ -121,7 +121,7 @@ public function getForId($orderId, $shipmentId, array $parameters = []) public function update($orderId, $shipmentId, array $data = []) { if (empty($shipmentId) || strpos($shipmentId, self::RESOURCE_ID_PREFIX) !== 0) { - throw new ApiException("Invalid subscription ID: '{$shipmentId}'. An subscription ID should start with '".self::RESOURCE_ID_PREFIX."'."); + throw new ApiException("Invalid subscription ID: '{$shipmentId}'. An subscription ID should start with '" . self::RESOURCE_ID_PREFIX . "'."); } $this->parentId = $orderId; diff --git a/src/Endpoints/SubscriptionEndpoint.php b/src/Endpoints/SubscriptionEndpoint.php index d9b919af..b0975049 100644 --- a/src/Endpoints/SubscriptionEndpoint.php +++ b/src/Endpoints/SubscriptionEndpoint.php @@ -2,6 +2,7 @@ namespace Mollie\Api\Endpoints; +use Generator; use Mollie\Api\Exceptions\ApiException; use Mollie\Api\Resources\Customer; use Mollie\Api\Resources\ResourceFactory; @@ -88,7 +89,7 @@ public function createForId($customerId, array $options = [], array $filters = [ public function update($customerId, $subscriptionId, array $data = []) { if (empty($subscriptionId) || strpos($subscriptionId, self::RESOURCE_ID_PREFIX) !== 0) { - throw new ApiException("Invalid subscription ID: '{$subscriptionId}'. An subscription ID should start with '".self::RESOURCE_ID_PREFIX."'."); + throw new ApiException("Invalid subscription ID: '{$subscriptionId}'. An subscription ID should start with '" . self::RESOURCE_ID_PREFIX . "'."); } $this->parentId = $customerId; @@ -138,6 +139,22 @@ public function listFor(Customer $customer, $from = null, $limit = null, array $ return $this->listForId($customer->id, $from, $limit, $parameters); } + /** + * Create an iterator for iterating over subscriptions for the given customer, retrieved from Mollie. + * + * @param Customer $customer + * @param string $from The first resource ID you want to include in your list. + * @param int $limit + * @param array $parameters + * @param boolean $iterateBackwards Set to true for reverse order iteration (default is false). + * + * @return Generator + */ + public function iteratorFor(Customer $customer, ?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): Generator + { + return $this->iteratorForId($customer->id, $from, $limit, $parameters, $iterateBackwards); + } + /** * @param string $customerId * @param string $from The first resource ID you want to include in your list. @@ -154,6 +171,24 @@ public function listForId($customerId, $from = null, $limit = null, array $param return parent::rest_list($from, $limit, $parameters); } + /** + * Create an iterator for iterating over subscriptions for the given customer id, retrieved from Mollie. + * + * @param string $customerId + * @param string $from The first resource ID you want to include in your list. + * @param int $limit + * @param array $parameters + * @param boolean $iterateBackwards Set to true for reverse order iteration (default is false). + * + * @return Generator + */ + public function iteratorForId(string $customerId, ?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): Generator + { + $this->parentId = $customerId; + + return $this->rest_iterator($from, $limit, $parameters, $iterateBackwards); + } + /** * @param Customer $customer * @param string $subscriptionId @@ -209,4 +244,21 @@ public function page($from = null, $limit = null, array $parameters = []) return $collection; } + + /** + * Create an iterator for iterating over subscriptions retrieved from Mollie. + * + * @param string $from The first resource ID you want to include in your list. + * @param int $limit + * @param array $parameters + * @param boolean $iterateBackwards Set to true for reverse order iteration (default is false). + * + * @return Generator + */ + public function iterator(?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): Generator + { + $page = $this->page($from, $limit, $parameters); + + return $this->iterate($page, $iterateBackwards); + } } diff --git a/src/Endpoints/TerminalEndpoint.php b/src/Endpoints/TerminalEndpoint.php index 4b100b56..9b1d90a3 100644 --- a/src/Endpoints/TerminalEndpoint.php +++ b/src/Endpoints/TerminalEndpoint.php @@ -2,6 +2,7 @@ namespace Mollie\Api\Endpoints; +use Generator; use Mollie\Api\Exceptions\ApiException; use Mollie\Api\Resources\Terminal; use Mollie\Api\Resources\TerminalCollection; @@ -69,4 +70,19 @@ public function page($from = null, $limit = null, array $parameters = []) { return $this->rest_list($from, $limit, $parameters); } + + /** + * Create an iterator for iterating over terminals retrieved from Mollie. + * + * @param string $from The first resource ID you want to include in your list. + * @param int $limit + * @param array $parameters + * @param boolean $iterateBackwards Set to true for reverse order iteration (default is false). + * + * @return Generator + */ + public function iterator(?string $from = null, ?int $limit = null, array $parameters = [], bool $iterateBackwards = false): Generator + { + return $this->rest_iterator($from, $limit, $parameters, $iterateBackwards); + } } diff --git a/tests/Mollie/API/Endpoints/BalanceEndpointTest.php b/tests/Mollie/API/Endpoints/BalanceEndpointTest.php index 5f1a2ad8..c2b14129 100644 --- a/tests/Mollie/API/Endpoints/BalanceEndpointTest.php +++ b/tests/Mollie/API/Endpoints/BalanceEndpointTest.php @@ -187,6 +187,119 @@ public function testListBalances() ); } + public function testIterateBalances() + { + $this->mockApiCall( + new Request( + "GET", + "/v2/balances" + ), + new Response( + 200, + [], + '{ + "count": 2, + "_embedded": { + "balances": [ + { + "resource": "balance", + "id": "bal_gVMhHKqSSRYJyPsuoPNFH", + "mode": "live", + "createdAt": "2019-01-10T12:06:28+00:00", + "currency": "EUR", + "status": "active", + "availableAmount": { + "value": "0.00", + "currency": "EUR" + }, + "incomingAmount": { + "value": "0.00", + "currency": "EUR" + }, + "outgoingAmount": { + "value": "0.00", + "currency": "EUR" + }, + "transferFrequency": "daily", + "transferThreshold": { + "value": "40.00", + "currency": "EUR" + }, + "transferReference": "Mollie payout", + "transferDestination": { + "type": "bank-account", + "beneficiaryName": "Jack Bauer", + "bankAccount": "NL53INGB0654422370", + "bankAccountId": "bnk_jrty3f" + }, + "_links": { + "self": { + "href": "https://api.mollie.com/v2/balances/bal_gVMhHKqSSRYJyPsuoPNFH", + "type": "application/hal+json" + } + } + }, + { + "resource": "balance", + "id": "bal_gVMhHKqSSRYJyPsuoPABC", + "mode": "live", + "createdAt": "2019-01-10T10:23:41+00:00", + "status": "active", + "currency": "EUR", + "availableAmount": { + "value": "0.00", + "currency": "EUR" + }, + "incomingAmount": { + "value": "0.00", + "currency": "EUR" + }, + "outgoingAmount": { + "value": "0.00", + "currency": "EUR" + }, + "transferFrequency": "twice-a-month", + "transferThreshold": { + "value": "5.00", + "currency": "EUR" + }, + "transferReference": "Mollie payout", + "transferDestination": { + "type": "bank-account", + "beneficiaryName": "Jack Bauer", + "bankAccount": "NL97MOLL6351480700", + "bankAccountId": "bnk_jrty3e" + }, + "_links": { + "self": { + "href": "https://api.mollie.com/v2/balances/bal_gVMhHKqSSRYJyPsuoPABC", + "type": "application/hal+json" + } + } + } + ] + }, + "_links": { + "documentation": { + "href": "https://docs.mollie.com/reference/v2/balances-api/list-balances", + "type": "text/html" + }, + "self": { + "href": "https://api.mollie.com/v2/balances?limit=5", + "type": "application/hal+json" + }, + "previous": null, + "next": null + } + }' + ) + ); + + foreach ($this->apiClient->balances->iterator() as $balance) { + $this->assertInstanceOf(Balance::class, $balance); + } + } + public function testGetBalance() { $this->mockApiCall( diff --git a/tests/Mollie/API/Endpoints/BalanceTransactionEndpointTest.php b/tests/Mollie/API/Endpoints/BalanceTransactionEndpointTest.php index 6e9aefe0..55237ae8 100644 --- a/tests/Mollie/API/Endpoints/BalanceTransactionEndpointTest.php +++ b/tests/Mollie/API/Endpoints/BalanceTransactionEndpointTest.php @@ -7,6 +7,7 @@ use GuzzleHttp\Psr7\Request; use GuzzleHttp\Psr7\Response; use Mollie\Api\Resources\Balance; +use Mollie\Api\Resources\BalanceTransaction; use Mollie\Api\Resources\BalanceTransactionCollection; use Mollie\Api\Resources\BaseCollection; use Tests\Mollie\TestHelpers\AmountObjectTestHelpers; @@ -97,6 +98,86 @@ public function testGetBalanceTransactionsThroughEndpoint() $this->assertTransactions($transactions); } + public function testIteratorForBalanceTransactionsThroughEndpoint() + { + $this->mockApiCall( + new Request("GET", "/v2/balances/bal_gVMhHKqSSRYJyPsuoPNFH/transactions"), + new Response( + 200, + [], + '{ + "count": 2, + "_embedded": { + "balance_transactions": [ + { + "resource": "balance_transaction", + "id": "baltr_QM24QwzUWR4ev4Xfgyt29A", + "type": "refund", + "resultAmount": { + "value": "-10.25", + "currency": "EUR" + }, + "initialAmount": { + "value": "-10.00", + "currency": "EUR" + }, + "deductions": { + "value": "-0.25", + "currency": "EUR" + }, + "createdAt": "2021-01-10T12:06:28+00:00", + "context": { + "paymentId": "tr_7UhSN1zuXS", + "refundId": "re_4qqhO89gsT" + } + }, + { + "resource": "balance_transaction", + "id": "baltr_QM24QwzUWR4ev4Xfgyt29B", + "type": "payment", + "resultAmount": { + "value": "9.71", + "currency": "EUR" + }, + "initialAmount": { + "value": "10.00", + "currency": "EUR" + }, + "deductions": { + "value": "-0.29", + "currency": "EUR" + }, + "createdAt": "2021-01-10T12:06:28+00:00", + "context": { + "paymentId": "tr_7UhSN1zuXS" + } + } + ] + }, + "_links": { + "documentation": { + "href": "https://docs.mollie.com/reference/v2/balances-api/list-balance-transactions", + "type": "text/html" + }, + "self": { + "href": "https://api.mollie.com/v2/balances/bal_gVMhHKqSSRYJyPsuoPNFH/transactions?limit=5", + "type": "application/hal+json" + }, + "previous": null, + "next": null + } + }' + ) + ); + + $balance = new Balance($this->apiClient); + $balance->id = "bal_gVMhHKqSSRYJyPsuoPNFH"; + + foreach ($this->apiClient->balanceTransactions->iteratorFor($balance) as $balanceTransactions) { + $this->assertInstanceOf(BalanceTransaction::class, $balanceTransactions); + } + } + public function testGetPrimaryBalanceTransactionsThroughBalanceTransactionEndpoint() { $this->mockApiCall( @@ -174,6 +255,83 @@ public function testGetPrimaryBalanceTransactionsThroughBalanceTransactionEndpoi $this->assertTransactions($transactions); } + public function testIteratorForPrimaryBalanceTransactionsThroughBalanceTransactionEndpoint() + { + $this->mockApiCall( + new Request("GET", "/v2/balances/primary/transactions"), + new Response( + 200, + [], + '{ + "count": 2, + "_embedded": { + "balance_transactions": [ + { + "resource": "balance_transaction", + "id": "baltr_QM24QwzUWR4ev4Xfgyt29A", + "type": "refund", + "resultAmount": { + "value": "-10.25", + "currency": "EUR" + }, + "initialAmount": { + "value": "-10.00", + "currency": "EUR" + }, + "deductions": { + "value": "-0.25", + "currency": "EUR" + }, + "createdAt": "2021-01-10T12:06:28+00:00", + "context": { + "paymentId": "tr_7UhSN1zuXS", + "refundId": "re_4qqhO89gsT" + } + }, + { + "resource": "balance_transaction", + "id": "baltr_QM24QwzUWR4ev4Xfgyt29B", + "type": "payment", + "resultAmount": { + "value": "9.71", + "currency": "EUR" + }, + "initialAmount": { + "value": "10.00", + "currency": "EUR" + }, + "deductions": { + "value": "-0.29", + "currency": "EUR" + }, + "createdAt": "2021-01-10T12:06:28+00:00", + "context": { + "paymentId": "tr_7UhSN1zuXS" + } + } + ] + }, + "_links": { + "documentation": { + "href": "https://docs.mollie.com/reference/v2/balances-api/list-balance-transactions", + "type": "text/html" + }, + "self": { + "href": "https://api.mollie.com/v2/balances/bal_gVMhHKqSSRYJyPsuoPNFH/transactions?limit=5", + "type": "application/hal+json" + }, + "previous": null, + "next": null + } + }' + ) + ); + + foreach ($this->apiClient->balanceTransactions->iteratorForPrimary() as $balanceTransactions) { + $this->assertInstanceOf(BalanceTransaction::class, $balanceTransactions); + } + } + private function assertTransactions(BaseCollection $transactions) { $this->assertInstanceOf(BalanceTransactionCollection::class, $transactions); diff --git a/tests/Mollie/API/Endpoints/ChargebackEndpointTest.php b/tests/Mollie/API/Endpoints/ChargebackEndpointTest.php index 38ad17ef..44fb3b56 100644 --- a/tests/Mollie/API/Endpoints/ChargebackEndpointTest.php +++ b/tests/Mollie/API/Endpoints/ChargebackEndpointTest.php @@ -129,6 +129,104 @@ public function testListChargebacks() $this->assertChargeback($chargebacks[1], 'tr_nQKWJbDj7j', 'chb_6cqlwf', "-0.37", null); } + public function testIterateChargebacks() + { + $this->mockApiCall( + new Request( + "GET", + "/v2/chargebacks" + ), + new Response( + 200, + [], + '{ + "_embedded":{ + "chargebacks":[ + { + "resource":"chargeback", + "id":"chb_n9z0tp", + "amount":{ + "value":"-13.00", + "currency":"EUR" + }, + "createdAt":"2018-03-28T11:44:32+00:00", + "paymentId":"tr_44aKxzEbr8", + "settlementAmount":{ + "value":"-13.00", + "currency":"EUR" + }, + "reversedAt": null, + "reason":{ + "code":"AC01", + "description":"" + }, + "_links":{ + "self":{ + "href":"https://api.mollie.com/v2/payments/tr_44aKxzEbr8/chargebacks/chb_n9z0tp", + "type":"application/hal+json" + }, + "payment":{ + "href":"https://api.mollie.com/v2/payments/tr_44aKxzEbr8", + "type":"application/hal+json" + }, + "documentation": { + "href": "https://docs.mollie.com/reference/v2/chargebacks-api/get-chargeback", + "type": "text/html" + } + } + }, + { + "resource":"chargeback", + "id":"chb_6cqlwf", + "amount":{ + "value":"-0.37", + "currency":"EUR" + }, + "createdAt":"2018-03-28T11:44:32+00:00", + "paymentId":"tr_nQKWJbDj7j", + "settlementAmount":{ + "value":"-0.37", + "currency":"EUR" + }, + "reversedAt": null, + "reason": null, + "_links":{ + "self":{ + "href":"https://api.mollie.com/v2/payments/tr_nQKWJbDj7j/chargebacks/chb_6cqlwf", + "type":"application/hal+json" + }, + "payment":{ + "href":"https://api.mollie.com/v2/payments/tr_nQKWJbDj7j", + "type":"application/hal+json" + }, + "documentation": { + "href": "https://docs.mollie.com/reference/v2/chargebacks-api/get-chargeback", + "type": "text/html" + } + } + } + ] + }, + "_links":{ + "documentation":{ + "href":"https://docs.mollie.com/reference/v2/chargebacks-api/list-chargebacks", + "type":"text/html" + }, + "self":{ + "href":"https://api.mollie.com/v2/chargebacks", + "type":"application/hal+json" + } + }, + "count": 2 + }' + ) + ); + + foreach ($this->apiClient->chargebacks->iterator() as $chargeback) { + $this->assertInstanceOf(Chargeback::class, $chargeback); + } + } + protected function assertChargeback($chargeback, $paymentId, $chargebackId, $amount, $reasonCode) { $this->assertInstanceOf(Chargeback::class, $chargeback); diff --git a/tests/Mollie/API/Endpoints/CustomerEndpointTest.php b/tests/Mollie/API/Endpoints/CustomerEndpointTest.php index e3a5eae3..20e939ba 100644 --- a/tests/Mollie/API/Endpoints/CustomerEndpointTest.php +++ b/tests/Mollie/API/Endpoints/CustomerEndpointTest.php @@ -157,6 +157,53 @@ public function testListWorks() } } + public function testIteratorWorks() + { + $this->mockApiCall( + new Request('GET', '/v2/customers'), + new Response( + 200, + [], + '{ + "_embedded": { + "customers": [ + { + "resource": "customer", + "id": "cst_FhQJRw4s2n", + "mode": "test", + "name": "John Doe", + "email": "johndoe@example.org", + "locale": null, + "metadata": null, + "recentlyUsedMethods": [], + "createdAt": "2018-04-19T08:49:01+00:00" + } + ] + }, + "count": 1, + "_links": { + "documentation": { + "href": "https://docs.mollie.com/reference/v2/customers-api/list-customers", + "type": "text/html" + }, + "self": { + "href": "https://api.mollie.com/v2/customers?limit=50", + "type": "application/hal+json" + }, + "previous": null, + "next": null + } + }' + ) + ); + + foreach ($this->apiClient->customers->iterator() as $customer) { + $this->assertInstanceOf(Customer::class, $customer); + $this->assertEquals("customer", $customer->resource); + $this->assertNotEmpty($customer->createdAt); + } + } + public function testUpdateWorks() { $expectedName = 'Kaas Broodje'; diff --git a/tests/Mollie/API/Endpoints/CustomerPaymentEndpointTest.php b/tests/Mollie/API/Endpoints/CustomerPaymentEndpointTest.php index 2dcb1d75..2de0d4b7 100644 --- a/tests/Mollie/API/Endpoints/CustomerPaymentEndpointTest.php +++ b/tests/Mollie/API/Endpoints/CustomerPaymentEndpointTest.php @@ -21,7 +21,7 @@ public function testCreateCustomerPayment() "/v2/customers/cst_FhQJRw4s2n/payments", [], '{ - "amount":{ + "amount":{ "value":"20.00", "currency":"EUR" }, @@ -41,13 +41,13 @@ public function testCreateCustomerPayment() "id":"tr_44aKxzEbr8", "mode":"test", "createdAt":"2018-03-13T14:02:29+00:00", - "amount":{ + "amount":{ "value":"20.00", "currency":"EUR" }, "description":"My first API payment", "method":null, - "metadata":{ + "metadata":{ "order_id":"1234" }, "status":"open", @@ -58,12 +58,12 @@ public function testCreateCustomerPayment() "sequenceType":"oneoff", "redirectUrl":"https://example.org/redirect", "webhookUrl":"https://example.org/webhook", - "_links":{ - "self":{ + "_links":{ + "self":{ "href":"https://api.mollie.com/v2/payments/tr_44aKxzEbr8", "type":"application/hal+json" }, - "checkout":{ + "checkout":{ "href":"https://www.mollie.com/payscreen/select-method/44aKxzEbr8", "type":"text/html" }, @@ -71,7 +71,7 @@ public function testCreateCustomerPayment() "href": "https://api.mollie.com/v2/customers/cst_FhQJRw4s2n", "type": "application/hal+json" }, - "documentation":{ + "documentation":{ "href":"https://docs.mollie.com/reference/v2/customers-api/create-payment", "type":"text/html" } diff --git a/tests/Mollie/API/Endpoints/InvoiceEndpointTest.php b/tests/Mollie/API/Endpoints/InvoiceEndpointTest.php index cc25b24f..3e251fd4 100644 --- a/tests/Mollie/API/Endpoints/InvoiceEndpointTest.php +++ b/tests/Mollie/API/Endpoints/InvoiceEndpointTest.php @@ -218,4 +218,102 @@ public function testListInvoices() $this->assertNotEmpty($invoice->lines); } } + + public function testIterateInvoices() + { + $this->mockApiCall( + new Request( + "GET", + "/v2/invoices", + [], + '' + ), + new Response( + 200, + [], + '{ + "_embedded": { + "invoices": [ + { + "resource": "invoice", + "id": "inv_bsa6PvAwaK", + "reference": "2018.190241", + "vatNumber": "123456789B01", + "status": "paid", + "issuedAt": "2018-05-02", + "paidAt": "2018-05-02", + "netAmount": { + "value": "100.00", + "currency": "EUR" + }, + "vatAmount": { + "value": "0.00", + "currency": "EUR" + }, + "grossAmount": { + "value": "100.00", + "currency": "EUR" + }, + "lines": [ + { + "period": "2018-04", + "description": "iDEAL transaction costs: april 2018", + "count": 1337, + "vatPercentage": 0, + "amount": { + "value": "50.00", + "currency": "EUR" + } + }, + { + "period": "2018-04", + "description": "Refunds iDEAL: april 2018", + "count": 1337, + "vatPercentage": 0, + "amount": { + "value": "50.00", + "currency": "EUR" + } + } + ], + "_links": { + "self": { + "href": "https://api.mollie.com/v2/invoices/inv_bsa6PvAwaK", + "type": "application/hal+json" + }, + "pdf": { + "href": "https://www.mollie.com/merchant/download/invoice/bsa6PvAwaK/79aa10f49132b7844c0243648ade6985", + "type": "application/pdf" + }, + "documentation": { + "href": "https://docs.mollie.com/reference/v2/invoices-api/get-invoice", + "type": "text/html" + } + } + } + ] + }, + "count": 1, + "_links": { + "documentation": { + "href": "https://docs.mollie.com/reference/v2/invoices-api/list-invoices", + "type": "text/html" + }, + "self": { + "href": "https://api.mollie.nl/v2/invoices?limit=50", + "type": "application/hal+json" + }, + "previous": null, + "next": null + } + }' + ) + ); + + foreach ($this->apiClient->invoices->iterator() as $invoice) { + $this->assertInstanceOf(Invoice::class, $invoice); + $this->assertEquals("invoice", $invoice->resource); + $this->assertNotEmpty($invoice->lines); + } + } } diff --git a/tests/Mollie/API/Endpoints/OrderEndpointTest.php b/tests/Mollie/API/Endpoints/OrderEndpointTest.php index a1985a63..998d7f28 100644 --- a/tests/Mollie/API/Endpoints/OrderEndpointTest.php +++ b/tests/Mollie/API/Endpoints/OrderEndpointTest.php @@ -122,32 +122,32 @@ public function testCreateOrder() $order = $this->apiClient->orders->create([ "amount" => [ - "value" => "1027.99", - "currency" => "EUR", + "value" => "1027.99", + "currency" => "EUR", ], "billingAddress" => [ - "organizationName" => "Organization Name LTD.", - "streetAndNumber" => "Keizersgracht 313", - "postalCode" => "1016 EE", - "city" => "Amsterdam", - "country" => "nl", - "givenName" => "Luke", - "familyName" => "Skywalker", - "email" => "luke@skywalker.com", + "organizationName" => "Organization Name LTD.", + "streetAndNumber" => "Keizersgracht 313", + "postalCode" => "1016 EE", + "city" => "Amsterdam", + "country" => "nl", + "givenName" => "Luke", + "familyName" => "Skywalker", + "email" => "luke@skywalker.com", ], "shippingAddress" => [ - "organizationName" => "Organization Name LTD.", - "streetAndNumber" => "Keizersgracht 313", - "postalCode" => "1016 EE", - "city" => "Amsterdam", - "country" => "nl", - "givenName" => "Luke", - "familyName" => "Skywalker", - "email" => "luke@skywalker.com", + "organizationName" => "Organization Name LTD.", + "streetAndNumber" => "Keizersgracht 313", + "postalCode" => "1016 EE", + "city" => "Amsterdam", + "country" => "nl", + "givenName" => "Luke", + "familyName" => "Skywalker", + "email" => "luke@skywalker.com", ], "metadata" => [ - "order_id" => "1337", - "description" => "Lego cars", + "order_id" => "1337", + "description" => "Lego cars", ], "consumerDateOfBirth" => "1958-01-31", "locale" => "nl_NL", @@ -569,6 +569,43 @@ public function testListOrders() $this->assertOrder($orders[2], 'ord_pbjz3z'); } + public function testIterateOrders() + { + $this->mockApiCall( + new Request("GET", "/v2/orders"), + new Response( + 200, + [], + '{ + "count": 3, + "_embedded": { + "orders": [ + ' . $this->getOrderResponseFixture("ord_pbjz1x") . ', + ' . $this->getOrderResponseFixture("ord_pbjz2y") . ', + ' . $this->getOrderResponseFixture("ord_pbjz3z") . ' + ] + }, + "_links": { + "self": { + "href": "https://api.mollie.com/v2/orders", + "type": "application/hal+json" + }, + "previous": null, + "next": null, + "documentation": { + "href": "https://docs.mollie.com/reference/v2/orders-api/list-orders", + "type": "text/html" + } + } + }' + ) + ); + + foreach ($this->apiClient->orders->iterator() as $order) { + $this->assertInstanceOf(Order::class, $order); + } + } + public function testCancelOrderDirectly() { $this->mockApiCall( @@ -780,10 +817,10 @@ public function testUpdateOrderLine() $orderLine->sku = '5702016116977'; $orderLine->quantity = 2; $orderLine->vatRate = '21.00'; - $orderLine->unitPrice = (object) ['currency' => 'EUR','value' => '349.00']; - $orderLine->totalAmount = (object) ['currency' => 'EUR','value' => '598.00']; - $orderLine->discountAmount = (object) ['currency' => 'EUR','value' => '100.00']; - $orderLine->vatAmount = (object) ['currency' => 'EUR','value' => '103.79']; + $orderLine->unitPrice = (object) ['currency' => 'EUR', 'value' => '349.00']; + $orderLine->totalAmount = (object) ['currency' => 'EUR', 'value' => '598.00']; + $orderLine->discountAmount = (object) ['currency' => 'EUR', 'value' => '100.00']; + $orderLine->vatAmount = (object) ['currency' => 'EUR', 'value' => '103.79']; $orderLine->metadata = (object) ['foo' => 'bar']; $result = $orderLine->update(); @@ -807,8 +844,8 @@ protected function assertOrder($order, $order_id, $order_status = OrderStatus::S $this->assertAmountObject('0.00', 'EUR', $order->amountRefunded); $this->assertEquals((object) [ - 'order_id' => '1337', - 'description' => 'Lego cars', + 'order_id' => '1337', + 'description' => 'Lego cars', ], $order->metadata); $this->assertEquals($order_status, $order->status); @@ -841,19 +878,19 @@ protected function assertOrder($order, $order_id, $order_status = OrderStatus::S $this->assertEquals("https://example.org/redirect", $order->redirectUrl); $this->assertEquals("https://example.org/webhook", $order->webhookUrl); - $links = (object )[ - 'self' => $this->createLinkObject( - 'https://api.mollie.com/v2/orders/' . $order_id, - 'application/hal+json' - ), - 'checkout' => $this->createLinkObject( - 'https://www.mollie.com/payscreen/select-method/7UhSN1zuXS', - 'text/html' - ), - 'documentation' => $this->createLinkObject( - 'https://docs.mollie.com/reference/v2/orders-api/get-order', - 'text/html' - ), + $links = (object)[ + 'self' => $this->createLinkObject( + 'https://api.mollie.com/v2/orders/' . $order_id, + 'application/hal+json' + ), + 'checkout' => $this->createLinkObject( + 'https://www.mollie.com/payscreen/select-method/7UhSN1zuXS', + 'text/html' + ), + 'documentation' => $this->createLinkObject( + 'https://docs.mollie.com/reference/v2/orders-api/get-order', + 'text/html' + ), ]; $this->assertEquals($links, $order->_links); diff --git a/tests/Mollie/API/Endpoints/PaymentEndpointTest.php b/tests/Mollie/API/Endpoints/PaymentEndpointTest.php index fca9112a..f742501d 100644 --- a/tests/Mollie/API/Endpoints/PaymentEndpointTest.php +++ b/tests/Mollie/API/Endpoints/PaymentEndpointTest.php @@ -23,7 +23,7 @@ public function testCreatePayment() "/v2/payments", [], '{ - "amount":{ + "amount":{ "value":"20.00", "currency":"EUR" }, @@ -43,13 +43,13 @@ public function testCreatePayment() "id":"tr_44aKxzEbr8", "mode":"test", "createdAt":"2018-03-13T14:02:29+00:00", - "amount":{ + "amount":{ "value":"20.00", "currency":"EUR" }, "description":"My first API payment", "method":null, - "metadata":{ + "metadata":{ "order_id":"1234" }, "status":"open", @@ -60,16 +60,16 @@ public function testCreatePayment() "sequenceType":"oneoff", "redirectUrl":"https://example.org/redirect", "webhookUrl":"https://example.org/webhook", - "_links":{ - "self":{ + "_links":{ + "self":{ "href":"https://api.mollie.com/v2/payments/tr_44aKxzEbr8", "type":"application/hal+json" }, - "checkout":{ + "checkout":{ "href":"https://www.mollie.com/payscreen/select-method/44aKxzEbr8", "type":"text/html" }, - "documentation":{ + "documentation":{ "href":"https://docs.mollie.com/reference/v2/payments-api/create-payment", "type":"text/html" } @@ -246,31 +246,31 @@ public function testGetPayment() new Response( 200, [], - '{ + '{ "resource":"payment", "id":"tr_44aKxzEbr8", "mode":"test", "createdAt":"2018-03-13T14:02:29+00:00", - "amount":{ + "amount":{ "value":"20.00", "currency":"EUR" }, "description":"My first API payment", "method":"ideal", - "metadata":{ + "metadata":{ "order_id":"1234" }, "status":"paid", "paidAt":"2018-03-19T12:18:35+00:00", - "amountRefunded":{ + "amountRefunded":{ "value":"0.00", "currency":"EUR" }, - "amountRemaining":{ + "amountRemaining":{ "value":"20.00", "currency":"EUR" }, - "details":{ + "details":{ "consumerName":"T. TEST", "consumerAccount":"NL17RABO0213698412", "consumerBic":"TESTNL99" @@ -281,16 +281,16 @@ public function testGetPayment() "sequenceType":"oneoff", "redirectUrl":"https://example.org/redirect", "webhookUrl":"https://example.org/webhook", - "settlementAmount":{ + "settlementAmount":{ "value":"20.00", "currency":"EUR" }, - "_links":{ - "self":{ + "_links":{ + "self":{ "href":"https://api.mollie.com/v2/payments/tr_44aKxzEbr8", "type":"application/hal+json" }, - "documentation":{ + "documentation":{ "href":"https://docs.mollie.com/reference/v2/payments-api/get-payment", "type":"text/html" } @@ -492,4 +492,137 @@ public function testListPayment() $nextLink = (object)["href" => "http://api.mollie.com/v2/payments?from=tr_eW8f5kzUkF&limit=3", "type" => "application/hal+json"]; $this->assertEquals($nextLink, $payments->_links->next); } + + public function testIteratePayment() + { + $this->mockApiCall( + new Request( + "GET", + "/v2/payments", + [], + '' + ), + new Response( + 200, + [], + '{ + "_embedded": { + "payments": [ + { + "resource": "payment", + "id": "tr_admNa2tFfa", + "mode": "test", + "createdAt": "2018-03-19T15:00:50+00:00", + "amount": { + "value": "100.00", + "currency": "EUR" + }, + "description": "Payment no 1", + "method": null, + "metadata": null, + "status": "open", + "isCancelable": false, + "expiresAt": "2018-03-19T15:15:50+00:00", + "details": null, + "locale": "nl_NL", + "profileId": "pfl_7N5qjbu42V", + "sequenceType": "oneoff", + "redirectUrl": "https://www.example.org/", + "_links": { + "self": { + "href": "https://api.mollie.com/v2/payments/tr_admNa2tFfa", + "type": "application/hal+json" + }, + "checkout": { + "href": "https://www.mollie.com/payscreen/select-method/admNa2tFfa", + "type": "text/html" + } + } + }, + { + "resource": "payment", + "id": "tr_bcaLc7hFfa", + "mode": "test", + "createdAt": "2018-03-19T15:00:50+00:00", + "amount": { + "value": "100.00", + "currency": "EUR" + }, + "description": "Payment no 2", + "method": null, + "metadata": null, + "status": "open", + "isCancelable": false, + "expiresAt": "2018-03-19T15:15:50+00:00", + "details": null, + "locale": "nl_NL", + "profileId": "pfl_7N5qjbu42V", + "sequenceType": "oneoff", + "redirectUrl": "https://www.example.org/", + "_links": { + "self": { + "href": "https://api.mollie.com/v2/payments/tr_bcaLc7hFfa", + "type": "application/hal+json" + }, + "checkout": { + "href": "https://www.mollie.com/payscreen/select-method/bcaLc7hFfa", + "type": "text/html" + } + } + }, + { + "resource": "payment", + "id": "tr_pslHy1tFfa", + "mode": "test", + "createdAt": "2018-03-19T15:00:50+00:00", + "amount": { + "value": "100.00", + "currency": "EUR" + }, + "description": "Payment no 3", + "method": null, + "metadata": null, + "status": "open", + "isCancelable": false, + "expiresAt": "2018-03-19T15:15:50+00:00", + "details": null, + "locale": "nl_NL", + "profileId": "pfl_7N5qjbu42V", + "sequenceType": "oneoff", + "redirectUrl": "https://www.example.org/", + "_links": { + "self": { + "href": "https://api.mollie.com/v2/payments/tr_pslHy1tFfa", + "type": "application/hal+json" + }, + "checkout": { + "href": "https://www.mollie.com/payscreen/select-method/pslHy1tFfa", + "type": "text/html" + } + } + } + ] + }, + "_links": { + "documentation": { + "href": "https://docs.mollie.com/reference/v2/payments-api/list-payments", + "type": "text/html" + }, + "self": { + "href": "http://api.mollie.com/v2/payments?limit=3", + "type": "application/hal+json" + }, + "previous": null, + "next": null + }, + "count": 3 + }' + ) + ); + + foreach ($this->apiClient->payments->iterator() as $payment) { + $this->assertInstanceOf(Payment::class, $payment); + $this->assertEquals("payment", $payment->resource); + } + } } diff --git a/tests/Mollie/API/Endpoints/RefundEndpointTest.php b/tests/Mollie/API/Endpoints/RefundEndpointTest.php index 2bc80483..351e2b46 100644 --- a/tests/Mollie/API/Endpoints/RefundEndpointTest.php +++ b/tests/Mollie/API/Endpoints/RefundEndpointTest.php @@ -94,4 +94,70 @@ public function testListRefunds() $paymentLink = (object)["href" => "https://api.mollie.com/v2/payments/tr_44aKxzEbr8", "type" => "application/hal+json"]; $this->assertEquals($paymentLink, $refund->_links->payment); } + + public function testIterateRefunds() + { + $this->mockApiCall( + new Request( + "GET", + "/v2/refunds", + [], + '' + ), + new Response( + 200, + [], + '{ + "_embedded": { + "refunds": [ + { + "resource": "refund", + "id": "re_haCsig5aru", + "amount": { + "value": "2.00", + "currency": "EUR" + }, + "status": "pending", + "createdAt": "2018-03-28T10:56:10+00:00", + "description": "My first API payment", + "paymentId": "tr_44aKxzEbr8", + "settlementAmount": { + "value": "-2.00", + "currency": "EUR" + }, + "_links": { + "self": { + "href": "https://api.mollie.com/v2/payments/tr_44aKxzEbr8/refunds/re_haCsig5aru", + "type": "application/hal+json" + }, + "payment": { + "href": "https://api.mollie.com/v2/payments/tr_44aKxzEbr8", + "type": "application/hal+json" + } + } + } + ] + }, + "_links": { + "documentation": { + "href": "https://docs.mollie.com/reference/v2/refunds-api/list-refunds", + "type": "text/html" + }, + "self": { + "href": "http://api.mollie.nl/v2/refunds?limit=10", + "type": "application/hal+json" + }, + "previous": null, + "next": null + }, + "count": 1 + }' + ) + ); + + foreach ($this->apiClient->refunds->iterator() as $refund) { + $this->assertInstanceOf(Refund::class, $refund); + $this->assertEquals("refund", $refund->resource); + } + } } diff --git a/tests/Mollie/API/Endpoints/SettlementEndpointTest.php b/tests/Mollie/API/Endpoints/SettlementEndpointTest.php index 592020aa..a0e8ae31 100644 --- a/tests/Mollie/API/Endpoints/SettlementEndpointTest.php +++ b/tests/Mollie/API/Endpoints/SettlementEndpointTest.php @@ -446,4 +446,209 @@ public function testListSettlement() $this->assertNotEmpty($settlement->periods); } } + + public function testIterateSettlement() + { + $this->mockApiCall( + new Request( + "GET", + "/v2/settlements", + [], + '' + ), + new Response( + 200, + [], + '{ + "_embedded": { + "settlements": [ + { + "resource": "settlement", + "id": "stl_xcaSGAHuRt", + "reference": "1234567.1234.12", + "createdAt": "2018-04-30T04:00:02+00:00", + "settledAt": "2018-05-01T04:00:02+00:00", + "status": "pending", + "amount": { + "value": "1980.98", + "currency": "EUR" + }, + "periods": { + "2018": { + "04": { + "revenue": [ + { + "description": "Creditcard", + "method": "creditcard", + "count": 2, + "amountNet": { + "value": "790.00", + "currency": "EUR" + }, + "amountVat": null, + "amountGross": { + "value": "1000.00", + "currency": "EUR" + } + }, + { + "description": "iDEAL", + "method": "ideal", + "count": 2, + "amountNet": { + "value": "790.00", + "currency": "EUR" + }, + "amountVat": null, + "amountGross": { + "value": "1000.00", + "currency": "EUR" + } + } + ], + "costs": [ + { + "description": "Creditcard", + "method": "creditcard", + "count": 2, + "rate": { + "fixed": { + "value": "0.00", + "currency": "EUR" + }, + "percentage": "1.80" + }, + "amountNet": { + "value": "14.22", + "currency": "EUR" + }, + "amountVat": { + "value": "2.9862", + "currency": "EUR" + }, + "amountGross": { + "value": "17.2062", + "currency": "EUR" + } + }, + { + "description": "Fixed creditcard costs", + "method": "creditcard", + "count": 2, + "rate": { + "fixed": { + "value": "0.25", + "currency": "EUR" + }, + "percentage": "0" + }, + "amountNet": { + "value": "0.50", + "currency": "EUR" + }, + "amountVat": { + "value": "0.105", + "currency": "EUR" + }, + "amountGross": { + "value": "0.605", + "currency": "EUR" + } + }, + { + "description": "Fixed iDEAL costs", + "method": "ideal", + "count": 2, + "rate": { + "fixed": { + "value": "0.25", + "currency": "EUR" + }, + "percentage": "0" + }, + "amountNet": { + "value": "0.50", + "currency": "EUR" + }, + "amountVat": { + "value": "0.105", + "currency": "EUR" + }, + "amountGross": { + "value": "0.605", + "currency": "EUR" + } + }, + { + "description": "Refunds iDEAL", + "method": "refund", + "count": 2, + "rate": { + "fixed": { + "value": "0.25", + "currency": "EUR" + }, + "percentage": "0" + }, + "amountNet": { + "value": "0.50", + "currency": "EUR" + }, + "amountVat": { + "value": "0.105", + "currency": "EUR" + }, + "amountGross": { + "value": "0.605", + "currency": "EUR" + } + } + ] + } + } + }, + "_links": { + "self": { + "href": "https://api.mollie.com/v2/settlements/stl_xcaSGAHuRt", + "type": "application/hal+json" + }, + "payments": { + "href": "https://api.mollie.com/v2/settlements/stl_xcaSGAHuRt/payments", + "type": "application/hal+json" + }, + "refunds": { + "href": "https://api.mollie.com/v2/settlements/stl_xcaSGAHuRt/refunds", + "type": "application/hal+json" + }, + "chargebacks": { + "href": "https://api.mollie.com/v2/settlements/stl_xcaSGAHuRt/chargebacks", + "type": "application/hal+json" + } + } + } + ] + }, + "count": 1, + "_links": { + "documentation": { + "href": "https://docs.mollie.com/reference/v2/settlements-api/list-settlements", + "type": "text/html" + }, + "self": { + "href": "https://api.mollie.nl/v2/settlements", + "type": "application/hal+json" + }, + "previous": null, + "next": null + } + }' + ) + ); + + foreach ($this->apiClient->settlements->iterator() as $settlement) { + $this->assertInstanceOf(Settlement::class, $settlement); + $this->assertEquals("settlement", $settlement->resource); + $this->assertNotEmpty($settlement->periods); + } + } } diff --git a/tests/Mollie/API/Endpoints/TerminalEndpointTest.php b/tests/Mollie/API/Endpoints/TerminalEndpointTest.php index 3a9bfccc..e3e0d146 100644 --- a/tests/Mollie/API/Endpoints/TerminalEndpointTest.php +++ b/tests/Mollie/API/Endpoints/TerminalEndpointTest.php @@ -92,6 +92,7 @@ public function testListTerminal() "terminals": [ { "id": "term_7MgL4wea46qkRcoTZjWEH", + "resource": "terminal", "profileId": "pfl_QkEhN94Ba", "status": "active", "brand": "PAX", @@ -113,6 +114,7 @@ public function testListTerminal() }, { "id": "term_7MgL4wea46qkRcoTZjWEG", + "resource": "terminal", "profileId": "pfl_QkEhN94Bb", "status": "pending", "brand": "PAX", @@ -134,6 +136,7 @@ public function testListTerminal() }, { "id": "term_7MgL4wea46qkRcoTZjWEI", + "resource": "terminal", "profileId": "pfl_QkEhN94Bc", "status": "inactive", "brand": "PAX", @@ -186,4 +189,110 @@ public function testListTerminal() $this->assertNull($terminals->_links->previous); } + + public function testIterateTerminal() + { + $this->mockApiCall( + new Request( + "GET", + "/v2/terminals", + [], + '' + ), + new Response( + 200, + [], + '{ + "count": 3, + "_embedded": { + "terminals": [ + { + "id": "term_7MgL4wea46qkRcoTZjWEH", + "resource": "terminal", + "profileId": "pfl_QkEhN94Ba", + "status": "active", + "brand": "PAX", + "model": "A920", + "serialNumber": "1234567890", + "currency": "EUR", + "description": "Terminal #12345", + "timezone": "GMT +08:00", + "locale": "nl_NL", + "createdAt": "2022-02-12T11:58:35.0Z", + "updatedAt": "2022-11-15T13:32:11+00:00", + "activatedAt": "2022-02-12T12:13:35.0Z", + "_links": { + "self": { + "href": "https://api.mollie.com/v2/terminals/term_7MgL4wea46qkRcoTZjWEH", + "type": "application/hal+json" + } + } + }, + { + "id": "term_7MgL4wea46qkRcoTZjWEG", + "resource": "terminal", + "profileId": "pfl_QkEhN94Bb", + "status": "pending", + "brand": "PAX", + "model": "A920", + "serialNumber": "1234567891", + "currency": "EUR", + "description": "Terminal #12346", + "timezone": "GMT +08:00", + "locale": "nl_NL", + "createdAt": "2022-02-13T11:58:35.0Z", + "updatedAt": "2022-11-16T13:32:11+00:00", + "activatedAt": "2022-02-13T12:13:35.0Z", + "_links": { + "self": { + "href": "https://api.mollie.com/v2/terminals/term_7MgL4wea46qkRcoTZjWEG", + "type": "application/hal+json" + } + } + }, + { + "id": "term_7MgL4wea46qkRcoTZjWEI", + "resource": "terminal", + "profileId": "pfl_QkEhN94Bc", + "status": "inactive", + "brand": "PAX", + "model": "A920", + "serialNumber": "1234567892", + "currency": "EUR", + "description": "Terminal #12347", + "timezone": "GMT +08:00", + "locale": "nl_NL", + "createdAt": "2022-02-14T11:58:35.0Z", + "updatedAt": "2022-11-17T13:32:11+00:00", + "activatedAt": "2022-02-14T12:13:35.0Z", + "_links": { + "self": { + "href": "https://api.mollie.com/v2/terminals/term_7MgL4wea46qkRcoTZjWEI", + "type": "application/hal+json" + } + } + } + ] + }, + "_links": { + "self": { + "href": "https://api.mollie.com/v2/terminals", + "type": "application/hal+json" + }, + "previous": null, + "next": null, + "documentation": { + "href": "https://docs.mollie.com/reference/v2/terminals-api/list-terminals", + "type": "text/html" + } + } + }' + ) + ); + + foreach ($this->apiClient->terminals->iterator() as $terminal) { + $this->assertInstanceOf(Terminal::class, $terminal); + $this->assertEquals("terminal", $terminal->resource); + } + } } diff --git a/tests/SettlementRefundEndpointTest.php b/tests/SettlementRefundEndpointTest.php deleted file mode 100644 index 14df7f53..00000000 --- a/tests/SettlementRefundEndpointTest.php +++ /dev/null @@ -1,91 +0,0 @@ -mockApiCall( - new Request( - 'GET', - '/v2/settlements/stl_jDk30akdN/refunds?limit=5&foo=bar' - ), - new Response( - 200, - [], - '{ - "_embedded": { - "refunds": [ - { - "resource": "refund", - "id": "re_3aKhkUNigy", - "amount": { - "value": "10.00", - "currency": "EUR" - }, - "status": "refunded", - "createdAt": "2018-08-30T07:59:02+00:00", - "description": "Order #33", - "paymentId": "tr_maJaG2j8OM", - "settlementAmount": { - "value": "-10.00", - "currency": "EUR" - }, - "settlementId": "stl_jDk30akdN", - "_links": { - "self": { - "href": "https://api.mollie.com/v2/payments/tr_maJaG2j8OM/refunds/re_3aKhkUNigy", - "type": "application/hal+json" - }, - "payment": { - "href": "https://api.mollie.com/v2/payments/tr_maJaG2j8OM", - "type": "application/hal+json" - }, - "settlement": { - "href": "https://api.mollie.com/v2/settlements/stl_jDk30akdN", - "type": "application/hal+json" - } - } - }, - { } - ] - }, - "count": 1, - "_links": { - "documentation": { - "href": "https://docs.mollie.com/reference/v2/settlements-api/list-settlement-refunds", - "type": "text/html" - }, - "self": { - "href": "https://api.mollie.com/v2/settlements/stl_jDk30akdN/refunds?limit=50", - "type": "application/hal+json" - }, - "previous": null, - "next": null - } - }' - ) - ); - - $settlement = new Settlement($this->apiClient); - $settlement->id = 'stl_jDk30akdN'; - - $refunds = $settlement->refunds(5, ['foo' => 'bar']); - - $this->assertInstanceOf(RefundCollection::class, $refunds); - $this->assertCount(2, $refunds); - - $refund = $refunds[0]; - $this->assertInstanceOf(Refund::class, $refund); - $this->assertEquals("re_3aKhkUNigy", $refund->id); - } -} From 4a8760976ecf9cd8b8e819dd56bd033a4ae1e4f1 Mon Sep 17 00:00:00 2001 From: Naoray Date: Thu, 26 Oct 2023 09:58:13 +0000 Subject: [PATCH 04/26] Fix styling --- src/Endpoints/BalanceEndpoint.php | 2 +- src/Endpoints/BalanceTransactionEndpoint.php | 6 +++--- src/Endpoints/ChargebackEndpoint.php | 2 +- src/Endpoints/ClientEndpoint.php | 2 +- src/Endpoints/CollectionEndpointAbstract.php | 6 +++--- src/Endpoints/CustomerEndpoint.php | 2 +- src/Endpoints/CustomerPaymentsEndpoint.php | 4 ++-- src/Endpoints/InvoiceEndpoint.php | 2 +- src/Endpoints/MandateEndpoint.php | 4 ++-- src/Endpoints/OrderEndpoint.php | 2 +- src/Endpoints/PaymentCaptureEndpoint.php | 4 ++-- src/Endpoints/PaymentChargebackEndpoint.php | 4 ++-- src/Endpoints/PaymentEndpoint.php | 2 +- src/Endpoints/PaymentLinkEndpoint.php | 2 +- src/Endpoints/PaymentRefundEndpoint.php | 4 ++-- src/Endpoints/ProfileEndpoint.php | 2 +- src/Endpoints/RefundEndpoint.php | 2 +- src/Endpoints/SettlementCaptureEndpoint.php | 2 +- src/Endpoints/SettlementChargebackEndpoint.php | 2 +- src/Endpoints/SettlementPaymentEndpoint.php | 2 +- src/Endpoints/SettlementRefundEndpoint.php | 2 +- src/Endpoints/SettlementsEndpoint.php | 2 +- src/Endpoints/SubscriptionEndpoint.php | 6 +++--- src/Endpoints/TerminalEndpoint.php | 2 +- 24 files changed, 35 insertions(+), 35 deletions(-) diff --git a/src/Endpoints/BalanceEndpoint.php b/src/Endpoints/BalanceEndpoint.php index a0582c8b..50dcbf42 100644 --- a/src/Endpoints/BalanceEndpoint.php +++ b/src/Endpoints/BalanceEndpoint.php @@ -87,7 +87,7 @@ public function page(?string $from = null, ?int $limit = null, array $parameters * @param string $from The first Balance ID you want to include in your list. * @param int $limit * @param array $parameters - * @param boolean $iterateBackwards Set to true for reverse order iteration (default is false). + * @param bool $iterateBackwards Set to true for reverse order iteration (default is false). * * @return Generator */ diff --git a/src/Endpoints/BalanceTransactionEndpoint.php b/src/Endpoints/BalanceTransactionEndpoint.php index 5dcec331..e0238007 100644 --- a/src/Endpoints/BalanceTransactionEndpoint.php +++ b/src/Endpoints/BalanceTransactionEndpoint.php @@ -56,7 +56,7 @@ public function listFor(Balance $balance, array $parameters = []) * * @param Balance $balance * @param array $parameters - * @param boolean $iterateBackwards Set to true for reverse order iteration (default is false). + * @param bool $iterateBackwards Set to true for reverse order iteration (default is false). * * @return Generator */ @@ -86,7 +86,7 @@ public function listForId(string $balanceId, array $parameters = []) * * @param string $balanceId * @param array $parameters - * @param boolean $iterateBackwards Set to true for reverse order iteration (default is false). + * @param bool $iterateBackwards Set to true for reverse order iteration (default is false). * * @return Generator */ @@ -116,7 +116,7 @@ public function listForPrimary(array $parameters = []) * Create an iterator for iterating over transactions for the primary balance retrieved from Mollie. * * @param array $parameters - * @param boolean $iterateBackwards Set to true for reverse order iteration (default is false). + * @param bool $iterateBackwards Set to true for reverse order iteration (default is false). * * @return Generator */ diff --git a/src/Endpoints/ChargebackEndpoint.php b/src/Endpoints/ChargebackEndpoint.php index 68efd372..d02d08ae 100644 --- a/src/Endpoints/ChargebackEndpoint.php +++ b/src/Endpoints/ChargebackEndpoint.php @@ -55,7 +55,7 @@ public function page(?string $from = null, ?int $limit = null, array $parameters * @param string $from The first chargevback ID you want to include in your list. * @param int $limit * @param array $parameters - * @param boolean $iterateBackwards Set to true for reverse order iteration (default is false). + * @param bool $iterateBackwards Set to true for reverse order iteration (default is false). * * @return Generator */ diff --git a/src/Endpoints/ClientEndpoint.php b/src/Endpoints/ClientEndpoint.php index 5e3efb94..cdad8b50 100644 --- a/src/Endpoints/ClientEndpoint.php +++ b/src/Endpoints/ClientEndpoint.php @@ -74,7 +74,7 @@ public function page(?string $from = null, ?int $limit = null, array $parameters * @param string $from The first client ID you want to include in your list. * @param int $limit * @param array $parameters - * @param boolean $iterateBackwards Set to true for reverse order iteration (default is false). + * @param bool $iterateBackwards Set to true for reverse order iteration (default is false). * * @return Generator */ diff --git a/src/Endpoints/CollectionEndpointAbstract.php b/src/Endpoints/CollectionEndpointAbstract.php index c36f54bc..81e05787 100644 --- a/src/Endpoints/CollectionEndpointAbstract.php +++ b/src/Endpoints/CollectionEndpointAbstract.php @@ -48,7 +48,7 @@ protected function rest_list(?string $from = null, ?int $limit = null, array $fi * @param string $from The first resource ID you want to include in your list. * @param int $limit * @param array $filters - * @param boolean $iterateBackwards Set to true for reverse order iteration (default is false). + * @param bool $iterateBackwards Set to true for reverse order iteration (default is false). * @return Generator */ protected function rest_iterator(?string $from = null, ?int $limit = null, array $filters = [], bool $iterateBackwards = false): Generator @@ -63,7 +63,7 @@ protected function rest_iterator(?string $from = null, ?int $limit = null, array * Iterate over a CursorCollection and yield its elements. * * @param CursorCollection $page - * @param boolean $iterateBackwards + * @param bool $iterateBackwards * * @return Generator */ @@ -74,7 +74,7 @@ protected function iterate(CursorCollection $page, bool $iterateBackwards = fals yield $item; } - if (($iterateBackwards && !$page->hasPrevious()) || !$page->hasNext()) { + if (($iterateBackwards && ! $page->hasPrevious()) || ! $page->hasNext()) { break; } diff --git a/src/Endpoints/CustomerEndpoint.php b/src/Endpoints/CustomerEndpoint.php index 5e48a1b4..603ba1eb 100644 --- a/src/Endpoints/CustomerEndpoint.php +++ b/src/Endpoints/CustomerEndpoint.php @@ -126,7 +126,7 @@ public function page(?string $from = null, ?int $limit = null, array $parameters * @param string $from The first customer ID you want to include in your list. * @param int $limit * @param array $parameters - * @param boolean $iterateBackwards Set to true for reverse order iteration (default is false). + * @param bool $iterateBackwards Set to true for reverse order iteration (default is false). * * @return Generator */ diff --git a/src/Endpoints/CustomerPaymentsEndpoint.php b/src/Endpoints/CustomerPaymentsEndpoint.php index ea231b88..0738dd65 100644 --- a/src/Endpoints/CustomerPaymentsEndpoint.php +++ b/src/Endpoints/CustomerPaymentsEndpoint.php @@ -87,7 +87,7 @@ public function listFor(Customer $customer, ?string $from = null, ?int $limit = * @param string $from The first resource ID you want to include in your list. * @param int $limit * @param array $parameters - * @param boolean $iterateBackwards Set to true for reverse order iteration (default is false). + * @param bool $iterateBackwards Set to true for reverse order iteration (default is false). * * @return Generator */ @@ -119,7 +119,7 @@ public function listForId($customerId, ?string $from = null, ?int $limit = null, * @param string $from The first resource ID you want to include in your list. * @param int $limit * @param array $parameters - * @param boolean $iterateBackwards Set to true for reverse order iteration (default is false). + * @param bool $iterateBackwards Set to true for reverse order iteration (default is false). * * @return Generator */ diff --git a/src/Endpoints/InvoiceEndpoint.php b/src/Endpoints/InvoiceEndpoint.php index afd7750d..3f5af151 100644 --- a/src/Endpoints/InvoiceEndpoint.php +++ b/src/Endpoints/InvoiceEndpoint.php @@ -84,7 +84,7 @@ public function all(array $parameters = []) * @param string $from The first resource ID you want to include in your list. * @param int $limit * @param array $parameters - * @param boolean $iterateBackwards Set to true for reverse order iteration (default is false). + * @param bool $iterateBackwards Set to true for reverse order iteration (default is false). * * @return Generator */ diff --git a/src/Endpoints/MandateEndpoint.php b/src/Endpoints/MandateEndpoint.php index 160f4b74..75302515 100644 --- a/src/Endpoints/MandateEndpoint.php +++ b/src/Endpoints/MandateEndpoint.php @@ -111,7 +111,7 @@ public function listFor(Customer $customer, $from = null, $limit = null, array $ * @param string $from The first resource ID you want to include in your list. * @param int $limit * @param array $parameters - * @param boolean $iterateBackwards Set to true for reverse order iteration (default is false). + * @param bool $iterateBackwards Set to true for reverse order iteration (default is false). * * @return Generator */ @@ -143,7 +143,7 @@ public function listForId($customerId, $from = null, $limit = null, array $param * @param string $from The first resource ID you want to include in your list. * @param int $limit * @param array $parameters - * @param boolean $iterateBackwards Set to true for reverse order iteration (default is false). + * @param bool $iterateBackwards Set to true for reverse order iteration (default is false). * * @return Generator */ diff --git a/src/Endpoints/OrderEndpoint.php b/src/Endpoints/OrderEndpoint.php index 102b44ea..ee1eaaee 100644 --- a/src/Endpoints/OrderEndpoint.php +++ b/src/Endpoints/OrderEndpoint.php @@ -135,7 +135,7 @@ public function page(?string $from = null, ?int $limit = null, array $parameters * @param string $from The first order ID you want to include in your list. * @param int $limit * @param array $parameters - * @param boolean $iterateBackwards Set to true for reverse order iteration (default is false). + * @param bool $iterateBackwards Set to true for reverse order iteration (default is false). * * @return Generator */ diff --git a/src/Endpoints/PaymentCaptureEndpoint.php b/src/Endpoints/PaymentCaptureEndpoint.php index d7bd6f4e..1671f095 100644 --- a/src/Endpoints/PaymentCaptureEndpoint.php +++ b/src/Endpoints/PaymentCaptureEndpoint.php @@ -113,7 +113,7 @@ public function listFor(Payment $payment, array $parameters = []) * @param string $from The first resource ID you want to include in your list. * @param int $limit * @param array $parameters - * @param boolean $iterateBackwards Set to true for reverse order iteration (default is false). + * @param bool $iterateBackwards Set to true for reverse order iteration (default is false). * * @return Generator */ @@ -143,7 +143,7 @@ public function listForId($paymentId, array $parameters = []) * @param string $from The first resource ID you want to include in your list. * @param int $limit * @param array $parameters - * @param boolean $iterateBackwards Set to true for reverse order iteration (default is false). + * @param bool $iterateBackwards Set to true for reverse order iteration (default is false). * * @return Generator */ diff --git a/src/Endpoints/PaymentChargebackEndpoint.php b/src/Endpoints/PaymentChargebackEndpoint.php index e2f1d33a..42da9b03 100644 --- a/src/Endpoints/PaymentChargebackEndpoint.php +++ b/src/Endpoints/PaymentChargebackEndpoint.php @@ -81,7 +81,7 @@ public function listFor(Payment $payment, array $parameters = []) * @param string $from The first resource ID you want to include in your list. * @param int $limit * @param array $parameters - * @param boolean $iterateBackwards Set to true for reverse order iteration (default is false). + * @param bool $iterateBackwards Set to true for reverse order iteration (default is false). * * @return Generator */ @@ -111,7 +111,7 @@ public function listForId($paymentId, array $parameters = []) * @param string $from The first resource ID you want to include in your list. * @param int $limit * @param array $parameters - * @param boolean $iterateBackwards Set to true for reverse order iteration (default is false). + * @param bool $iterateBackwards Set to true for reverse order iteration (default is false). * * @return Generator */ diff --git a/src/Endpoints/PaymentEndpoint.php b/src/Endpoints/PaymentEndpoint.php index a68032cc..10ad9d34 100644 --- a/src/Endpoints/PaymentEndpoint.php +++ b/src/Endpoints/PaymentEndpoint.php @@ -147,7 +147,7 @@ public function page($from = null, $limit = null, array $parameters = []) * @param string $from The first resource ID you want to include in your list. * @param int $limit * @param array $parameters - * @param boolean $iterateBackwards Set to true for reverse order iteration (default is false). + * @param bool $iterateBackwards Set to true for reverse order iteration (default is false). * * @return Generator */ diff --git a/src/Endpoints/PaymentLinkEndpoint.php b/src/Endpoints/PaymentLinkEndpoint.php index 6e98c053..22adf5cd 100644 --- a/src/Endpoints/PaymentLinkEndpoint.php +++ b/src/Endpoints/PaymentLinkEndpoint.php @@ -92,7 +92,7 @@ public function page($from = null, $limit = null, array $parameters = []) * @param string $from The first resource ID you want to include in your list. * @param int $limit * @param array $parameters - * @param boolean $iterateBackwards Set to true for reverse order iteration (default is false). + * @param bool $iterateBackwards Set to true for reverse order iteration (default is false). * * @return Generator */ diff --git a/src/Endpoints/PaymentRefundEndpoint.php b/src/Endpoints/PaymentRefundEndpoint.php index 28c2ee36..9ea4a168 100644 --- a/src/Endpoints/PaymentRefundEndpoint.php +++ b/src/Endpoints/PaymentRefundEndpoint.php @@ -81,7 +81,7 @@ public function listFor(Payment $payment, array $parameters = []) * @param string $from The first resource ID you want to include in your list. * @param int $limit * @param array $parameters - * @param boolean $iterateBackwards Set to true for reverse order iteration (default is false). + * @param bool $iterateBackwards Set to true for reverse order iteration (default is false). * * @return Generator */ @@ -111,7 +111,7 @@ public function listForId($paymentId, array $parameters = []) * @param string $from The first resource ID you want to include in your list. * @param int $limit * @param array $parameters - * @param boolean $iterateBackwards Set to true for reverse order iteration (default is false). + * @param bool $iterateBackwards Set to true for reverse order iteration (default is false). * * @return Generator */ diff --git a/src/Endpoints/ProfileEndpoint.php b/src/Endpoints/ProfileEndpoint.php index 07647743..45d382df 100644 --- a/src/Endpoints/ProfileEndpoint.php +++ b/src/Endpoints/ProfileEndpoint.php @@ -148,7 +148,7 @@ public function page($from = null, $limit = null, array $parameters = []) * @param string $from The first resource ID you want to include in your list. * @param int $limit * @param array $parameters - * @param boolean $iterateBackwards Set to true for reverse order iteration (default is false). + * @param bool $iterateBackwards Set to true for reverse order iteration (default is false). * * @return Generator */ diff --git a/src/Endpoints/RefundEndpoint.php b/src/Endpoints/RefundEndpoint.php index 616f15af..720d14c5 100644 --- a/src/Endpoints/RefundEndpoint.php +++ b/src/Endpoints/RefundEndpoint.php @@ -55,7 +55,7 @@ public function page($from = null, $limit = null, array $parameters = []) * @param string $from The first resource ID you want to include in your list. * @param int $limit * @param array $parameters - * @param boolean $iterateBackwards Set to true for reverse order iteration (default is false). + * @param bool $iterateBackwards Set to true for reverse order iteration (default is false). * * @return Generator */ diff --git a/src/Endpoints/SettlementCaptureEndpoint.php b/src/Endpoints/SettlementCaptureEndpoint.php index 4c08136a..03786563 100644 --- a/src/Endpoints/SettlementCaptureEndpoint.php +++ b/src/Endpoints/SettlementCaptureEndpoint.php @@ -50,7 +50,7 @@ public function pageForId(string $settlementId, string $from = null, int $limit * @param string $from The first resource ID you want to include in your list. * @param int $limit * @param array $parameters - * @param boolean $iterateBackwards Set to true for reverse order iteration (default is false). + * @param bool $iterateBackwards Set to true for reverse order iteration (default is false). * * @return Generator */ diff --git a/src/Endpoints/SettlementChargebackEndpoint.php b/src/Endpoints/SettlementChargebackEndpoint.php index 2992c322..6dc5a735 100644 --- a/src/Endpoints/SettlementChargebackEndpoint.php +++ b/src/Endpoints/SettlementChargebackEndpoint.php @@ -53,7 +53,7 @@ public function pageForId(string $settlementId, string $from = null, int $limit * @param string $from The first resource ID you want to include in your list. * @param int $limit * @param array $parameters - * @param boolean $iterateBackwards Set to true for reverse order iteration (default is false). + * @param bool $iterateBackwards Set to true for reverse order iteration (default is false). * * @return Generator */ diff --git a/src/Endpoints/SettlementPaymentEndpoint.php b/src/Endpoints/SettlementPaymentEndpoint.php index 9766bd1d..5dc6b4e4 100644 --- a/src/Endpoints/SettlementPaymentEndpoint.php +++ b/src/Endpoints/SettlementPaymentEndpoint.php @@ -51,7 +51,7 @@ public function pageForId($settlementId, $from = null, $limit = null, array $par * @param string $from The first resource ID you want to include in your list. * @param int $limit * @param array $parameters - * @param boolean $iterateBackwards Set to true for reverse order iteration (default is false). + * @param bool $iterateBackwards Set to true for reverse order iteration (default is false). * * @return Generator */ diff --git a/src/Endpoints/SettlementRefundEndpoint.php b/src/Endpoints/SettlementRefundEndpoint.php index 4984fe23..dc11f782 100644 --- a/src/Endpoints/SettlementRefundEndpoint.php +++ b/src/Endpoints/SettlementRefundEndpoint.php @@ -53,7 +53,7 @@ public function pageForId(string $settlementId, string $from = null, int $limit * @param string $from The first resource ID you want to include in your list. * @param int $limit * @param array $parameters - * @param boolean $iterateBackwards Set to true for reverse order iteration (default is false). + * @param bool $iterateBackwards Set to true for reverse order iteration (default is false). * * @return Generator */ diff --git a/src/Endpoints/SettlementsEndpoint.php b/src/Endpoints/SettlementsEndpoint.php index 6fc4c570..2de54067 100644 --- a/src/Endpoints/SettlementsEndpoint.php +++ b/src/Endpoints/SettlementsEndpoint.php @@ -92,7 +92,7 @@ public function page($from = null, $limit = null, array $parameters = []) * @param string $from The first resource ID you want to include in your list. * @param int $limit * @param array $parameters - * @param boolean $iterateBackwards Set to true for reverse order iteration (default is false). + * @param bool $iterateBackwards Set to true for reverse order iteration (default is false). * * @return Generator */ diff --git a/src/Endpoints/SubscriptionEndpoint.php b/src/Endpoints/SubscriptionEndpoint.php index b0975049..690992fb 100644 --- a/src/Endpoints/SubscriptionEndpoint.php +++ b/src/Endpoints/SubscriptionEndpoint.php @@ -146,7 +146,7 @@ public function listFor(Customer $customer, $from = null, $limit = null, array $ * @param string $from The first resource ID you want to include in your list. * @param int $limit * @param array $parameters - * @param boolean $iterateBackwards Set to true for reverse order iteration (default is false). + * @param bool $iterateBackwards Set to true for reverse order iteration (default is false). * * @return Generator */ @@ -178,7 +178,7 @@ public function listForId($customerId, $from = null, $limit = null, array $param * @param string $from The first resource ID you want to include in your list. * @param int $limit * @param array $parameters - * @param boolean $iterateBackwards Set to true for reverse order iteration (default is false). + * @param bool $iterateBackwards Set to true for reverse order iteration (default is false). * * @return Generator */ @@ -251,7 +251,7 @@ public function page($from = null, $limit = null, array $parameters = []) * @param string $from The first resource ID you want to include in your list. * @param int $limit * @param array $parameters - * @param boolean $iterateBackwards Set to true for reverse order iteration (default is false). + * @param bool $iterateBackwards Set to true for reverse order iteration (default is false). * * @return Generator */ diff --git a/src/Endpoints/TerminalEndpoint.php b/src/Endpoints/TerminalEndpoint.php index 9b1d90a3..dbd9ff12 100644 --- a/src/Endpoints/TerminalEndpoint.php +++ b/src/Endpoints/TerminalEndpoint.php @@ -77,7 +77,7 @@ public function page($from = null, $limit = null, array $parameters = []) * @param string $from The first resource ID you want to include in your list. * @param int $limit * @param array $parameters - * @param boolean $iterateBackwards Set to true for reverse order iteration (default is false). + * @param bool $iterateBackwards Set to true for reverse order iteration (default is false). * * @return Generator */ From eafd22e004fad1e239e9edafe488f33d94e06b96 Mon Sep 17 00:00:00 2001 From: Krishan Koenig Date: Thu, 26 Oct 2023 12:20:26 +0200 Subject: [PATCH 05/26] wip --- src/Endpoints/CollectionEndpointAbstract.php | 27 +---------------- src/Endpoints/SubscriptionEndpoint.php | 2 +- src/Resources/CursorCollection.php | 31 ++++++++++++++++++-- 3 files changed, 31 insertions(+), 29 deletions(-) diff --git a/src/Endpoints/CollectionEndpointAbstract.php b/src/Endpoints/CollectionEndpointAbstract.php index 81e05787..65987623 100644 --- a/src/Endpoints/CollectionEndpointAbstract.php +++ b/src/Endpoints/CollectionEndpointAbstract.php @@ -56,32 +56,7 @@ protected function rest_iterator(?string $from = null, ?int $limit = null, array /** @var CursorCollection $page */ $page = $this->rest_list($from, $limit, $filters); - return $this->iterate($page, $iterateBackwards); - } - - /** - * Iterate over a CursorCollection and yield its elements. - * - * @param CursorCollection $page - * @param bool $iterateBackwards - * - * @return Generator - */ - protected function iterate(CursorCollection $page, bool $iterateBackwards = false): Generator - { - while (true) { - foreach ($page as $item) { - yield $item; - } - - if (($iterateBackwards && ! $page->hasPrevious()) || ! $page->hasNext()) { - break; - } - - $page = $iterateBackwards - ? $page->previous() - : $page->next(); - } + return $page->getAutoIterator($iterateBackwards); } /** diff --git a/src/Endpoints/SubscriptionEndpoint.php b/src/Endpoints/SubscriptionEndpoint.php index 690992fb..06930027 100644 --- a/src/Endpoints/SubscriptionEndpoint.php +++ b/src/Endpoints/SubscriptionEndpoint.php @@ -259,6 +259,6 @@ public function iterator(?string $from = null, ?int $limit = null, array $parame { $page = $this->page($from, $limit, $parameters); - return $this->iterate($page, $iterateBackwards); + return $page->getAutoIterator($iterateBackwards); } } diff --git a/src/Resources/CursorCollection.php b/src/Resources/CursorCollection.php index e3265335..36e4d40d 100644 --- a/src/Resources/CursorCollection.php +++ b/src/Resources/CursorCollection.php @@ -2,6 +2,7 @@ namespace Mollie\Api\Resources; +use Generator; use Mollie\Api\MollieApiClient; abstract class CursorCollection extends BaseCollection @@ -36,7 +37,7 @@ abstract protected function createResourceObject(); */ final public function next() { - if (! $this->hasNext()) { + if (!$this->hasNext()) { return null; } @@ -59,7 +60,7 @@ final public function next() */ final public function previous() { - if (! $this->hasPrevious()) { + if (!$this->hasPrevious()) { return null; } @@ -93,4 +94,30 @@ public function hasPrevious() { return isset($this->_links->previous->href); } + + /** + * Iterate over a CursorCollection and yield its elements. + * + * @param boolean $iterateBackwards + * + * @return Generator + */ + public function getAutoIterator(bool $iterateBackwards = false): Generator + { + $page = $this; + + while (true) { + foreach ($page as $item) { + yield $item; + } + + if (($iterateBackwards && !$page->hasPrevious()) || !$page->hasNext()) { + break; + } + + $page = $iterateBackwards + ? $page->previous() + : $page->next(); + } + } } From f3322f3788e4008540211bcc7eb25f8479e8e715 Mon Sep 17 00:00:00 2001 From: Naoray Date: Thu, 26 Oct 2023 10:20:54 +0000 Subject: [PATCH 06/26] Fix styling --- src/Resources/CursorCollection.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Resources/CursorCollection.php b/src/Resources/CursorCollection.php index 36e4d40d..2a20e54e 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; } @@ -98,7 +98,7 @@ public function hasPrevious() /** * Iterate over a CursorCollection and yield its elements. * - * @param boolean $iterateBackwards + * @param bool $iterateBackwards * * @return Generator */ @@ -111,7 +111,7 @@ public function getAutoIterator(bool $iterateBackwards = false): Generator yield $item; } - if (($iterateBackwards && !$page->hasPrevious()) || !$page->hasNext()) { + if (($iterateBackwards && ! $page->hasPrevious()) || ! $page->hasNext()) { break; } From 396230a0a51c7a1ab832c2085c70db3e21216761 Mon Sep 17 00:00:00 2001 From: Krishan Koenig Date: Fri, 27 Oct 2023 09:59:35 +0200 Subject: [PATCH 07/26] add pagination examples --- examples/pagination/backwards.php | 57 +++++++++++++++++++++++++++++ examples/pagination/basic_usage.php | 56 ++++++++++++++++++++++++++++ 2 files changed, 113 insertions(+) create mode 100644 examples/pagination/backwards.php create mode 100644 examples/pagination/basic_usage.php diff --git a/examples/pagination/backwards.php b/examples/pagination/backwards.php new file mode 100644 index 00000000..910ced84 --- /dev/null +++ b/examples/pagination/backwards.php @@ -0,0 +1,57 @@ +orders->page($orderId); + + while ($page->hasPrevious()) { + foreach ($page as $order) { + echo ($order->id); + } + + $page = $page->previous(); + } + + // iterating backwards using the iterator by passing iterateBackwards = true + // in php 8.0+ you could also use the named parameter syntax iterator(iterateBackwards: true) + foreach ($client->orders->iterator(null, null, [], true) as $order) { + echo ($order->id); + } +} catch (\Mollie\Api\Exceptions\ApiException $e) { + echo "API call failed: " . htmlspecialchars($e->getMessage()); +} diff --git a/examples/pagination/basic_usage.php b/examples/pagination/basic_usage.php new file mode 100644 index 00000000..26194844 --- /dev/null +++ b/examples/pagination/basic_usage.php @@ -0,0 +1,56 @@ +orders->page(); + + while ($page->hasNext()) { + foreach ($page as $order) { + echo ($order->id); + } + + $page = $page->next(); + } + + + // using the iterator we can iterate over all orders directly + foreach ($client->orders->iterator() as $order) { + echo ($order->id); + } +} catch (\Mollie\Api\Exceptions\ApiException $e) { + echo "API call failed: " . htmlspecialchars($e->getMessage()); +} From bc69d2f77a0b76f5b6c91ee99bbd8927ed3f74e3 Mon Sep 17 00:00:00 2001 From: Naoray Date: Fri, 27 Oct 2023 07:59:59 +0000 Subject: [PATCH 08/26] Fix styling --- examples/pagination/backwards.php | 4 ++-- examples/pagination/basic_usage.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/pagination/backwards.php b/examples/pagination/backwards.php index 910ced84..5eeb6b53 100644 --- a/examples/pagination/backwards.php +++ b/examples/pagination/backwards.php @@ -41,7 +41,7 @@ while ($page->hasPrevious()) { foreach ($page as $order) { - echo ($order->id); + echo($order->id); } $page = $page->previous(); @@ -50,7 +50,7 @@ // iterating backwards using the iterator by passing iterateBackwards = true // in php 8.0+ you could also use the named parameter syntax iterator(iterateBackwards: true) foreach ($client->orders->iterator(null, null, [], true) as $order) { - echo ($order->id); + echo($order->id); } } catch (\Mollie\Api\Exceptions\ApiException $e) { echo "API call failed: " . htmlspecialchars($e->getMessage()); diff --git a/examples/pagination/basic_usage.php b/examples/pagination/basic_usage.php index 26194844..38f4d53f 100644 --- a/examples/pagination/basic_usage.php +++ b/examples/pagination/basic_usage.php @@ -40,7 +40,7 @@ while ($page->hasNext()) { foreach ($page as $order) { - echo ($order->id); + echo($order->id); } $page = $page->next(); @@ -49,7 +49,7 @@ // using the iterator we can iterate over all orders directly foreach ($client->orders->iterator() as $order) { - echo ($order->id); + echo($order->id); } } catch (\Mollie\Api\Exceptions\ApiException $e) { echo "API call failed: " . htmlspecialchars($e->getMessage()); From 01b313f5ea8aaf6db5b94f37c4c857ec8cb02d5e Mon Sep 17 00:00:00 2001 From: Krishan Koenig Date: Fri, 27 Oct 2023 10:16:46 +0200 Subject: [PATCH 09/26] fix example --- examples/pagination/backwards.php | 8 ++++---- examples/pagination/basic_usage.php | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/examples/pagination/backwards.php b/examples/pagination/backwards.php index 5eeb6b53..d14823dc 100644 --- a/examples/pagination/backwards.php +++ b/examples/pagination/backwards.php @@ -37,11 +37,11 @@ $orderId = 'ord_8wmqcHMN4U'; // cursor paginating backwards through all orders - $page = $client->orders->page($orderId); + $page = $mollie->orders->page($orderId); while ($page->hasPrevious()) { foreach ($page as $order) { - echo($order->id); + echo ($order->id); } $page = $page->previous(); @@ -49,8 +49,8 @@ // iterating backwards using the iterator by passing iterateBackwards = true // in php 8.0+ you could also use the named parameter syntax iterator(iterateBackwards: true) - foreach ($client->orders->iterator(null, null, [], true) as $order) { - echo($order->id); + foreach ($mollie->orders->iterator(null, null, [], true) as $order) { + echo ($order->id); } } catch (\Mollie\Api\Exceptions\ApiException $e) { echo "API call failed: " . htmlspecialchars($e->getMessage()); diff --git a/examples/pagination/basic_usage.php b/examples/pagination/basic_usage.php index 38f4d53f..658da245 100644 --- a/examples/pagination/basic_usage.php +++ b/examples/pagination/basic_usage.php @@ -36,11 +36,11 @@ // cursor paginating through all orders - $page = $client->orders->page(); + $page = $mollie->orders->page(); while ($page->hasNext()) { foreach ($page as $order) { - echo($order->id); + echo ($order->id); } $page = $page->next(); @@ -48,8 +48,8 @@ // using the iterator we can iterate over all orders directly - foreach ($client->orders->iterator() as $order) { - echo($order->id); + foreach ($mollie->orders->iterator() as $order) { + echo ($order->id); } } catch (\Mollie\Api\Exceptions\ApiException $e) { echo "API call failed: " . htmlspecialchars($e->getMessage()); From 1e54ac4806a003a6ffe6ea6dae24fa9aae60fb2f Mon Sep 17 00:00:00 2001 From: Naoray Date: Fri, 27 Oct 2023 08:17:10 +0000 Subject: [PATCH 10/26] Fix styling --- examples/pagination/backwards.php | 4 ++-- examples/pagination/basic_usage.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/pagination/backwards.php b/examples/pagination/backwards.php index d14823dc..35aa6363 100644 --- a/examples/pagination/backwards.php +++ b/examples/pagination/backwards.php @@ -41,7 +41,7 @@ while ($page->hasPrevious()) { foreach ($page as $order) { - echo ($order->id); + echo($order->id); } $page = $page->previous(); @@ -50,7 +50,7 @@ // iterating backwards using the iterator by passing iterateBackwards = true // in php 8.0+ you could also use the named parameter syntax iterator(iterateBackwards: true) foreach ($mollie->orders->iterator(null, null, [], true) as $order) { - echo ($order->id); + echo($order->id); } } catch (\Mollie\Api\Exceptions\ApiException $e) { echo "API call failed: " . htmlspecialchars($e->getMessage()); diff --git a/examples/pagination/basic_usage.php b/examples/pagination/basic_usage.php index 658da245..3e927ca8 100644 --- a/examples/pagination/basic_usage.php +++ b/examples/pagination/basic_usage.php @@ -40,7 +40,7 @@ while ($page->hasNext()) { foreach ($page as $order) { - echo ($order->id); + echo($order->id); } $page = $page->next(); @@ -49,7 +49,7 @@ // using the iterator we can iterate over all orders directly foreach ($mollie->orders->iterator() as $order) { - echo ($order->id); + echo($order->id); } } catch (\Mollie\Api\Exceptions\ApiException $e) { echo "API call failed: " . htmlspecialchars($e->getMessage()); From 68c204e1de22532e11daef813c4b4346ab550e96 Mon Sep 17 00:00:00 2001 From: Krishan Koenig Date: Mon, 30 Oct 2023 11:03:01 +0100 Subject: [PATCH 11/26] add CursorCollectionTest --- .../API/Resources/CursorCollectionTest.php | 112 ++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 tests/Mollie/API/Resources/CursorCollectionTest.php diff --git a/tests/Mollie/API/Resources/CursorCollectionTest.php b/tests/Mollie/API/Resources/CursorCollectionTest.php new file mode 100644 index 00000000..2eec4c3c --- /dev/null +++ b/tests/Mollie/API/Resources/CursorCollectionTest.php @@ -0,0 +1,112 @@ +createMock(MollieApiClient::class); + $mockedClient->expects($this->once()) + ->method('performHttpCallToFullUrl') + ->willReturn((object) [ + 'count' => 1, + '_links' => (object) [ + 'self' => [ + 'href' => 'https://api.mollie.com/v2/orders?from=ord_stTC2WHAuS', + ], + ], + '_embedded' => (object) [ + 'orders' => [ + (object) ['id' => 'ord_stTC2WHAuS'], + ], + ], + ]); + + $collection = new OrderCollection( + $mockedClient, + 1, + (object) [ + 'next' => (object) [ + '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((object) [ + 'count' => 1, + '_links' => (object) [ + 'self' => [ + 'href' => 'https://api.mollie.com/v2/orders?from=ord_stTC2WHAuS', + ], + ], + '_embedded' => (object) [ + 'orders' => [ + (object) ['id' => 'ord_stTC2WHAuS'], + ], + ], + ]); + + $collection = new OrderCollection( + $mockedClient, + 1, + (object) [ + 'previous' => (object) [ + '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()); + } +} From 1ddf8a5b234eeb70f7d1584720ffdee8d12ae66d Mon Sep 17 00:00:00 2001 From: Krishan Koenig Date: Mon, 30 Oct 2023 12:53:02 +0100 Subject: [PATCH 12/26] 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; + })); + } +} From 90ad2554588d1117e9fbc6d57edcd4752a117615 Mon Sep 17 00:00:00 2001 From: Naoray Date: Mon, 30 Oct 2023 11:53:23 +0000 Subject: [PATCH 13/26] Fix styling --- src/Endpoints/BalanceEndpoint.php | 2 +- src/Endpoints/BalanceTransactionEndpoint.php | 2 +- src/Endpoints/ChargebackEndpoint.php | 2 +- src/Endpoints/ClientEndpoint.php | 2 +- src/Endpoints/CollectionEndpointAbstract.php | 2 +- src/Endpoints/CustomerEndpoint.php | 2 +- src/Endpoints/CustomerPaymentsEndpoint.php | 2 +- src/Endpoints/InvoiceEndpoint.php | 2 +- src/Endpoints/MandateEndpoint.php | 2 +- src/Endpoints/OrderEndpoint.php | 2 +- src/Endpoints/PaymentCaptureEndpoint.php | 2 +- src/Endpoints/PaymentChargebackEndpoint.php | 2 +- src/Endpoints/PaymentEndpoint.php | 2 +- src/Endpoints/PaymentLinkEndpoint.php | 2 +- src/Endpoints/ProfileEndpoint.php | 2 +- src/Endpoints/RefundEndpoint.php | 2 +- src/Endpoints/SettlementCaptureEndpoint.php | 2 +- src/Endpoints/SettlementChargebackEndpoint.php | 2 +- src/Endpoints/SettlementsEndpoint.php | 2 +- src/Endpoints/TerminalEndpoint.php | 2 +- src/Resources/CursorCollection.php | 6 +++--- src/Resources/LazyCollection.php | 6 +++--- 22 files changed, 26 insertions(+), 26 deletions(-) diff --git a/src/Endpoints/BalanceEndpoint.php b/src/Endpoints/BalanceEndpoint.php index ec2bdea4..5f44e41a 100644 --- a/src/Endpoints/BalanceEndpoint.php +++ b/src/Endpoints/BalanceEndpoint.php @@ -2,11 +2,11 @@ namespace Mollie\Api\Endpoints; -use Mollie\Api\Resources\LazyCollection; 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 { diff --git a/src/Endpoints/BalanceTransactionEndpoint.php b/src/Endpoints/BalanceTransactionEndpoint.php index 4b4c6105..4f244713 100644 --- a/src/Endpoints/BalanceTransactionEndpoint.php +++ b/src/Endpoints/BalanceTransactionEndpoint.php @@ -4,10 +4,10 @@ namespace Mollie\Api\Endpoints; -use Mollie\Api\Resources\LazyCollection; use Mollie\Api\Resources\Balance; use Mollie\Api\Resources\BalanceTransaction; use Mollie\Api\Resources\BalanceTransactionCollection; +use Mollie\Api\Resources\LazyCollection; class BalanceTransactionEndpoint extends CollectionEndpointAbstract { diff --git a/src/Endpoints/ChargebackEndpoint.php b/src/Endpoints/ChargebackEndpoint.php index 63480ec2..c40c179c 100644 --- a/src/Endpoints/ChargebackEndpoint.php +++ b/src/Endpoints/ChargebackEndpoint.php @@ -2,10 +2,10 @@ namespace Mollie\Api\Endpoints; -use Mollie\Api\Resources\LazyCollection; use Mollie\Api\Exceptions\ApiException; use Mollie\Api\Resources\Chargeback; use Mollie\Api\Resources\ChargebackCollection; +use Mollie\Api\Resources\LazyCollection; class ChargebackEndpoint extends CollectionEndpointAbstract { diff --git a/src/Endpoints/ClientEndpoint.php b/src/Endpoints/ClientEndpoint.php index ab4cc976..9d7ba66b 100644 --- a/src/Endpoints/ClientEndpoint.php +++ b/src/Endpoints/ClientEndpoint.php @@ -2,10 +2,10 @@ namespace Mollie\Api\Endpoints; -use Mollie\Api\Resources\LazyCollection; use Mollie\Api\Exceptions\ApiException; use Mollie\Api\Resources\Client; use Mollie\Api\Resources\ClientCollection; +use Mollie\Api\Resources\LazyCollection; class ClientEndpoint extends CollectionEndpointAbstract { diff --git a/src/Endpoints/CollectionEndpointAbstract.php b/src/Endpoints/CollectionEndpointAbstract.php index 6fda8b4a..06b1dec2 100644 --- a/src/Endpoints/CollectionEndpointAbstract.php +++ b/src/Endpoints/CollectionEndpointAbstract.php @@ -2,10 +2,10 @@ namespace Mollie\Api\Endpoints; -use Mollie\Api\Resources\LazyCollection; 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 diff --git a/src/Endpoints/CustomerEndpoint.php b/src/Endpoints/CustomerEndpoint.php index 4f268451..61c8e689 100644 --- a/src/Endpoints/CustomerEndpoint.php +++ b/src/Endpoints/CustomerEndpoint.php @@ -2,10 +2,10 @@ namespace Mollie\Api\Endpoints; -use Mollie\Api\Resources\LazyCollection; use Mollie\Api\Exceptions\ApiException; use Mollie\Api\Resources\Customer; use Mollie\Api\Resources\CustomerCollection; +use Mollie\Api\Resources\LazyCollection; class CustomerEndpoint extends CollectionEndpointAbstract { diff --git a/src/Endpoints/CustomerPaymentsEndpoint.php b/src/Endpoints/CustomerPaymentsEndpoint.php index f9e8ab47..1730cb24 100644 --- a/src/Endpoints/CustomerPaymentsEndpoint.php +++ b/src/Endpoints/CustomerPaymentsEndpoint.php @@ -2,8 +2,8 @@ namespace Mollie\Api\Endpoints; -use Mollie\Api\Resources\LazyCollection; use Mollie\Api\Resources\Customer; +use Mollie\Api\Resources\LazyCollection; use Mollie\Api\Resources\Payment; use Mollie\Api\Resources\PaymentCollection; diff --git a/src/Endpoints/InvoiceEndpoint.php b/src/Endpoints/InvoiceEndpoint.php index 66208916..e577abd9 100644 --- a/src/Endpoints/InvoiceEndpoint.php +++ b/src/Endpoints/InvoiceEndpoint.php @@ -2,10 +2,10 @@ namespace Mollie\Api\Endpoints; -use Mollie\Api\Resources\LazyCollection; use Mollie\Api\Exceptions\ApiException; use Mollie\Api\Resources\Invoice; use Mollie\Api\Resources\InvoiceCollection; +use Mollie\Api\Resources\LazyCollection; class InvoiceEndpoint extends CollectionEndpointAbstract { diff --git a/src/Endpoints/MandateEndpoint.php b/src/Endpoints/MandateEndpoint.php index 9884cd43..d6dee89f 100644 --- a/src/Endpoints/MandateEndpoint.php +++ b/src/Endpoints/MandateEndpoint.php @@ -2,8 +2,8 @@ namespace Mollie\Api\Endpoints; -use Mollie\Api\Resources\LazyCollection; use Mollie\Api\Resources\Customer; +use Mollie\Api\Resources\LazyCollection; use Mollie\Api\Resources\Mandate; use Mollie\Api\Resources\MandateCollection; diff --git a/src/Endpoints/OrderEndpoint.php b/src/Endpoints/OrderEndpoint.php index df637b4e..91f7bc34 100644 --- a/src/Endpoints/OrderEndpoint.php +++ b/src/Endpoints/OrderEndpoint.php @@ -2,8 +2,8 @@ namespace Mollie\Api\Endpoints; -use Mollie\Api\Resources\LazyCollection; use Mollie\Api\Exceptions\ApiException; +use Mollie\Api\Resources\LazyCollection; use Mollie\Api\Resources\Order; use Mollie\Api\Resources\OrderCollection; diff --git a/src/Endpoints/PaymentCaptureEndpoint.php b/src/Endpoints/PaymentCaptureEndpoint.php index 8f2aef61..61b2a6d2 100644 --- a/src/Endpoints/PaymentCaptureEndpoint.php +++ b/src/Endpoints/PaymentCaptureEndpoint.php @@ -2,9 +2,9 @@ namespace Mollie\Api\Endpoints; -use Mollie\Api\Resources\LazyCollection; use Mollie\Api\Resources\Capture; use Mollie\Api\Resources\CaptureCollection; +use Mollie\Api\Resources\LazyCollection; use Mollie\Api\Resources\Payment; class PaymentCaptureEndpoint extends CollectionEndpointAbstract diff --git a/src/Endpoints/PaymentChargebackEndpoint.php b/src/Endpoints/PaymentChargebackEndpoint.php index 347a62c0..5a232f88 100644 --- a/src/Endpoints/PaymentChargebackEndpoint.php +++ b/src/Endpoints/PaymentChargebackEndpoint.php @@ -2,9 +2,9 @@ namespace Mollie\Api\Endpoints; -use Mollie\Api\Resources\LazyCollection; use Mollie\Api\Resources\Chargeback; use Mollie\Api\Resources\ChargebackCollection; +use Mollie\Api\Resources\LazyCollection; use Mollie\Api\Resources\Payment; class PaymentChargebackEndpoint extends CollectionEndpointAbstract diff --git a/src/Endpoints/PaymentEndpoint.php b/src/Endpoints/PaymentEndpoint.php index 15080e7e..79b1c6b6 100644 --- a/src/Endpoints/PaymentEndpoint.php +++ b/src/Endpoints/PaymentEndpoint.php @@ -2,8 +2,8 @@ namespace Mollie\Api\Endpoints; -use Mollie\Api\Resources\LazyCollection; 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; diff --git a/src/Endpoints/PaymentLinkEndpoint.php b/src/Endpoints/PaymentLinkEndpoint.php index 1951040f..f27d09dd 100644 --- a/src/Endpoints/PaymentLinkEndpoint.php +++ b/src/Endpoints/PaymentLinkEndpoint.php @@ -2,8 +2,8 @@ namespace Mollie\Api\Endpoints; -use Mollie\Api\Resources\LazyCollection; 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; diff --git a/src/Endpoints/ProfileEndpoint.php b/src/Endpoints/ProfileEndpoint.php index 55bcee2f..d430275c 100644 --- a/src/Endpoints/ProfileEndpoint.php +++ b/src/Endpoints/ProfileEndpoint.php @@ -2,9 +2,9 @@ namespace Mollie\Api\Endpoints; -use Mollie\Api\Resources\LazyCollection; 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; diff --git a/src/Endpoints/RefundEndpoint.php b/src/Endpoints/RefundEndpoint.php index 019ae558..de03c477 100644 --- a/src/Endpoints/RefundEndpoint.php +++ b/src/Endpoints/RefundEndpoint.php @@ -2,8 +2,8 @@ namespace Mollie\Api\Endpoints; -use Mollie\Api\Resources\LazyCollection; use Mollie\Api\Exceptions\ApiException; +use Mollie\Api\Resources\LazyCollection; use Mollie\Api\Resources\Refund; use Mollie\Api\Resources\RefundCollection; diff --git a/src/Endpoints/SettlementCaptureEndpoint.php b/src/Endpoints/SettlementCaptureEndpoint.php index 7ad672e0..3e51443e 100644 --- a/src/Endpoints/SettlementCaptureEndpoint.php +++ b/src/Endpoints/SettlementCaptureEndpoint.php @@ -4,9 +4,9 @@ namespace Mollie\Api\Endpoints; -use Mollie\Api\Resources\LazyCollection; use Mollie\Api\Resources\Capture; use Mollie\Api\Resources\CaptureCollection; +use Mollie\Api\Resources\LazyCollection; class SettlementCaptureEndpoint extends CollectionEndpointAbstract { diff --git a/src/Endpoints/SettlementChargebackEndpoint.php b/src/Endpoints/SettlementChargebackEndpoint.php index f8032247..585ccf2c 100644 --- a/src/Endpoints/SettlementChargebackEndpoint.php +++ b/src/Endpoints/SettlementChargebackEndpoint.php @@ -4,9 +4,9 @@ namespace Mollie\Api\Endpoints; -use Mollie\Api\Resources\LazyCollection; use Mollie\Api\Resources\Chargeback; use Mollie\Api\Resources\ChargebackCollection; +use Mollie\Api\Resources\LazyCollection; class SettlementChargebackEndpoint extends CollectionEndpointAbstract { diff --git a/src/Endpoints/SettlementsEndpoint.php b/src/Endpoints/SettlementsEndpoint.php index da2269c1..76c7cad8 100644 --- a/src/Endpoints/SettlementsEndpoint.php +++ b/src/Endpoints/SettlementsEndpoint.php @@ -2,8 +2,8 @@ namespace Mollie\Api\Endpoints; -use Mollie\Api\Resources\LazyCollection; use Mollie\Api\Exceptions\ApiException; +use Mollie\Api\Resources\LazyCollection; use Mollie\Api\Resources\Settlement; use Mollie\Api\Resources\SettlementCollection; diff --git a/src/Endpoints/TerminalEndpoint.php b/src/Endpoints/TerminalEndpoint.php index b30bdb34..4f7a65c6 100644 --- a/src/Endpoints/TerminalEndpoint.php +++ b/src/Endpoints/TerminalEndpoint.php @@ -2,8 +2,8 @@ namespace Mollie\Api\Endpoints; -use Mollie\Api\Resources\LazyCollection; use Mollie\Api\Exceptions\ApiException; +use Mollie\Api\Resources\LazyCollection; use Mollie\Api\Resources\Terminal; use Mollie\Api\Resources\TerminalCollection; diff --git a/src/Resources/CursorCollection.php b/src/Resources/CursorCollection.php index a17f6df3..0f83c7e5 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; } @@ -112,7 +112,7 @@ public function getAutoIterator(bool $iterateBackwards = false): LazyCollection yield $item; } - if (($iterateBackwards && !$page->hasPrevious()) || !$page->hasNext()) { + if (($iterateBackwards && ! $page->hasPrevious()) || ! $page->hasNext()) { break; } diff --git a/src/Resources/LazyCollection.php b/src/Resources/LazyCollection.php index 655744ef..f555f086 100644 --- a/src/Resources/LazyCollection.php +++ b/src/Resources/LazyCollection.php @@ -70,7 +70,7 @@ public function first(callable $callback = null): mixed $iterator = $this->getIterator(); if (is_null($callback)) { - if (!$iterator->valid()) { + if (! $iterator->valid()) { return null; } @@ -113,7 +113,7 @@ public function take(int $limit): static $iterator = $this->getIterator(); while ($limit--) { - if (!$iterator->valid()) { + if (! $iterator->valid()) { break; } @@ -131,7 +131,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; } } From c34c35d5e921400437254b5e410418d579cdc58d Mon Sep 17 00:00:00 2001 From: Krishan Koenig Date: Mon, 30 Oct 2023 14:43:01 +0100 Subject: [PATCH 14/26] fix synthax test --- src/Resources/CursorCollection.php | 2 + src/Resources/LazyCollection.php | 39 +++++++++++++------ .../API/Resources/CursorCollectionTest.php | 8 +++- .../API/Resources/LazyCollectionTest.php | 16 ++++++++ 4 files changed, 52 insertions(+), 13 deletions(-) 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); + } } From 951d6884bc3c77426399042de26e51bba5fdcf5a Mon Sep 17 00:00:00 2001 From: Naoray Date: Mon, 30 Oct 2023 13:43:59 +0000 Subject: [PATCH 15/26] Fix styling --- src/Resources/CursorCollection.php | 1 + src/Resources/LazyCollection.php | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Resources/CursorCollection.php b/src/Resources/CursorCollection.php index 3168bd72..0bcf2a2d 100644 --- a/src/Resources/CursorCollection.php +++ b/src/Resources/CursorCollection.php @@ -108,6 +108,7 @@ public function getAutoIterator(bool $iterateBackwards = false): LazyCollection 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 a23eafe7..ea08a91a 100644 --- a/src/Resources/LazyCollection.php +++ b/src/Resources/LazyCollection.php @@ -129,7 +129,7 @@ public function take(int $limit): self * Determine if all items pass the given truth test. * * @param callable $callback - * @return boolean + * @return bool */ public function every(callable $callback): bool { From f23e7cb08e0c0bbe495fc46a731c2ab76c52c7dc Mon Sep 17 00:00:00 2001 From: Krishan Koenig Date: Mon, 30 Oct 2023 15:25:43 +0100 Subject: [PATCH 16/26] wip --- src/Resources/CursorCollection.php | 9 +++----- src/Resources/LazyCollection.php | 14 +++++++++---- src/Resources/MandateCollection.php | 16 ++++++-------- .../API/Resources/LazyCollectionTest.php | 21 ++++--------------- 4 files changed, 23 insertions(+), 37 deletions(-) diff --git a/src/Resources/CursorCollection.php b/src/Resources/CursorCollection.php index 0bcf2a2d..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; } @@ -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; } diff --git a/src/Resources/LazyCollection.php b/src/Resources/LazyCollection.php index ea08a91a..dbeac940 100644 --- a/src/Resources/LazyCollection.php +++ b/src/Resources/LazyCollection.php @@ -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; } @@ -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; } @@ -112,7 +118,7 @@ public function take(int $limit): self $iterator = $this->getIterator(); while ($limit--) { - if (! $iterator->valid()) { + if (!$iterator->valid()) { break; } @@ -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; } } diff --git a/src/Resources/MandateCollection.php b/src/Resources/MandateCollection.php index 23d39d16..235fe3ff 100644 --- a/src/Resources/MandateCollection.php +++ b/src/Resources/MandateCollection.php @@ -4,6 +4,11 @@ class MandateCollection extends CursorCollection { + public function getLazyCollectionName(): string + { + return LazyMandateCollection::class; + } + /** * @return string */ @@ -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); } } diff --git a/tests/Mollie/API/Resources/LazyCollectionTest.php b/tests/Mollie/API/Resources/LazyCollectionTest.php index a935da73..310ed190 100644 --- a/tests/Mollie/API/Resources/LazyCollectionTest.php +++ b/tests/Mollie/API/Resources/LazyCollectionTest.php @@ -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 { @@ -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); - } } From b6c2b11afaa1a086a83bdbefe8d6346142b35289 Mon Sep 17 00:00:00 2001 From: Naoray Date: Mon, 30 Oct 2023 14:26:37 +0000 Subject: [PATCH 17/26] Fix styling --- src/Resources/CursorCollection.php | 6 +++--- src/Resources/LazyCollection.php | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Resources/CursorCollection.php b/src/Resources/CursorCollection.php index a17f6df3..0f83c7e5 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; } @@ -112,7 +112,7 @@ public function getAutoIterator(bool $iterateBackwards = false): LazyCollection yield $item; } - if (($iterateBackwards && !$page->hasPrevious()) || !$page->hasNext()) { + if (($iterateBackwards && ! $page->hasPrevious()) || ! $page->hasNext()) { break; } diff --git a/src/Resources/LazyCollection.php b/src/Resources/LazyCollection.php index dbeac940..90360f53 100644 --- a/src/Resources/LazyCollection.php +++ b/src/Resources/LazyCollection.php @@ -75,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; } @@ -118,7 +118,7 @@ public function take(int $limit): self $iterator = $this->getIterator(); while ($limit--) { - if (!$iterator->valid()) { + if (! $iterator->valid()) { break; } @@ -142,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; } } From e934ce8c08758ed9517ee4f8b4ca1e0fc61af7cd Mon Sep 17 00:00:00 2001 From: Krishan Koenig Date: Mon, 30 Oct 2023 15:27:53 +0100 Subject: [PATCH 18/26] wip --- src/Resources/MandateCollection.php | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/Resources/MandateCollection.php b/src/Resources/MandateCollection.php index 235fe3ff..23d39d16 100644 --- a/src/Resources/MandateCollection.php +++ b/src/Resources/MandateCollection.php @@ -4,11 +4,6 @@ class MandateCollection extends CursorCollection { - public function getLazyCollectionName(): string - { - return LazyMandateCollection::class; - } - /** * @return string */ @@ -31,6 +26,15 @@ protected function createResourceObject() */ public function whereStatus($status) { - return $this->filter(fn (Mandate $mandate) => $mandate->status === $status); + $collection = new self($this->client, 0, $this->_links); + + foreach ($this as $item) { + if ($item->status === $status) { + $collection[] = $item; + $collection->count++; + } + } + + return $collection; } } From cde2bbcc8108b7cbb56d582d75bf6bb0b7d4abb3 Mon Sep 17 00:00:00 2001 From: Krishan Koenig Date: Mon, 30 Oct 2023 15:30:25 +0100 Subject: [PATCH 19/26] wip --- src/Resources/LazyCollection.php | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/src/Resources/LazyCollection.php b/src/Resources/LazyCollection.php index 90360f53..a7a39259 100644 --- a/src/Resources/LazyCollection.php +++ b/src/Resources/LazyCollection.php @@ -70,12 +70,12 @@ public function filter(callable $callback): self * @param (callable(value): bool)|null $callback * @return mixed|null */ - public function first(callable $callback = null): mixed + public function first(callable $callback = null) { $iterator = $this->getIterator(); if (is_null($callback)) { - if (! $iterator->valid()) { + if (!$iterator->valid()) { return null; } @@ -118,7 +118,7 @@ public function take(int $limit): self $iterator = $this->getIterator(); while ($limit--) { - if (! $iterator->valid()) { + if (!$iterator->valid()) { break; } @@ -142,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; } } @@ -184,9 +184,4 @@ protected function makeIterator($source): Iterator return $source(); } - - public function __call($name, $arguments) - { - var_dump($this->getIterator()); - } } From 4d389ef8d85281ab1aa7b4f1cd186edf422c04eb Mon Sep 17 00:00:00 2001 From: Naoray Date: Mon, 30 Oct 2023 14:30:45 +0000 Subject: [PATCH 20/26] Fix styling --- src/Resources/LazyCollection.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Resources/LazyCollection.php b/src/Resources/LazyCollection.php index a7a39259..6fbce03b 100644 --- a/src/Resources/LazyCollection.php +++ b/src/Resources/LazyCollection.php @@ -75,7 +75,7 @@ public function first(callable $callback = null) $iterator = $this->getIterator(); if (is_null($callback)) { - if (!$iterator->valid()) { + if (! $iterator->valid()) { return null; } @@ -118,7 +118,7 @@ public function take(int $limit): self $iterator = $this->getIterator(); while ($limit--) { - if (!$iterator->valid()) { + if (! $iterator->valid()) { break; } @@ -142,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; } } From 3393f47cbff63876a806a98aa739ba2df01dcf3d Mon Sep 17 00:00:00 2001 From: Krishan Koenig Date: Mon, 30 Oct 2023 15:32:28 +0100 Subject: [PATCH 21/26] fix test --- tests/Mollie/API/Resources/CursorCollectionTest.php | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/tests/Mollie/API/Resources/CursorCollectionTest.php b/tests/Mollie/API/Resources/CursorCollectionTest.php index a22672ad..8c54e973 100644 --- a/tests/Mollie/API/Resources/CursorCollectionTest.php +++ b/tests/Mollie/API/Resources/CursorCollectionTest.php @@ -4,9 +4,7 @@ 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 @@ -115,14 +113,12 @@ public function testWillReturnNullIfNoPreviousResultIsAvailable() public function testAutoPaginatorReturnsLazyCollection() { - $collection = new MandateCollection( + $collection = new OrderCollection( $this->createMock(MollieApiClient::class), 1, (object) [] ); - $this->assertInstanceOf(LazyCollection::class, $collection = $collection->getAutoIterator()); - - $collection->whereStatus(MandateStatus::STATUS_PENDING); + $this->assertInstanceOf(LazyCollection::class, $collection->getAutoIterator()); } } From 76124642258884c010ade30179a3e9aecf9ddb3c Mon Sep 17 00:00:00 2001 From: Krishan Koenig Date: Mon, 30 Oct 2023 15:54:19 +0100 Subject: [PATCH 22/26] fix phpstan --- src/Resources/LazyCollection.php | 45 +++++++++++++++++++------------- 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/src/Resources/LazyCollection.php b/src/Resources/LazyCollection.php index 6fbce03b..d4c8eac8 100644 --- a/src/Resources/LazyCollection.php +++ b/src/Resources/LazyCollection.php @@ -5,6 +5,10 @@ use Iterator; use IteratorAggregate; +/** + * @template TKey of array-key + * @template TValue + */ class LazyCollection implements IteratorAggregate { /** @@ -33,7 +37,7 @@ public function all(): array /** * Get an item from the collection by key. * - * @param int|string $key + * @param int|string $key * @return mixed|null */ public function get($key) @@ -50,12 +54,12 @@ public function get($key) /** * Run a filter over each of the items. * - * @param (callable(value, key): bool) $callback - * @return static + * @param (callable(TValue, TKey): bool) $callback + * @return self */ public function filter(callable $callback): self { - return new static(function () use ($callback) { + return new self(function () use ($callback) { foreach ($this as $key => $value) { if ($callback($value, $key)) { yield $key => $value; @@ -67,15 +71,15 @@ public function filter(callable $callback): self /** * Get the first item from the collection passing the given truth test. * - * @param (callable(value): bool)|null $callback - * @return mixed|null + * @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()) { + if (!$iterator->valid()) { return null; } @@ -94,12 +98,14 @@ public function first(callable $callback = null) /** * Run a map over each of the items. * - * @param callable(value, key): mixed $callback - * @return static + * @template TMapValue + * + * @param callable(TValue, TKey): TMapValue $callback + * @return static */ public function map(callable $callback): self { - return new static(function () use ($callback) { + return new self(function () use ($callback) { foreach ($this as $key => $value) { yield $key => $callback($value, $key); } @@ -109,16 +115,16 @@ public function map(callable $callback): self /** * Take the first or last {$limit} items. * - * @param int $limit + * @param int $limit * @return static */ public function take(int $limit): self { - return new static(function () use ($limit) { + return new self(function () use ($limit) { $iterator = $this->getIterator(); while ($limit--) { - if (! $iterator->valid()) { + if (!$iterator->valid()) { break; } @@ -134,7 +140,7 @@ public function take(int $limit): self /** * Determine if all items pass the given truth test. * - * @param callable $callback + * @param (callable(TValue, TKey): bool) $callback * @return bool */ public function every(callable $callback): bool @@ -142,7 +148,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; } } @@ -163,7 +169,7 @@ public function count(): int /** * Get an iterator for the items. * - * @return Iterator + * @return Iterator */ public function getIterator(): Iterator { @@ -173,8 +179,11 @@ public function getIterator(): Iterator /** * Get an iterator for the given value. * - * @param callable|IteratorAggregate $source - * @return Iterator + * @template TIteratorKey of array-key + * @template TIteratorValue + * + * @param IteratorAggregate|(callable(): \Generator) $source + * @return Iterator */ protected function makeIterator($source): Iterator { From 9f39287fd9b4e76f959edfb4a2d6d08ea361def8 Mon Sep 17 00:00:00 2001 From: Naoray Date: Mon, 30 Oct 2023 14:54:44 +0000 Subject: [PATCH 23/26] Fix styling --- src/Resources/LazyCollection.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Resources/LazyCollection.php b/src/Resources/LazyCollection.php index d4c8eac8..3625aecf 100644 --- a/src/Resources/LazyCollection.php +++ b/src/Resources/LazyCollection.php @@ -79,7 +79,7 @@ public function first(callable $callback = null) $iterator = $this->getIterator(); if (is_null($callback)) { - if (!$iterator->valid()) { + if (! $iterator->valid()) { return null; } @@ -124,7 +124,7 @@ public function take(int $limit): self $iterator = $this->getIterator(); while ($limit--) { - if (!$iterator->valid()) { + if (! $iterator->valid()) { break; } @@ -148,7 +148,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; } } From 94324f6e50b2b4766ac2b54ee61470b9eca28e73 Mon Sep 17 00:00:00 2001 From: Krishan Koenig Date: Mon, 30 Oct 2023 16:15:34 +0100 Subject: [PATCH 24/26] add chained test --- src/Resources/LazyCollection.php | 8 +++++--- tests/Mollie/API/Resources/LazyCollectionTest.php | 13 +++++++++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/Resources/LazyCollection.php b/src/Resources/LazyCollection.php index 3625aecf..0fc90f08 100644 --- a/src/Resources/LazyCollection.php +++ b/src/Resources/LazyCollection.php @@ -8,6 +8,8 @@ /** * @template TKey of array-key * @template TValue + * + * @implements IteratorAggregate */ class LazyCollection implements IteratorAggregate { @@ -37,8 +39,8 @@ public function all(): array /** * Get an item from the collection by key. * - * @param int|string $key - * @return mixed|null + * @param TKey $key + * @return TValue|null */ public function get($key) { @@ -113,7 +115,7 @@ public function map(callable $callback): self } /** - * Take the first or last {$limit} items. + * Take the first {$limit} items. * * @param int $limit * @return static diff --git a/tests/Mollie/API/Resources/LazyCollectionTest.php b/tests/Mollie/API/Resources/LazyCollectionTest.php index 310ed190..726c76bc 100644 --- a/tests/Mollie/API/Resources/LazyCollectionTest.php +++ b/tests/Mollie/API/Resources/LazyCollectionTest.php @@ -84,4 +84,17 @@ public function testEvery() 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()); + } } From 412a9f4c298e98119d3ab9777ce0ebf8027dc570 Mon Sep 17 00:00:00 2001 From: Krishan Koenig Date: Thu, 2 Nov 2023 11:26:59 +0100 Subject: [PATCH 25/26] add consecutive auto iteration test --- .../API/Resources/CursorCollectionTest.php | 139 +++++++++++++++--- 1 file changed, 117 insertions(+), 22 deletions(-) diff --git a/tests/Mollie/API/Resources/CursorCollectionTest.php b/tests/Mollie/API/Resources/CursorCollectionTest.php index 8c54e973..b55c9175 100644 --- a/tests/Mollie/API/Resources/CursorCollectionTest.php +++ b/tests/Mollie/API/Resources/CursorCollectionTest.php @@ -6,6 +6,7 @@ use Mollie\Api\Resources\LazyCollection; use Mollie\Api\Resources\OrderCollection; use PHPUnit\Framework\TestCase; +use stdClass; class CursorCollectionTest extends TestCase { @@ -14,28 +15,28 @@ public function testCanGetNextCollectionResultWhenNextLinkIsAvailable() $mockedClient = $this->createMock(MollieApiClient::class); $mockedClient->expects($this->once()) ->method('performHttpCallToFullUrl') - ->willReturn((object) [ + ->willReturn($this->arrayToObject([ 'count' => 1, - '_links' => (object) [ + '_links' => [ 'self' => [ 'href' => 'https://api.mollie.com/v2/orders?from=ord_stTC2WHAuS', ], ], - '_embedded' => (object) [ + '_embedded' => [ 'orders' => [ - (object) ['id' => 'ord_stTC2WHAuS'], + ['id' => 'ord_stTC2WHAuS'], ], ], - ]); + ])); $collection = new OrderCollection( $mockedClient, 1, - (object) [ - 'next' => (object) [ + $this->arrayToObject([ + 'next' => [ 'href' => 'https://api.mollie.com/v2/orders?from=ord_stTC2WHAuS', ], - ] + ]) ); $this->assertTrue($collection->hasNext()); @@ -65,28 +66,30 @@ public function testCanGetPreviousCollectionResultWhenPreviousLinkIsAvailable() $mockedClient = $this->createMock(MollieApiClient::class); $mockedClient->expects($this->once()) ->method('performHttpCallToFullUrl') - ->willReturn((object) [ - 'count' => 1, - '_links' => (object) [ - 'self' => [ - 'href' => 'https://api.mollie.com/v2/orders?from=ord_stTC2WHAuS', + ->willReturn( + $this->arrayToObject([ + 'count' => 1, + '_links' => [ + 'self' => [ + 'href' => 'https://api.mollie.com/v2/orders?from=ord_stTC2WHAuS', + ], ], - ], - '_embedded' => (object) [ - 'orders' => [ - (object) ['id' => 'ord_stTC2WHAuS'], + '_embedded' => [ + 'orders' => [ + ['id' => 'ord_stTC2WHAuS'], + ], ], - ], - ]); + ]) + ); $collection = new OrderCollection( $mockedClient, 1, - (object) [ - 'previous' => (object) [ + $this->arrayToObject([ + 'previous' => [ 'href' => 'https://api.mollie.com/v2/orders?from=ord_stTC2WHAuS', ], - ] + ]) ); $this->assertTrue($collection->hasPrevious()); @@ -121,4 +124,96 @@ public function testAutoPaginatorReturnsLazyCollection() $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; + } } From 657fbad2dbe4b211807382fe8d87cab1f4b6aeaa Mon Sep 17 00:00:00 2001 From: Naoray Date: Thu, 2 Nov 2023 10:27:27 +0000 Subject: [PATCH 26/26] Fix styling --- tests/Mollie/API/Resources/CursorCollectionTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Mollie/API/Resources/CursorCollectionTest.php b/tests/Mollie/API/Resources/CursorCollectionTest.php index b55c9175..d521d4dc 100644 --- a/tests/Mollie/API/Resources/CursorCollectionTest.php +++ b/tests/Mollie/API/Resources/CursorCollectionTest.php @@ -168,7 +168,7 @@ public function testAutoPaginatorCanHandleConsecutiveCalls() '_links' => [ 'self' => [ 'href' => 'https://api.mollie.com/v2/orders?from=ord_stTC2WHAuB', - ] + ], ], '_embedded' => [ 'orders' => [ @@ -204,7 +204,7 @@ public function testAutoPaginatorCanHandleConsecutiveCalls() */ private function arrayToObject($data) { - if (!is_array($data)) { + if (! is_array($data)) { return $data; }