Overview

Classes

  • xu
  • Xu\Foundation\Foundation
  • Xu\Foundation\Jsonable
  • Xu\Model\Collection
  • Xu\Model\Model

Functions

  • xu
  • xu_add_option
  • xu_attachment_url_to_postid
  • xu_cache_delete
  • xu_cache_delete_group
  • xu_cache_get
  • xu_cache_key
  • xu_camel_case
  • xu_clean_attachment_cache
  • xu_clean_nav_menu_cache
  • xu_clean_post_cache
  • xu_contains
  • xu_convert_to_string
  • xu_current_attachment_url_to_postid
  • xu_current_url
  • xu_current_url_to_postid
  • xu_dashify
  • xu_delete_option
  • xu_delete_site_transient
  • xu_delete_transient
  • xu_doing_ajax
  • xu_ends_with
  • xu_get_class_name
  • xu_get_model
  • xu_get_option
  • xu_get_option_name
  • xu_get_option_post_id
  • xu_get_posts
  • xu_get_site_transient
  • xu_get_top_parent_post
  • xu_get_top_parent_post_type
  • xu_get_top_parent_post_type_object
  • xu_get_transient
  • xu_is_empty
  • xu_is_http_method
  • xu_is_json
  • xu_is_post_type
  • xu_is_wp
  • xu_is_xml
  • xu_post_exists
  • xu_register_large_option_post_type
  • xu_snake_case
  • xu_starts_with
  • xu_strip_spaces
  • xu_studly_case
  • xu_update_option
  • xu_url_to_postid
  • xu_valid_personnummer
  • xu_wp_get_attachment_image
  • xu_wp_get_attachment_image_sizes
  • xu_wp_get_attachment_image_src
  • xu_wp_get_attachment_image_srcset
  • xu_wp_get_attachment_url
  • xu_wp_nav_menu
  • Overview
  • Class
  1: <?php
  2: 
  3: namespace Xu\Model;
  4: 
  5: use ArrayAccess;
  6: use ReflectionClass;
  7: use Xu\Foundation\Jsonable;
  8: 
  9: abstract class Model extends Jsonable implements ArrayAccess {
 10: 
 11:     /**
 12:      * Model attributes.
 13:      *
 14:      * @var array
 15:      */
 16:     protected $attributes = [];
 17: 
 18:     /**
 19:      * Determine if a attribute exists or not.
 20:      *
 21:      * @param  string $key
 22:      *
 23:      * @return bool
 24:      */
 25:     public function __isset( $key ) {
 26:         $attributes = $this->get_attributes_array();
 27:         return isset( $attributes[$key] );
 28:     }
 29: 
 30:     /**
 31:      * Get post property by key.
 32:      *
 33:      * @param  string $key
 34:      *
 35:      * @return mixed
 36:      */
 37:     public function __get( $key ) {
 38:         switch ( $key ) {
 39:             case 'id':
 40:             case 'ID':
 41:                 return get_the_ID() ?: null;
 42:             case 'post':
 43:                 return get_post();
 44:             default:
 45:                 break;
 46:         }
 47: 
 48:         if ( $attribute = $this->get( $key ) ) {
 49:             return $attribute;
 50:         }
 51:     }
 52: 
 53:     /**
 54:      * Set attribute value.
 55:      *
 56:      * @param string $key
 57:      * @param mixed  $value
 58:      */
 59:     public function __set( $key, $value ) {
 60:         $this->attributes[$key] = $value;
 61:     }
 62: 
 63:     /**
 64:      * Unset attribute key.
 65:      *
 66:      * @param string $key
 67:      */
 68:     public function __unset( $key ) {
 69:         unset( $this->attributes[$key] );
 70:     }
 71: 
 72:     /**
 73:      * Call method with parameters.
 74:      *
 75:      * @param  string $name
 76:      * @param  array  $parameters
 77:      *
 78:      * @return mixed
 79:      */
 80:     public function __call( $method, array $parameters = [] ) {
 81:         return call_user_func_array( $method, $parameters );
 82:     }
 83: 
 84:     /**
 85:      * Call static method with paramters.
 86:      *
 87:      * @param  string $method
 88:      * @param  array  $parameters
 89:      *
 90:      * @return mixed
 91:      */
 92:     public static function __callStatic( $method, array $parameters = [] ) {
 93:         $instance = new static;
 94:         return call_user_func_array( [$instance, $method], $arguments );
 95:     }
 96: 
 97:     /**
 98:      * Create a collection of models.
 99:      *
100:      * @param  mixed $items
101:      *
102:      * @return array
103:      */
104:     public static function collection( $items = [] ) {
105:         $items = is_array( $items ) ? $items : [$items];
106: 
107:         $items = array_map( function ( $item ) {
108:             if ( $item instanceof Model ) {
109:                 return $item;
110:             }
111: 
112:             if ( ! is_array( $item ) && ! is_object( $item ) ) {
113:                 return;
114:             }
115: 
116:             if ( is_object( $item ) && get_class( $item ) !== 'stdClass' ) {
117:                 return;
118:             }
119: 
120:             return static::create( (array) $item );
121:         }, $items );
122: 
123:         return new Collection( array_filter( $items ) );
124:     }
125: 
126:     /**
127:      * Create a model from attributes.
128:      *
129:      * @param  array $attributes
130:      *
131:      * @return \Xu\Model\Model
132:      */
133:     public static function create( array $attributes = [] ) {
134:         $model = new static;
135:         $model->set_attributes( $attributes );
136:         return $model;
137:     }
138: 
139:     /**
140:      * Get model attribute.
141:      *
142:      * @param  string $key
143:      * @param  mixed  $default
144:      *
145:      * @return mixed
146:      */
147:     public function get( $key, $default = null ) {
148:         if ( empty( $key ) || ! is_string( $key ) ) {
149:             return;
150:         }
151: 
152:         $key        = explode( '.', $key );
153:         $attributes = $this->get_attributes_array();
154: 
155:         foreach ( $key as $part ) {
156:             if ( $attributes instanceof Model === false && ( ! is_array( $attributes ) || ! array_key_exists( $part, $attributes ) ) ) {
157:                 return $default;
158:             }
159: 
160:             $attributes = $attributes[$part];
161:         }
162: 
163:         if ( is_null( $attributes ) ) {
164:             return $default;
165:         }
166: 
167:         return $attributes;
168:     }
169: 
170:     /**
171:      * Get the model attributes.
172:      *
173:      * @return array
174:      */
175:     public function get_attributes() {
176:         return [];
177:     }
178: 
179:     /**
180:      * Get attributes array.
181:      *
182:      * @return array
183:      */
184:     public function get_attributes_array() {
185:         $attributes = $this->get_attributes();
186:         $attributes = is_array( $attributes ) ? $attributes : [];
187:         return array_merge( $attributes, $this->attributes );
188:     }
189: 
190:     /**
191:      * Get a model within a model instance.
192:      *
193:      * @param  string $model
194:      *
195:      * @return mixed
196:      */
197:     public function get_model( $model ) {
198:         return xu_get_model( $model );
199:     }
200: 
201:     /**
202:      * Create a new model instance.
203:      *
204:      * @return mixed
205:      */
206:     public static function model() {
207:         $reflection = new ReflectionClass( static::class );
208:         return $reflection->newInstanceArgs( func_get_args() );
209:     }
210: 
211:     /**
212:      * Determine if the given offset exists.
213:      *
214:      * @param  mixed $offset
215:      *
216:      * @return bool
217:      */
218:     public function offsetExists( $offset ) {
219:         return isset( $this->$offset );
220:     }
221: 
222:     /**
223:      * Get the value for a given offset.
224:      *
225:      * @param  mixed $offset
226:      *
227:      * @return mixed
228:      */
229:     public function offsetGet( $offset ) {
230:         return $this->$offset;
231:     }
232: 
233:     /**
234:      * Set the value for a given offset.
235:      *
236:      * @param mixed $offset
237:      * @param mixed $value
238:      */
239:     public function offsetSet( $offset, $value ) {
240:         $this->$offset = $value;
241:     }
242: 
243:     /**
244:      * Unset the value for a given offset.
245:      *
246:      * @param mixed $offset
247:      */
248:     public function offsetUnset( $offset ) {
249:         unset( $this->$offset );
250:     }
251: 
252:     /**
253:      * Set model attribute.
254:      *
255:      * @param string $key
256:      * @param mixed  $value
257:      */
258:     public function set( $key, $value ) {
259:         $this->$key = $value;
260:     }
261: 
262:     /**
263:      * Set attributes.
264:      *
265:      * @param array $attributes
266:      */
267:     public function set_attributes( array $attributes ) {
268:         foreach ( $attributes as $key => $value ) {
269:             $this->$key = $value;
270:         }
271:     }
272: 
273:     /**
274:      * Convert the model instance to an array.
275:      *
276:      * @return array
277:      */
278:     public function to_array() {
279:         return $this->get_attributes_array();
280:     }
281: }
282: 
API documentation generated by ApiGen