diff --git a/composer.json b/composer.json index f99d117..f47b604 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,7 @@ } ], "require": { - "php": "^8.0", + "php": "^8.1", "ext-json": "*", "illuminate/support": "^11.0|^12.0", "illuminate/console": "^11.0|^12.0", diff --git a/src/Id/Concerns/MagicProperties.php b/src/Id/Concerns/MagicProperties.php index 9e476a6..0e6ba26 100644 --- a/src/Id/Concerns/MagicProperties.php +++ b/src/Id/Concerns/MagicProperties.php @@ -6,12 +6,34 @@ use stdClass; trait MagicProperties { - protected function setMagicProperties(stdClass $data): void + protected function setMagicProperties(stdClass|array $data): void { - foreach ($data as $key => $value) { + foreach ((object)$data as $key => $value) { if (!property_exists($this, $key)) { $this->{$key} = $value; } } } + + /** + * Magic getter: return null for undefined properties + * + * @param string $name + * @return mixed|null + */ + public function __get(string $name) + { + return null; + } + + /** + * Magic isset: return false for undefined properties + * + * @param string $name + * @return bool + */ + public function __isset(string $name): bool + { + return false; + } } \ No newline at end of file diff --git a/src/Id/Concerns/ManagesInvoices.php b/src/Id/Concerns/ManagesInvoices.php index 78899b8..212fc85 100644 --- a/src/Id/Concerns/ManagesInvoices.php +++ b/src/Id/Concerns/ManagesInvoices.php @@ -17,9 +17,9 @@ trait ManagesInvoices * @throws RequestRequiresClientIdException * @throws GuzzleException */ - public function invoices(): Invoices + public function invoices(array $parameters = []): Invoices { - return (new Invoices($this->request('GET', 'v1/invoices'))) + return (new Invoices($this->request('GET', 'v1/invoices', [], $parameters))) ->setBillable($this); } } \ No newline at end of file diff --git a/src/Id/Concerns/ManagesOrders.php b/src/Id/Concerns/ManagesOrders.php index b15ad12..c86548a 100644 --- a/src/Id/Concerns/ManagesOrders.php +++ b/src/Id/Concerns/ManagesOrders.php @@ -18,9 +18,9 @@ trait ManagesOrders * @throws RequestRequiresClientIdException * @throws GuzzleException */ - public function orders(): Orders + public function orders(array $parameters = []): Orders { - return (new Orders($this->request('GET', 'v1/orders'))) + return (new Orders($this->request('GET', 'v1/orders', [], $parameters))) ->setBillable($this); } } \ No newline at end of file diff --git a/src/Id/Resources/BaseCollection.php b/src/Id/Resources/BaseCollection.php index 260990e..64ad11f 100644 --- a/src/Id/Resources/BaseCollection.php +++ b/src/Id/Resources/BaseCollection.php @@ -2,11 +2,14 @@ namespace Anikeen\Id\Resources; +use Anikeen\Id\Concerns\MagicProperties; use Anikeen\Id\Result; use JsonSerializable; abstract class BaseCollection implements JsonSerializable { + use MagicProperties; + public function __construct(protected Result $result) { // diff --git a/src/Id/Resources/Order.php b/src/Id/Resources/Order.php index 2f1e8c7..3189d9a 100644 --- a/src/Id/Resources/Order.php +++ b/src/Id/Resources/Order.php @@ -102,13 +102,12 @@ class Order extends BaseResource /** * Get order items from given order. * - * @param string $orderId The order ID. * @throws RequestRequiresClientIdException * @throws GuzzleException */ - public function orderItems(string $orderId): OrderItems + public function orderItems(array $parameters = []): OrderItems { - return (new OrderItems($this->billable->request('GET', sprintf('v1/orders/%s/items', $this->id)))) + return (new OrderItems($this->billable->request('GET', sprintf('v1/orders/%s/items', $this->id), [], $parameters))) ->setBillable($this->billable) ->setParent($this); } diff --git a/src/Id/Resources/PaymentMethod.php b/src/Id/Resources/PaymentMethod.php index 1d624e6..8107c6f 100644 --- a/src/Id/Resources/PaymentMethod.php +++ b/src/Id/Resources/PaymentMethod.php @@ -4,6 +4,9 @@ namespace Anikeen\Id\Resources; use Anikeen\Id\Concerns\HasBillable; +/** + * @property string $id + */ class PaymentMethod extends BaseResource { use HasBillable; diff --git a/src/Id/Resources/PaymentMethods.php b/src/Id/Resources/PaymentMethods.php index 517edfe..ca6f9e0 100644 --- a/src/Id/Resources/PaymentMethods.php +++ b/src/Id/Resources/PaymentMethods.php @@ -11,6 +11,8 @@ class PaymentMethods extends BaseCollection { use HasBillable; + private ?PaymentMethod $cachedDefaultPaymentMethod = null; + /** * Check if current user has at least one payment method. * @@ -30,8 +32,24 @@ class PaymentMethods extends BaseCollection */ public function defaultPaymentMethod(): PaymentMethod { - return (new PaymentMethod($this->billable->request('GET', 'v1/payment-methods/default'))) - ->setBillable($this->billable); + if ($this->cachedDefaultPaymentMethod === null) { + $this->cachedDefaultPaymentMethod = (new PaymentMethod( + $this->billable->request('GET', 'v1/payment-methods/default') + ))->setBillable($this->billable); + } + + return $this->cachedDefaultPaymentMethod; + } + + /** + * Check if the current user has a default payment method. + * + * @throws RequestRequiresClientIdException + * @throws GuzzleException + */ + public function hasDefaultPaymentMethod(): bool + { + return $this->defaultPaymentMethod()?->id !== null; } /** diff --git a/src/Id/Resources/Subscription.php b/src/Id/Resources/Subscription.php index f988a55..d14b6b1 100644 --- a/src/Id/Resources/Subscription.php +++ b/src/Id/Resources/Subscription.php @@ -8,6 +8,12 @@ use GuzzleHttp\Exception\GuzzleException; /** * @property string $id + * @property string $name + * @property string $description + * @property string $status + * @property string $unit + * @property float $price + * @property string $ends_at */ class Subscription extends BaseResource {