| 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 |
|