PluginProbe
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments / 2.1.3
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments v2.1.3
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
151 lines 2.7 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 = $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 * Does this collection have a next page?
42 *
43 * @return boolean
44 */
45 public function hasNextPage() {
46 $seen = $this->pagination->page * $this->pagination->limit;
47 return $this->pagination->count > $seen;
48 }
49
50 /**
51 * Does this collection have a previous page?
52 *
53 * @return boolean
54 */
55 public function hasPreviousPage() {
56 return $this->pagination->page > 1;
57 }
58
59 /**
60 * Get a specific attribute
61 *
62 * @param string $key Attribute name.
63 *
64 * @return mixed
65 */
66 public function getAttribute( $key ) {
67 $attribute = null;
68
69 if ( $this->hasAttribute( $key ) ) {
70 $attribute = $this->attributes->$key;
71 }
72
73 return $attribute;
74 }
75
76 /**
77 * Does it have the attribute
78 *
79 * @param string $key Attribute key.
80 *
81 * @return boolean
82 */
83 public function hasAttribute( $key ) {
84 return property_exists( $this->attributes, $key );
85 }
86
87 /**
88 * Get the attribute
89 *
90 * @param string $key Attribute name.
91 *
92 * @return mixed
93 */
94 public function __get( $key ) {
95 return $this->getAttribute( $key );
96 }
97
98 /**
99 * Set the attribute
100 *
101 * @param string $key Attribute name.
102 * @param mixed $value Value of attribute.
103 *
104 * @return void
105 */
106 public function __set( $key, $value ) {
107 $this->setAttribute( $key, $value );
108 }
109
110 /**
111 * Determine if the given attribute exists.
112 *
113 * @param mixed $offset Name.
114 * @return bool
115 */
116 public function offsetExists( $offset ) {
117 return ! is_null( $this->getAttribute( $offset ) );
118 }
119
120 /**
121 * Get the value for a given offset.
122 *
123 * @param mixed $offset Name.
124 * @return mixed
125 */
126 public function offsetGet( $offset ) {
127 return $this->getAttribute( $offset );
128 }
129
130 /**
131 * Set the value for a given offset.
132 *
133 * @param mixed $offset Name.
134 * @param mixed $value Value.
135 * @return void
136 */
137 public function offsetSet( $offset, $value ) {
138 $this->setAttribute( $offset, $value );
139 }
140
141 /**
142 * Forward call to method
143 *
144 * @param string $method Method to call.
145 * @param mixed $params Method params.
146 */
147 public function __call( $method, $params ) {
148 return call_user_func_array( [ $this, $method ], $params );
149 }
150 }
151