CanFinalize.php
3 years ago
HasAffiliation.php
2 years ago
HasAttributes.php
1 year ago
HasBillingAddress.php
2 years ago
HasCharge.php
3 years ago
HasCheckout.php
3 years ago
HasCommissionStructure.php
2 years ago
HasCoupon.php
3 years ago
HasCustomer.php
3 years ago
HasDiscount.php
1 year ago
HasImageSizes.php
2 years ago
HasLicense.php
2 years ago
HasLineItem.php
2 years ago
HasOrder.php
3 years ago
HasPaymentInstrument.php
1 year ago
HasPaymentIntent.php
3 years ago
HasPaymentMethod.php
3 years ago
HasPayout.php
2 years ago
HasPayouts.php
2 years ago
HasPrice.php
3 years ago
HasProcessorType.php
3 years ago
HasProduct.php
3 years ago
HasPurchase.php
3 years ago
HasPurchases.php
3 years ago
HasReferral.php
2 years ago
HasReferralItems.php
2 years ago
HasReferrals.php
2 years ago
HasRefund.php
3 years ago
HasShippingAddress.php
1 year ago
HasSubscription.php
3 years ago
HasSubscriptions.php
3 years ago
SyncsCustomer.php
3 years ago
HasAttributes.php
409 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\Models\Traits; |
| 4 | |
| 5 | trait HasAttributes { |
| 6 | /** |
| 7 | * Keeps track of booted models |
| 8 | * |
| 9 | * @var array |
| 10 | */ |
| 11 | protected static $booted = []; |
| 12 | |
| 13 | /** |
| 14 | * Stores model attributes |
| 15 | * |
| 16 | * @var array |
| 17 | */ |
| 18 | protected $attributes = []; |
| 19 | |
| 20 | /** |
| 21 | * Fillable model items |
| 22 | * |
| 23 | * @var array |
| 24 | */ |
| 25 | protected $fillable = [ '*' ]; |
| 26 | |
| 27 | /** |
| 28 | * Guarded model items |
| 29 | * |
| 30 | * @var array |
| 31 | */ |
| 32 | protected $guarded = []; |
| 33 | |
| 34 | /** |
| 35 | * Model constructor |
| 36 | * |
| 37 | * @param array $attributes Optional attributes. |
| 38 | */ |
| 39 | public function __construct( $attributes = [] ) { |
| 40 | // if we have string here, assume it's the id. |
| 41 | if ( is_string( $attributes ) || is_int( $attributes ) ) { |
| 42 | $attributes = [ 'id' => $attributes ]; |
| 43 | } |
| 44 | $this->fill( $attributes ); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Get fillable items |
| 49 | * |
| 50 | * @return array |
| 51 | */ |
| 52 | public function getFillable() { |
| 53 | return $this->fillable; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Is the attribute fillable? |
| 58 | * |
| 59 | * @param string $key Attribute name. |
| 60 | * |
| 61 | * @return boolean |
| 62 | */ |
| 63 | public function isFillable( $key ) { |
| 64 | $fillable = $this->getFillable(); |
| 65 | |
| 66 | if ( in_array( $key, $fillable, true ) ) { |
| 67 | return true; |
| 68 | } |
| 69 | |
| 70 | if ( $this->isGuarded( $key ) ) { |
| 71 | return false; |
| 72 | } |
| 73 | |
| 74 | return ! empty( $fillable ) && '*' === $fillable[0]; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Sets attributes in model |
| 79 | * |
| 80 | * @param array $attributes Attributes to fill. |
| 81 | * |
| 82 | * @return Model |
| 83 | */ |
| 84 | public function fill( $attributes ) { |
| 85 | return $this->setAttributes( $attributes ); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Get guarded items |
| 90 | * |
| 91 | * @return array |
| 92 | */ |
| 93 | public function getGuarded() { |
| 94 | return $this->guarded; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Is the key guarded |
| 99 | * |
| 100 | * @param string $key Name of the attribute. |
| 101 | * |
| 102 | * @return boolean |
| 103 | */ |
| 104 | public function isGuarded( $key ) { |
| 105 | $guarded = $this->getGuarded(); |
| 106 | return in_array( $key, $guarded, true ) || [ '*' ] === $guarded; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Reset attributes to blank. |
| 111 | * |
| 112 | * @return $this |
| 113 | */ |
| 114 | public function resetAttributes() { |
| 115 | $this->attributes = []; |
| 116 | return $this; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Set model attributes |
| 121 | * |
| 122 | * @param array $attributes Attributes to add. |
| 123 | * @param boolean $is_guarded Use guarded. |
| 124 | * |
| 125 | * @return Model |
| 126 | */ |
| 127 | public function setAttributes( $attributes, $is_guarded = true ) { |
| 128 | if ( empty( $attributes ) ) { |
| 129 | return $this; |
| 130 | } |
| 131 | |
| 132 | foreach ( $attributes as $key => $value ) { |
| 133 | // remove api attributes. |
| 134 | if ( in_array( $key, [ '_locale', 'rest_route' ], true ) ) { |
| 135 | continue; |
| 136 | } |
| 137 | |
| 138 | // set attribute. |
| 139 | if ( ! $is_guarded ) { |
| 140 | $this->setAttribute( $key, maybe_unserialize( maybe_unserialize( $value ) ) ); |
| 141 | } else { |
| 142 | if ( $this->isFillable( $key ) ) { |
| 143 | $this->setAttribute( $key, maybe_unserialize( maybe_unserialize( $value ) ) ); |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | return apply_filters( 'surecart/set_attributes', $this ); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Sets an attribute |
| 153 | * Optionally calls a mutator based on set{Attribute}Attribute |
| 154 | * |
| 155 | * @param string $key Attribute key. |
| 156 | * @param mixed $value Attribute value. |
| 157 | * |
| 158 | * @return mixed|void |
| 159 | */ |
| 160 | public function setAttribute( $key, $value ) { |
| 161 | $setter = $this->getMutator( $key, 'set' ); |
| 162 | |
| 163 | if ( $setter ) { |
| 164 | return $this->{$setter}( $value ); |
| 165 | } else { |
| 166 | $this->attributes[ $key ] = $value; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Does it have the attribute |
| 172 | * |
| 173 | * @param string $key Attribute key. |
| 174 | * |
| 175 | * @return boolean |
| 176 | */ |
| 177 | public function hasAttribute( $key ) { |
| 178 | return array_key_exists( $key, $this->attributes ); |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Get a specific attribute |
| 183 | * |
| 184 | * @param string $key Attribute name. |
| 185 | * |
| 186 | * @return mixed |
| 187 | */ |
| 188 | public function getAttribute( $key ) { |
| 189 | $attribute = null; |
| 190 | |
| 191 | if ( $this->hasAttribute( $key ) ) { |
| 192 | $attribute = $this->attributes[ $key ]; |
| 193 | } |
| 194 | |
| 195 | $getter = $this->getMutator( $key, 'get' ); |
| 196 | |
| 197 | if ( $getter ) { |
| 198 | return $this->{$getter}( $attribute ); |
| 199 | } elseif ( ! is_null( $attribute ) ) { |
| 200 | return $attribute; |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Calls a mutator based on set{Attribute}Attribute |
| 206 | * |
| 207 | * @param string $key Attribute key. |
| 208 | * @param mixed $type 'get' or 'set'. |
| 209 | * |
| 210 | * @return string|false |
| 211 | */ |
| 212 | public function getMutator( $key, $type ) { |
| 213 | $key = ucwords( str_replace( [ '-', '_' ], ' ', $key ) ); |
| 214 | |
| 215 | $method = $type . str_replace( ' ', '', $key ) . 'Attribute'; |
| 216 | |
| 217 | if ( method_exists( $this, $method ) ) { |
| 218 | return $method; |
| 219 | } |
| 220 | |
| 221 | return false; |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Get the attributes |
| 226 | * |
| 227 | * @return array |
| 228 | */ |
| 229 | public function getAttributes() { |
| 230 | return json_decode( wp_json_encode( $this->attributes ), true ); |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Convert to object. |
| 235 | * |
| 236 | * @return Object |
| 237 | */ |
| 238 | public function toObject() { |
| 239 | $attributes = (object) $this->attributes; |
| 240 | |
| 241 | // Check if any accessor is available and call it. |
| 242 | foreach ( get_class_methods( $this ) as $method ) { |
| 243 | if ( method_exists( get_class(), $method ) ) { |
| 244 | continue; |
| 245 | } |
| 246 | |
| 247 | if ( 'get' === substr( $method, 0, 3 ) && 'Attribute' === substr( $method, -9 ) ) { |
| 248 | $key = str_replace( [ 'get', 'Attribute' ], '', $method ); |
| 249 | if ( $key ) { |
| 250 | $pieces = preg_split( '/(?=[A-Z])/', $key ); |
| 251 | $pieces = array_map( 'strtolower', array_filter( $pieces ) ); |
| 252 | $key = implode( '_', $pieces ); |
| 253 | $value = array_key_exists( $key, $this->attributes ) ? $this->attributes[ $key ] : null; |
| 254 | $attributes->$key = $this->{$method}( $value ); |
| 255 | } |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | // Check if any attribute is a model and call toArray. |
| 260 | array_walk_recursive( |
| 261 | $attributes, |
| 262 | function ( &$value ) { |
| 263 | if ( method_exists( $value, 'toObject' ) ) { |
| 264 | $value = $value->toObject(); |
| 265 | } |
| 266 | } |
| 267 | ); |
| 268 | |
| 269 | return $attributes; |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * Calls accessors during toArray. |
| 274 | * |
| 275 | * @return Array |
| 276 | */ |
| 277 | public function toArray() { |
| 278 | $attributes = $this->getAttributes(); |
| 279 | |
| 280 | // Check if any accessor is available and call it. |
| 281 | foreach ( get_class_methods( $this ) as $method ) { |
| 282 | if ( method_exists( get_class(), $method ) ) { |
| 283 | continue; |
| 284 | } |
| 285 | |
| 286 | if ( 'get' === substr( $method, 0, 3 ) && 'Attribute' === substr( $method, -9 ) ) { |
| 287 | $key = str_replace( [ 'get', 'Attribute' ], '', $method ); |
| 288 | if ( $key ) { |
| 289 | $pieces = preg_split( '/(?=[A-Z])/', $key ); |
| 290 | $pieces = array_map( 'strtolower', array_filter( $pieces ) ); |
| 291 | $key = implode( '_', $pieces ); |
| 292 | $value = array_key_exists( $key, $this->attributes ) ? $this->attributes[ $key ] : null; |
| 293 | $attributes[ $key ] = $this->{$method}( $value ); |
| 294 | } |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | // Check if any attribute is a model and call toArray. |
| 299 | array_walk_recursive( |
| 300 | $attributes, |
| 301 | function ( &$value ) { |
| 302 | if ( method_exists( $value, 'toArray' ) ) { |
| 303 | $value = $value->toArray(); |
| 304 | } |
| 305 | } |
| 306 | ); |
| 307 | |
| 308 | return $attributes; |
| 309 | } |
| 310 | |
| 311 | /** |
| 312 | * Serialize to json. |
| 313 | * |
| 314 | * @return Array |
| 315 | */ |
| 316 | #[\ReturnTypeWillChange] |
| 317 | public function jsonSerialize() { |
| 318 | return $this->toArray(); |
| 319 | } |
| 320 | |
| 321 | /** |
| 322 | * Get the attribute |
| 323 | * |
| 324 | * @param string $key Attribute name. |
| 325 | * |
| 326 | * @return mixed |
| 327 | */ |
| 328 | public function __get( $key ) { |
| 329 | return $this->getAttribute( $key ); |
| 330 | } |
| 331 | |
| 332 | /** |
| 333 | * Set the attribute |
| 334 | * |
| 335 | * @param string $key Attribute name. |
| 336 | * @param mixed $value Value of attribute. |
| 337 | * |
| 338 | * @return void |
| 339 | */ |
| 340 | public function __set( $key, $value ) { |
| 341 | $this->setAttribute( $key, $value ); |
| 342 | } |
| 343 | |
| 344 | /** |
| 345 | * Determine if the given attribute exists. |
| 346 | * |
| 347 | * @param mixed $offset Name. |
| 348 | * @return bool |
| 349 | */ |
| 350 | #[\ReturnTypeWillChange] |
| 351 | public function offsetExists( $offset ) { |
| 352 | return ! is_null( $this->getAttribute( $offset ) ); |
| 353 | } |
| 354 | |
| 355 | /** |
| 356 | * Get the value for a given offset. |
| 357 | * |
| 358 | * @param mixed $offset Name. |
| 359 | * @return mixed |
| 360 | */ |
| 361 | #[\ReturnTypeWillChange] |
| 362 | public function offsetGet( $offset ) { |
| 363 | return $this->getAttribute( $offset ); |
| 364 | } |
| 365 | |
| 366 | /** |
| 367 | * Set the value for a given offset. |
| 368 | * |
| 369 | * @param mixed $offset Name. |
| 370 | * @param mixed $value Value. |
| 371 | * @return void |
| 372 | */ |
| 373 | #[\ReturnTypeWillChange] |
| 374 | public function offsetSet( $offset, $value ) { |
| 375 | $this->setAttribute( $offset, $value ); |
| 376 | } |
| 377 | |
| 378 | /** |
| 379 | * Unset the value for a given offset. |
| 380 | * |
| 381 | * @param mixed $offset Name. |
| 382 | * @return void |
| 383 | */ |
| 384 | #[\ReturnTypeWillChange] |
| 385 | public function offsetUnset( $offset ) { |
| 386 | unset( $this->attributes[ $offset ] ); |
| 387 | } |
| 388 | |
| 389 | /** |
| 390 | * Determine if an attribute or relation exists on the model. |
| 391 | * |
| 392 | * @param string $key Name. |
| 393 | * @return bool |
| 394 | */ |
| 395 | public function __isset( $key ) { |
| 396 | return $this->offsetExists( $key ); |
| 397 | } |
| 398 | |
| 399 | /** |
| 400 | * Unset an attribute on the model. |
| 401 | * |
| 402 | * @param string $key Name. |
| 403 | * @return void |
| 404 | */ |
| 405 | public function __unset( $key ) { |
| 406 | $this->offsetUnset( $key ); |
| 407 | } |
| 408 | } |
| 409 |