Blocks
1 year ago
Concerns
1 year ago
Traits
1 year ago
AbandonedCheckout.php
1 year ago
AbandonedCheckoutProtocol.php
3 years ago
Account.php
3 years ago
AccountPortalSession.php
3 years ago
Activation.php
1 year ago
Affiliation.php
1 year ago
AffiliationProduct.php
1 year ago
AffiliationProtocol.php
2 years ago
AffiliationRequest.php
1 year ago
ApiToken.php
3 years ago
BalanceTransaction.php
1 year ago
Brand.php
3 years ago
BulkAction.php
2 years ago
Bump.php
1 year ago
BuyLink.php
2 years ago
CancellationAct.php
1 year ago
CancellationReason.php
3 years ago
Charge.php
1 year ago
Checkout.php
1 year ago
Click.php
1 year ago
Collection.php
2 years ago
CommissionStructure.php
2 years ago
Component.php
3 years ago
Coupon.php
1 year ago
Customer.php
1 year ago
CustomerLink.php
3 years ago
CustomerNotificationProtocol.php
3 years ago
CustomerPortalProtocol.php
1 year ago
DatabaseModel.php
1 year ago
Discount.php
1 year ago
DisplayCurrency.php
1 year ago
Download.php
3 years ago
Event.php
3 years ago
Export.php
2 years ago
ExternalApiModel.php
1 year ago
Fee.php
1 year ago
Form.php
3 years ago
Fulfillment.php
1 year ago
FulfillmentItem.php
1 year ago
GalleryItem.php
1 year ago
GalleryItemAttachment.php
1 year ago
GalleryItemMedia.php
1 year ago
GalleryItemProductMedia.php
1 year ago
IncomingWebhook.php
1 year ago
Integration.php
2 years ago
IntegrationCatalog.php
1 year ago
Invoice.php
1 year ago
License.php
1 year ago
LineItem.php
1 year ago
ManualPaymentMethod.php
1 year ago
Media.php
1 year ago
Model.php
1 year ago
ModelInterface.php
3 years ago
Order.php
1 year ago
OrderProtocol.php
3 years ago
PaymentFailure.php
1 year ago
PaymentInstrument.php
1 year ago
PaymentIntent.php
1 year ago
PaymentMethod.php
1 year ago
Payout.php
1 year ago
PayoutGroup.php
2 years ago
Period.php
1 year ago
PlatformFee.php
1 year ago
PortalSession.php
3 years ago
Price.php
1 year ago
Processor.php
1 year ago
Product.php
1 year ago
ProductCollection.php
1 year ago
ProductGroup.php
1 year ago
ProductMedia.php
1 year ago
Promotion.php
1 year ago
ProvisionalAccount.php
2 years ago
Purchase.php
2 years ago
Referral.php
1 year ago
ReferralItem.php
2 years ago
Refund.php
1 year ago
RefundItem.php
1 year ago
RegisteredWebhook.php
2 years ago
ReturnItem.php
2 years ago
ReturnReason.php
2 years ago
ReturnRequest.php
1 year ago
ServiceFee.php
1 year ago
ShippingChoice.php
1 year ago
ShippingMethod.php
3 years ago
ShippingProfile.php
3 years ago
ShippingProtocol.php
3 years ago
ShippingRate.php
3 years ago
ShippingZone.php
3 years ago
Statistic.php
3 years ago
Subscription.php
1 year ago
SubscriptionProtocol.php
3 years ago
TaxOverride.php
2 years ago
TaxProtocol.php
3 years ago
TaxRegistration.php
1 year ago
TaxZone.php
3 years ago
Upload.php
3 years ago
Upsell.php
1 year ago
UpsellFunnel.php
1 year ago
User.php
2 years ago
Variant.php
1 year ago
VariantOption.php
2 years ago
VariantOptionValue.php
1 year ago
VariantValue.php
2 years ago
VerificationCode.php
3 years ago
Webhook.php
1 year ago
WebhookRegistration.php
2 years ago
Collection.php
169 lines
| 1 | <?php |
| 2 | namespace SureCart\Models; |
| 3 | |
| 4 | /** |
| 5 | * Stores a collection of items. |
| 6 | */ |
| 7 | class Collection { |
| 8 | /** |
| 9 | * Keeps track of booted models |
| 10 | * |
| 11 | * @var array |
| 12 | */ |
| 13 | protected static $booted = []; |
| 14 | |
| 15 | /** |
| 16 | * Stores the collection data |
| 17 | * |
| 18 | * @var StdClass |
| 19 | */ |
| 20 | protected $attributes; |
| 21 | |
| 22 | /** |
| 23 | * Model constructor |
| 24 | * |
| 25 | * @param object $collection Optional attributes. |
| 26 | */ |
| 27 | public function __construct( $collection ) { |
| 28 | $this->attributes = (object) $collection; |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Get the called class name |
| 33 | * |
| 34 | * @return string |
| 35 | */ |
| 36 | public static function getCalledClassName() { |
| 37 | return str_replace( '\\', '_', get_called_class() ); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Get the total. |
| 42 | * |
| 43 | * @return string |
| 44 | */ |
| 45 | public function total() { |
| 46 | return $this->pagination->count ?? 0; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Get the total pages. |
| 51 | * |
| 52 | * @return string |
| 53 | */ |
| 54 | public function totalPages() { |
| 55 | return ceil( ( $this->pagination->count ?? 0 ) / ( $this->pagination->limit ?? 1 ) ); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Does this collection have a next page? |
| 60 | * |
| 61 | * @return boolean |
| 62 | */ |
| 63 | public function hasNextPage() { |
| 64 | $seen = $this->pagination->page * $this->pagination->limit; |
| 65 | return $this->pagination->count > $seen; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Does this collection have a previous page? |
| 70 | * |
| 71 | * @return boolean |
| 72 | */ |
| 73 | public function hasPreviousPage() { |
| 74 | return $this->pagination->page > 1; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Get a specific attribute |
| 79 | * |
| 80 | * @param string $key Attribute name. |
| 81 | * |
| 82 | * @return mixed |
| 83 | */ |
| 84 | public function getAttribute( $key ) { |
| 85 | $attribute = null; |
| 86 | |
| 87 | if ( $this->hasAttribute( $key ) ) { |
| 88 | $attribute = $this->attributes->$key; |
| 89 | } |
| 90 | |
| 91 | return $attribute; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Does it have the attribute |
| 96 | * |
| 97 | * @param string $key Attribute key. |
| 98 | * |
| 99 | * @return boolean |
| 100 | */ |
| 101 | public function hasAttribute( $key ) { |
| 102 | return property_exists( $this->attributes, $key ); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Get the attribute |
| 107 | * |
| 108 | * @param string $key Attribute name. |
| 109 | * |
| 110 | * @return mixed |
| 111 | */ |
| 112 | public function __get( $key ) { |
| 113 | return $this->getAttribute( $key ); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Set the attribute |
| 118 | * |
| 119 | * @param string $key Attribute name. |
| 120 | * @param mixed $value Value of attribute. |
| 121 | * |
| 122 | * @return void |
| 123 | */ |
| 124 | public function __set( $key, $value ) { |
| 125 | $this->setAttribute( $key, $value ); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Determine if the given attribute exists. |
| 130 | * |
| 131 | * @param mixed $offset Name. |
| 132 | * @return bool |
| 133 | */ |
| 134 | public function offsetExists( $offset ) { |
| 135 | return ! is_null( $this->getAttribute( $offset ) ); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Get the value for a given offset. |
| 140 | * |
| 141 | * @param mixed $offset Name. |
| 142 | * @return mixed |
| 143 | */ |
| 144 | public function offsetGet( $offset ) { |
| 145 | return $this->getAttribute( $offset ); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Set the value for a given offset. |
| 150 | * |
| 151 | * @param mixed $offset Name. |
| 152 | * @param mixed $value Value. |
| 153 | * @return void |
| 154 | */ |
| 155 | public function offsetSet( $offset, $value ) { |
| 156 | $this->setAttribute( $offset, $value ); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Forward call to method |
| 161 | * |
| 162 | * @param string $method Method to call. |
| 163 | * @param mixed $params Method params. |
| 164 | */ |
| 165 | public function __call( $method, $params ) { |
| 166 | return call_user_func_array( [ $this, $method ], $params ); |
| 167 | } |
| 168 | } |
| 169 |