PluginProbe ʕ •ᴥ•ʔ
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments / 3.0.3
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments v3.0.3
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 1.0.6 1.1.0 1.1.1 1.1.10 1.1.11 1.1.12 1.1.13 1.1.14 1.1.15 1.1.16 1.1.17 1.1.18 1.1.19 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 1.1.9 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.11.0 1.11.1 1.11.2 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.7.0 1.7.1 1.7.2 1.8.0 1.8.1 1.8.2 1.8.3 1.8.4 1.8.5 1.9.0 1.9.1 1.9.2 1.9.3 1.9.4 1.9.5 2.0.0 2.0.1 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.10.0 2.10.1 2.11.0 2.11.1 2.11.2 2.11.3 2.11.4 2.12.0 2.13.0 2.14.0 2.14.1 2.15.0 2.15.1 2.16.0 2.16.1 2.16.2 2.16.3 2.17.0 2.17.1 2.17.2 2.18.0 2.19.0 2.19.2 2.19.3 2.19.4 2.2.0 2.2.1 2.20.0 2.20.1 2.20.2 2.20.3 2.20.4 2.20.5 2.20.6 2.21.0 2.22.0 2.22.1 2.23.0 2.24.0 2.25.0 2.25.1 2.25.2 2.26.0 2.27.0 2.27.1 2.28.0 2.29.0 2.29.1 2.29.2 2.29.3 2.29.4 2.3.0 2.3.1 2.30.0 2.31.0 2.31.1 2.31.2 2.31.3 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.40.0 2.40.1 2.5.0 2.5.1 2.5.2 2.6.0 2.6.1 2.6.2 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.8.0 2.8.1 2.8.2 2.8.3 2.8.4 2.9.0 3.0.0 3.0.0-RC1 3.0.0-RC2 3.0.0-beta1 3.0.0-beta2 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.10.0 3.10.1 3.11.0 3.12.0 3.13.0 3.13.1 3.13.2 3.13.3 3.13.4 3.14.0 3.15.0 3.15.1 3.15.2 3.15.3 3.15.4 3.15.5 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.16.5 3.16.6 3.16.7 3.16.8 3.17.0 3.17.1 3.17.2 3.17.3 3.17.4 3.17.5 3.17.6 3.18.0 3.19.0 3.19.1 3.19.2 3.2.0 3.2.1 3.2.2 3.20.0 3.20.1 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.4.3 3.5.0 3.5.1 3.5.2 3.5.3 3.6.0 3.6.1 3.6.2 3.7.0 3.7.1 3.7.2 3.7.3 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.9.0 4.0.0 4.0.1 4.0.2 4.0.3 trunk 4.1.0 0.2.19.1 4.1.1 1.0.0 4.2.0 1.0.1 1.0.2
surecart / app / src / Models / Traits / HasAttributes.php
surecart / app / src / Models / Traits Last commit date
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