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: class Collection extends Jsonable implements ArrayAccess {
 10: 
 11:     /**
 12:      * Collection items.
 13:      *
 14:      * @var array
 15:      */
 16:     protected $items = [];
 17: 
 18:     /**
 19:      * The constructor.
 20:      *
 21:      * @param array $items
 22:      */
 23:     public function __construct( array $items = [] ) {
 24:         $this->items = $items;
 25:     }
 26: 
 27:     /**
 28:      * Get all models in the collection.
 29:      *
 30:      * @return array
 31:      */
 32:     public function all() {
 33:         return $this->items;
 34:     }
 35: 
 36:     /**
 37:      * Get number of items that exists in the collection.
 38:      *
 39:      * @return int
 40:      */
 41:     public function count() {
 42:         return count( $this->items );
 43:     }
 44: 
 45:     /**
 46:      * Get item in collection.
 47:      *
 48:      * @param  string $offset
 49:      *
 50:      * @return mixed
 51:      */
 52:     public function eq( $offset ) {
 53:         return $this->offsetGet( $offset );
 54:     }
 55: 
 56:     /**
 57:      * Create a collection of all elements that do pass
 58:      * the given truth test.
 59:      *
 60:      * @param  callable|string $callback
 61:      * @param  mixed $value
 62:      *
 63:      * @return array
 64:      */
 65:     public function filter( $callback, $value = null ) {
 66:         if ( ! is_callable( $callback ) ) {
 67:             $callback = function ( $item ) use ( $callback, $value ) {
 68:                 return $item[$callback] === $value;
 69:             };
 70:         }
 71: 
 72:         return new static( array_values( array_filter( $this->items, $callback ) ) );
 73:     }
 74: 
 75:     /**
 76:      * Find attribute value by key.
 77:      *
 78:      * @param  mixed  $attributes
 79:      * @param  string $key
 80:      * @param  mxied  $default
 81:      *
 82:      * @return mixed
 83:      */
 84:     protected function find_in_item( $attributes, $key, $default = null ) {
 85:         if ( empty( $key ) || ! is_string( $key ) ) {
 86:             return;
 87:         }
 88: 
 89:         $key = explode( '.', $key );
 90: 
 91:         foreach ( $key as $part ) {
 92:             if ( is_object( $attributes ) && get_class( $attributes ) === 'stdClass' ) {
 93:                 $attributes = (array) $attributes;
 94:             }
 95: 
 96:             if (
 97:                 $attributes instanceof Model === false
 98:                 &&
 99:                 ( ! is_array( $attributes ) || ! array_key_exists( $part, $attributes ) )
100:             ) {
101:                 return $default;
102:             }
103: 
104:             $attributes = $attributes[$part];
105:         }
106: 
107:         if ( is_null( $attributes ) ) {
108:             return $default;
109:         }
110: 
111:         return $attributes;
112:     }
113: 
114:     /**
115:      * Get the first item in the collection.
116:      *
117:      * @param callable $callback
118:      *
119:      * @return mixed
120:      */
121:     public function first( callable $callback = null ) {
122:         if ( empty( $callback ) ) {
123:             return $this->items[0];
124:         }
125: 
126:         if ( $items = $this->filter( $callback ) ) {
127:             return $items->first();
128:         }
129:     }
130: 
131:     /**
132:      * Determine if the collection is empty or not.
133:      *
134:      * @return bool
135:      */
136:     public function is_empty() {
137:         return empty( $this->items );
138:     }
139: 
140:     /**
141:      * Get the last item in the collection.
142:      *
143:      * @param callable $callback
144:      *
145:      * @return mixed
146:      */
147:     public function last( callable $callback = null ) {
148:         if ( empty( $callback ) ) {
149:             return $this->items[count( $this->items ) - 1];
150:         }
151: 
152:         if ( $items = $this->filter( $callback ) ) {
153:             return $items->last();
154:         }
155:     }
156: 
157:     /**
158:      * Run map over each of the items.
159:      *
160:      * @param  callable $callback
161:      *
162:      * @return \Xu\Model\Collection
163:      */
164:     public function map( callable $callback ) {
165:         $keys  = array_keys( $this->items );
166:         $items = array_map( $callback, $this->items, $keys );
167:         return new static( array_combine( $keys, $items ) );
168:     }
169: 
170:     /**
171:      * Determine if the given offset exists.
172:      *
173:      * @param  mixed $offset
174:      *
175:      * @return bool
176:      */
177:     public function offsetExists( $offset ) {
178:         return isset( $this->items[$offset] );
179:     }
180: 
181:     /**
182:      * Get the value for a given offset.
183:      *
184:      * @param  mixed $offset
185:      *
186:      * @return mixed
187:      */
188:     public function offsetGet( $offset ) {
189:         return $this->items[$offset];
190:     }
191: 
192:     /**
193:      * Set the value for a given offset.
194:      *
195:      * @param mixed $offset
196:      * @param mixed $value
197:      */
198:     public function offsetSet( $offset, $value ) {
199:         $this->items[$offset] = $value;
200:     }
201: 
202:     /**
203:      * Unset the value for a given offset.
204:      *
205:      * @param mixed $offset
206:      */
207:     public function offsetUnset( $offset ) {
208:         unset( $this->items[$offset] );
209:     }
210: 
211:     /**
212:      * Pluck a certain field out of each item in a list.
213:      *
214:      * @param  string $key
215:      *
216:      * @return array
217:      */
218:     public function pluck( $key ) {
219:         return wp_list_pluck( $this->items, $key );
220:     }
221: 
222:     /**
223:      * Removes and return the last item from the collection.
224:      *
225:      * @return mixed
226:      */
227:     public function pop() {
228:         return array_pop( $this->items );
229:     }
230: 
231:     /**
232:      * Create a collection of all elements that do not pass
233:      * the given truth test.
234:      *
235:      * @param  callable|string $callback
236:      * @param  mixed $value
237:      *
238:      * @return \Xu\Model\Collection
239:      */
240:     public function reject( $callback, $value = null ) {
241:         if ( ! is_callable( $callback ) ) {
242:             $callback = function ( $item ) use ( $callback, $value ) {
243:                 return $item[$callback] === $value;
244:             };
245:         }
246: 
247:         return $this->filter( function ( $item ) use ( $callback ) {
248:             return ! $callback( $item );
249:         } );
250:     }
251: 
252:     /**
253:      * Reverse the collection items.
254:      *
255:      * @return array
256:      */
257:     public function reverse() {
258:         return new static( array_reverse( $this->items ) );
259:     }
260: 
261:     /**
262:      * Removes and return the first item from the collection.
263:      *
264:      * @return mixed
265:      */
266:     public function shift() {
267:         return array_shift( $this->items );
268:     }
269: 
270:     /**
271:      * Extract a slice of the collection.
272:      *
273:      * @param  int  $offset
274:      * @param  int  $length
275:      * @param  bool $preserve_keys
276:      *
277:      * @return \Xu\Model\Collection
278:      */
279:     public function slice( $offset, $length = null, $preserve_keys = false ) {
280:         return new static( array_slice( $this->items, $offset ) );
281:     }
282: 
283:     /**
284:      * Sort through each item in the collection.
285:      *
286:      * @param  callable $callback
287:      *
288:      * @return \Xu\Model\Collection
289:      */
290:     public function sort( callable $callback ) {
291:         $items = $this->all();
292:         uasort( $items, $callback );
293:         return new static( array_values( $items ) );
294:     }
295: 
296:     /**
297:      * Filter models by the given key value pair.
298:      *
299:      * @param  string $key
300:      * @param  mixed  $value
301:      *
302:      * @return \Xu\Model\Collection
303:      */
304:     public function where( $key, $value = null ) {
305:         if ( is_callable( $key ) ) {
306:             return $this->filter( $key );
307:         }
308: 
309:         return $this->filter( function ( $item ) use ( $key, $value ) {
310:             return $this->find_in_item( $item, $key ) === $value;
311:         } );
312:     }
313: 
314:     /**
315:      * Convert the collection items to an array.
316:      *
317:      * @return array
318:      */
319:     public function to_array() {
320:         return $this->items;
321:     }
322: }
323: 
API documentation generated by ApiGen