PluginProbe
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments / 4.4.2
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments v4.4.2
4.7.2 4.7.1 4.7.0 4.6.6 4.6.5 4.6.4 4.6.3 4.6.2 4.6.1 4.6.0 4.5.1 4.5.0 4.4.2 4.4.1 4.4.0 4.3.3 4.3.2 4.3.1 4.3.0 4.2.3 4.2.2 4.2.1 1.0.3 1.0.4 1.0.5 All 281 releases
surecart / app / src / Models / Collection.php
Collection.php
170 lines 3.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 #[\ReturnTypeWillChange]
145 public function offsetGet( $offset ) {
146 return $this->getAttribute( $offset );
147 }
148
149 /**
150 * Set the value for a given offset.
151 *
152 * @param mixed $offset Name.
153 * @param mixed $value Value.
154 * @return void
155 */
156 public function offsetSet( $offset, $value ) {
157 $this->setAttribute( $offset, $value );
158 }
159
160 /**
161 * Forward call to method
162 *
163 * @param string $method Method to call.
164 * @param mixed $params Method params.
165 */
166 public function __call( $method, $params ) {
167 return call_user_func_array( [ $this, $method ], $params );
168 }
169 }
170