| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPDeveloper\QueryBuilder; |
| 4 |
defined( 'ABSPATH' ) || exit(); |
| 5 |
|
| 6 |
use ArrayAccess; |
| 7 |
use IteratorAggregate; |
| 8 |
use WPDeveloper\QueryBuilder\Interfaces\Arrayable; |
| 9 |
use WPDeveloper\QueryBuilder\Interfaces\JSONable; |
| 10 |
use WPDeveloper\QueryBuilder\Interfaces\Stringable; |
| 11 |
|
| 12 |
class Collection implements ArrayAccess, Arrayable, IteratorAggregate, JSONable, Stringable { |
| 13 |
/** |
| 14 |
* The items contained in the collection. |
| 15 |
* |
| 16 |
* @var array |
| 17 |
*/ |
| 18 |
protected $items = array(); |
| 19 |
|
| 20 |
/** |
| 21 |
* Create a new collection. |
| 22 |
* |
| 23 |
* @param mixed $items |
| 24 |
* |
| 25 |
* @return void |
| 26 |
*/ |
| 27 |
public function __construct( $items = array() ) { |
| 28 |
$items = is_null( $items ) ? [] : $this->getArrayableItems( $items ); |
| 29 |
|
| 30 |
$this->items = (array) $items; |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Create a new collection instance if the value isn't one already. |
| 35 |
* |
| 36 |
* @param mixed $items |
| 37 |
* |
| 38 |
* @return static |
| 39 |
*/ |
| 40 |
public static function make( $items = null ) { |
| 41 |
return new static( $items ); |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Get all of the items in the collection. |
| 46 |
* |
| 47 |
* @return array |
| 48 |
*/ |
| 49 |
public function all() { |
| 50 |
return $this->items; |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
/** |
| 55 |
* Diff the collection with the given items. |
| 56 |
* |
| 57 |
* @param Arrayable|array $items |
| 58 |
* |
| 59 |
* @return static |
| 60 |
*/ |
| 61 |
public function diff( $items ) { |
| 62 |
return new static( array_diff( $this->items, $this->getArrayableItems( $items ) ) ); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Execute a callback over each item. |
| 67 |
* |
| 68 |
* @param callable $callback |
| 69 |
* |
| 70 |
* @return static |
| 71 |
*/ |
| 72 |
public function each( callable $callback ) { |
| 73 |
return new static( array_map( $callback, $this->items ) ); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Run a filter over each of the items. |
| 78 |
* |
| 79 |
* @param callable $callback |
| 80 |
* |
| 81 |
* @return static |
| 82 |
*/ |
| 83 |
public function filter( callable $callback ) { |
| 84 |
return new static( array_filter( $this->items, $callback ) ); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Filter items by the given key value pair. |
| 89 |
* |
| 90 |
* @param string $key |
| 91 |
* @param mixed $value |
| 92 |
* @param bool $strict |
| 93 |
* |
| 94 |
* @return static |
| 95 |
*/ |
| 96 |
public function where( $key, $value, $strict = true ) { |
| 97 |
return $this->filter( function ( $item ) use ( $key, $value, $strict ) { |
| 98 |
return $strict ? self::data_get( $item, $key ) === $value |
| 99 |
: self::data_get( $item, $key ) == $value; |
| 100 |
} ); |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Filter items by the given key value pair using loose comparison. |
| 105 |
* |
| 106 |
* @param string $key |
| 107 |
* @param mixed $value |
| 108 |
* |
| 109 |
* @return static |
| 110 |
*/ |
| 111 |
public function whereLoose( $key, $value ) { |
| 112 |
return $this->where( $key, $value, false ); |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Flip the items in the collection. |
| 117 |
* |
| 118 |
* @return static |
| 119 |
*/ |
| 120 |
public function flip() { |
| 121 |
return new static( array_flip( $this->items ) ); |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Remove an item from the collection by key. |
| 126 |
* |
| 127 |
* @param mixed $key |
| 128 |
* |
| 129 |
* @return void |
| 130 |
*/ |
| 131 |
public function forget( $key ) { |
| 132 |
$this->offsetUnset( $key ); |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Get an item from the collection by key. |
| 137 |
* |
| 138 |
* @param mixed $key |
| 139 |
* @param mixed $default |
| 140 |
* |
| 141 |
* @return mixed |
| 142 |
*/ |
| 143 |
public function get( $key, $default = null ) { |
| 144 |
if ( $this->offsetExists( $key ) ) { |
| 145 |
return $this->items[ $key ]; |
| 146 |
} |
| 147 |
|
| 148 |
return $default instanceof \Closure ? $default() : $default; |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Determine if an item exists in the collection by key. |
| 153 |
* |
| 154 |
* @param mixed $key |
| 155 |
* |
| 156 |
* @return bool |
| 157 |
*/ |
| 158 |
public function has( $key ) { |
| 159 |
return $this->offsetExists( $key ); |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* Intersect the collection with the given items. |
| 164 |
* |
| 165 |
* @param Arrayable|array $items |
| 166 |
* |
| 167 |
* @return static |
| 168 |
*/ |
| 169 |
public function intersect( $items ) { |
| 170 |
return new static( array_intersect( $this->items, $this->getArrayableItems( $items ) ) ); |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* Determine if the collection is empty or not. |
| 175 |
* |
| 176 |
* @return bool |
| 177 |
*/ |
| 178 |
public function isEmpty() { |
| 179 |
return empty( $this->items ); |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* Determine if the given value is callable, but not a string. |
| 184 |
* |
| 185 |
* @param mixed $value |
| 186 |
* |
| 187 |
* @return bool |
| 188 |
*/ |
| 189 |
protected function useAsCallable( $value ) { |
| 190 |
return ! is_string( $value ) && is_callable( $value ); |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Get the keys of the collection items. |
| 195 |
* |
| 196 |
* @return static |
| 197 |
*/ |
| 198 |
public function keys() { |
| 199 |
return new static( array_keys( $this->items ) ); |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* Get the last item from the collection. |
| 204 |
* |
| 205 |
* @return mixed|null |
| 206 |
*/ |
| 207 |
public function last() { |
| 208 |
return count( $this->items ) > 0 ? end( $this->items ) : null; |
| 209 |
} |
| 210 |
|
| 211 |
/** |
| 212 |
* Run a map over each of the items. |
| 213 |
* |
| 214 |
* @param callable $callback |
| 215 |
* |
| 216 |
* @return static |
| 217 |
*/ |
| 218 |
public function map( callable $callback ) { |
| 219 |
return new static( array_map( $callback, $this->items, array_keys( $this->items ) ) ); |
| 220 |
} |
| 221 |
|
| 222 |
/** |
| 223 |
* Merge the collection with the given items. |
| 224 |
* |
| 225 |
* @param Arrayable|array $items |
| 226 |
* |
| 227 |
* @return static |
| 228 |
*/ |
| 229 |
public function merge( $items ) { |
| 230 |
return new static( array_merge( $this->items, $this->getArrayableItems( $items ) ) ); |
| 231 |
} |
| 232 |
|
| 233 |
/** |
| 234 |
* Get and remove the last item from the collection. |
| 235 |
* |
| 236 |
* @return mixed|null |
| 237 |
*/ |
| 238 |
public function pop() { |
| 239 |
return array_pop( $this->items ); |
| 240 |
} |
| 241 |
|
| 242 |
/** |
| 243 |
* Push an item onto the beginning of the collection. |
| 244 |
* |
| 245 |
* @param mixed $value |
| 246 |
* |
| 247 |
* @return void |
| 248 |
*/ |
| 249 |
public function prepend( $value ) { |
| 250 |
array_unshift( $this->items, $value ); |
| 251 |
} |
| 252 |
|
| 253 |
/** |
| 254 |
* Push an item onto the end of the collection. |
| 255 |
* |
| 256 |
* @param mixed $value |
| 257 |
* |
| 258 |
* @return void |
| 259 |
*/ |
| 260 |
public function push( $value ) { |
| 261 |
$this->offsetSet( null, $value ); |
| 262 |
} |
| 263 |
|
| 264 |
/** |
| 265 |
* Put an item in the collection by key. |
| 266 |
* |
| 267 |
* @param mixed $key |
| 268 |
* @param mixed $value |
| 269 |
* |
| 270 |
* @return void |
| 271 |
*/ |
| 272 |
public function put( $key, $value ) { |
| 273 |
$this->offsetSet( $key, $value ); |
| 274 |
} |
| 275 |
|
| 276 |
/** |
| 277 |
* Get one or more items randomly from the collection. |
| 278 |
* |
| 279 |
* @param int $amount |
| 280 |
* |
| 281 |
* @return mixed |
| 282 |
*/ |
| 283 |
public function random( $amount = 1 ) { |
| 284 |
if ( $this->isEmpty() ) { |
| 285 |
return; |
| 286 |
} |
| 287 |
|
| 288 |
$keys = array_rand( $this->items, $amount ); |
| 289 |
|
| 290 |
return is_array( $keys ) ? array_intersect_key( $this->items, array_flip( $keys ) ) : $this->items[ $keys ]; |
| 291 |
} |
| 292 |
|
| 293 |
/** |
| 294 |
* Reduce the collection to a single value. |
| 295 |
* |
| 296 |
* @param callable $callback |
| 297 |
* @param mixed $initial |
| 298 |
* |
| 299 |
* @return mixed |
| 300 |
*/ |
| 301 |
public function reduce( callable $callback, $initial = null ) { |
| 302 |
return array_reduce( $this->items, $callback, $initial ); |
| 303 |
} |
| 304 |
|
| 305 |
/** |
| 306 |
* Create a collection of all elements that do not pass a given truth test. |
| 307 |
* |
| 308 |
* @param callable|mixed $callback |
| 309 |
* |
| 310 |
* @return static |
| 311 |
*/ |
| 312 |
public function reject( $callback ) { |
| 313 |
if ( $this->useAsCallable( $callback ) ) { |
| 314 |
return $this->filter( function ( $item ) use ( $callback ) { |
| 315 |
return ! $callback( $item ); |
| 316 |
} ); |
| 317 |
} |
| 318 |
|
| 319 |
return $this->filter( function ( $item ) use ( $callback ) { |
| 320 |
return $item != $callback; |
| 321 |
} ); |
| 322 |
} |
| 323 |
|
| 324 |
/** |
| 325 |
* Reverse items order. |
| 326 |
* |
| 327 |
* @return static |
| 328 |
*/ |
| 329 |
public function reverse() { |
| 330 |
return new static( array_reverse( $this->items ) ); |
| 331 |
} |
| 332 |
|
| 333 |
/** |
| 334 |
* Search the collection for a given value and return the corresponding key if successful. |
| 335 |
* |
| 336 |
* @param mixed $value |
| 337 |
* @param bool $strict |
| 338 |
* |
| 339 |
* @return mixed |
| 340 |
*/ |
| 341 |
public function search( $value, $strict = false ) { |
| 342 |
if ( ! $this->useAsCallable( $value ) ) { |
| 343 |
return array_search( $value, $this->items, $strict ); |
| 344 |
} |
| 345 |
|
| 346 |
foreach ( $this->items as $key => $item ) { |
| 347 |
if ( $value( $item, $key ) ) { |
| 348 |
return $key; |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
return false; |
| 353 |
} |
| 354 |
|
| 355 |
/** |
| 356 |
* Get and remove the first item from the collection. |
| 357 |
* |
| 358 |
* @return mixed|null |
| 359 |
*/ |
| 360 |
public function shift() { |
| 361 |
return array_shift( $this->items ); |
| 362 |
} |
| 363 |
|
| 364 |
/** |
| 365 |
* Shuffle the items in the collection. |
| 366 |
* |
| 367 |
* @return $this |
| 368 |
*/ |
| 369 |
public function shuffle() { |
| 370 |
shuffle( $this->items ); |
| 371 |
|
| 372 |
return $this; |
| 373 |
} |
| 374 |
|
| 375 |
/** |
| 376 |
* Slice the underlying collection array. |
| 377 |
* |
| 378 |
* @param int $offset |
| 379 |
* @param int $length |
| 380 |
* @param bool $preserveKeys |
| 381 |
* |
| 382 |
* @return static |
| 383 |
*/ |
| 384 |
public function slice( $offset, $length = null, $preserveKeys = false ) { |
| 385 |
return new static( array_slice( $this->items, $offset, $length, $preserveKeys ) ); |
| 386 |
} |
| 387 |
|
| 388 |
/** |
| 389 |
* Chunk the underlying collection array. |
| 390 |
* |
| 391 |
* @param int $size |
| 392 |
* @param bool $preserveKeys |
| 393 |
* |
| 394 |
* @return static |
| 395 |
*/ |
| 396 |
public function chunk( $size, $preserveKeys = false ) { |
| 397 |
$chunks = []; |
| 398 |
|
| 399 |
foreach ( array_chunk( $this->items, $size, $preserveKeys ) as $chunk ) { |
| 400 |
$chunks[] = new static( $chunk ); |
| 401 |
} |
| 402 |
|
| 403 |
return new static( $chunks ); |
| 404 |
} |
| 405 |
|
| 406 |
/** |
| 407 |
* Sort through each item with a callback. |
| 408 |
* |
| 409 |
* @param callable $callback |
| 410 |
* |
| 411 |
* @return $this |
| 412 |
*/ |
| 413 |
public function sort( callable $callback ) { |
| 414 |
uasort( $this->items, $callback ); |
| 415 |
|
| 416 |
return $this; |
| 417 |
} |
| 418 |
|
| 419 |
/** |
| 420 |
* Splice portion of the underlying collection array. |
| 421 |
* |
| 422 |
* @param int $offset |
| 423 |
* @param int $length |
| 424 |
* @param mixed $replacement |
| 425 |
* |
| 426 |
* @return static |
| 427 |
*/ |
| 428 |
public function splice( $offset, $length = 0, $replacement = [] ) { |
| 429 |
return new static( array_splice( $this->items, $offset, $length, $replacement ) ); |
| 430 |
} |
| 431 |
|
| 432 |
/** |
| 433 |
* Take the first or last {$limit} items. |
| 434 |
* |
| 435 |
* @param int $limit |
| 436 |
* |
| 437 |
* @return static |
| 438 |
*/ |
| 439 |
public function take( $limit = null ) { |
| 440 |
if ( $limit < 0 ) { |
| 441 |
return $this->slice( $limit, abs( $limit ) ); |
| 442 |
} |
| 443 |
|
| 444 |
return $this->slice( 0, $limit ); |
| 445 |
} |
| 446 |
|
| 447 |
/** |
| 448 |
* Transform each item in the collection using a callback. |
| 449 |
* |
| 450 |
* @param callable $callback |
| 451 |
* |
| 452 |
* @return $this |
| 453 |
*/ |
| 454 |
public function transform( callable $callback ) { |
| 455 |
$this->items = array_map( $callback, $this->items ); |
| 456 |
|
| 457 |
return $this; |
| 458 |
} |
| 459 |
|
| 460 |
/** |
| 461 |
* Return only unique items from the collection array. |
| 462 |
* |
| 463 |
* @return static |
| 464 |
*/ |
| 465 |
public function unique() { |
| 466 |
return new static( array_unique( $this->items ) ); |
| 467 |
} |
| 468 |
|
| 469 |
/** |
| 470 |
* Reset the keys on the underlying array. |
| 471 |
* |
| 472 |
* @return static |
| 473 |
*/ |
| 474 |
public function values() { |
| 475 |
return new static( array_values( $this->items ) ); |
| 476 |
} |
| 477 |
|
| 478 |
/** |
| 479 |
* Count the number of items in the collection. |
| 480 |
* |
| 481 |
* @return int |
| 482 |
*/ |
| 483 |
public function count() { |
| 484 |
return count( $this->items ); |
| 485 |
} |
| 486 |
|
| 487 |
/** |
| 488 |
* Determine if an item exists at an offset. |
| 489 |
* |
| 490 |
* @param mixed $key |
| 491 |
* |
| 492 |
* @return bool |
| 493 |
*/ |
| 494 |
public function offsetExists( $key ) { |
| 495 |
return array_key_exists( $key, $this->items ); |
| 496 |
} |
| 497 |
|
| 498 |
/** |
| 499 |
* Get an item at a given offset. |
| 500 |
* |
| 501 |
* @param mixed $key |
| 502 |
* |
| 503 |
* @return mixed |
| 504 |
*/ |
| 505 |
public function offsetGet( $key ) { |
| 506 |
return $this->items[ $key ]; |
| 507 |
} |
| 508 |
|
| 509 |
/** |
| 510 |
* Set the item at a given offset. |
| 511 |
* |
| 512 |
* @param mixed $key |
| 513 |
* @param mixed $value |
| 514 |
* |
| 515 |
* @return void |
| 516 |
*/ |
| 517 |
public function offsetSet( $key, $value ) { |
| 518 |
if ( is_null( $key ) ) { |
| 519 |
$this->items[] = $value; |
| 520 |
} else { |
| 521 |
$this->items[ $key ] = $value; |
| 522 |
} |
| 523 |
} |
| 524 |
|
| 525 |
/** |
| 526 |
* Unset the item at a given offset. |
| 527 |
* |
| 528 |
* @param string $key |
| 529 |
* |
| 530 |
* @return void |
| 531 |
*/ |
| 532 |
public function offsetUnset( $key ) { |
| 533 |
unset( $this->items[ $key ] ); |
| 534 |
} |
| 535 |
|
| 536 |
|
| 537 |
/** |
| 538 |
* Get an iterator for the items. |
| 539 |
* |
| 540 |
* @return \ArrayIterator |
| 541 |
*/ |
| 542 |
public function getIterator() { |
| 543 |
return new \ArrayIterator( $this->items ); |
| 544 |
} |
| 545 |
|
| 546 |
/** |
| 547 |
* Returns collection as pure array. |
| 548 |
* Does depth array casting. |
| 549 |
* @return array |
| 550 |
* @since 1.0.2 |
| 551 |
* |
| 552 |
*/ |
| 553 |
public function __toArray() { |
| 554 |
$output = []; |
| 555 |
$value = null; |
| 556 |
foreach ( $this as $key => $value ) { |
| 557 |
$output[ $key ] = ! is_object( $value ) |
| 558 |
? $value |
| 559 |
: ( method_exists( $value, '__toArray' ) |
| 560 |
? $value->__toArray() |
| 561 |
: (array) $value |
| 562 |
); |
| 563 |
} |
| 564 |
|
| 565 |
return $output; |
| 566 |
} |
| 567 |
|
| 568 |
/** |
| 569 |
* Returns collection as pure array. |
| 570 |
* Does depth array casting. |
| 571 |
* @return array |
| 572 |
* @since 1.0.2 |
| 573 |
* |
| 574 |
*/ |
| 575 |
public function toArray() { |
| 576 |
return $this->__toArray(); |
| 577 |
} |
| 578 |
|
| 579 |
/** |
| 580 |
* Returns collection as a string. |
| 581 |
* |
| 582 |
* @param string |
| 583 |
* |
| 584 |
* @since 1.0.2 |
| 585 |
* |
| 586 |
*/ |
| 587 |
public function __toString() { |
| 588 |
return json_encode( $this->__toArray() ); |
| 589 |
} |
| 590 |
|
| 591 |
/** |
| 592 |
* Returns object as JSON string. |
| 593 |
* |
| 594 |
* @param int $options JSON encoding options. See @link. |
| 595 |
* @param int $depth JSON encoding depth. See @link. |
| 596 |
* |
| 597 |
* @return string |
| 598 |
* @link http://php.net/manual/en/function.json-encode.php |
| 599 |
* |
| 600 |
* @since 1.0.2 |
| 601 |
* |
| 602 |
*/ |
| 603 |
public function __toJSON( $options = 0, $depth = 512 ) { |
| 604 |
return json_encode( $this->__toArray(), $options, $depth ); |
| 605 |
} |
| 606 |
|
| 607 |
/** |
| 608 |
* Returns object as JSON string. |
| 609 |
* |
| 610 |
* @param int $options JSON encoding options. See @link. |
| 611 |
* @param int $depth JSON encoding depth. See @link. |
| 612 |
* |
| 613 |
* @return string |
| 614 |
* @link http://php.net/manual/en/function.json-encode.php |
| 615 |
* |
| 616 |
* @since 1.0.2 |
| 617 |
* |
| 618 |
*/ |
| 619 |
public function toJSON( $options = 0, $depth = 512 ) { |
| 620 |
return $this->__toJSON( $options, $depth ); |
| 621 |
} |
| 622 |
|
| 623 |
/** |
| 624 |
* @param $target |
| 625 |
* @param $key |
| 626 |
* @param null $default |
| 627 |
* |
| 628 |
* @return array |
| 629 |
* @since |
| 630 |
*/ |
| 631 |
public static function data_get( $target, $key, $default = null ) { |
| 632 |
if ( is_null( $key ) ) { |
| 633 |
return $target; |
| 634 |
} |
| 635 |
|
| 636 |
$key = is_array( $key ) ? $key : explode( '.', $key ); |
| 637 |
|
| 638 |
foreach ( $key as $i => $segment ) { |
| 639 |
unset( $key[ $i ] ); |
| 640 |
|
| 641 |
if ( is_null( $segment ) ) { |
| 642 |
return $target; |
| 643 |
} |
| 644 |
|
| 645 |
if ( $segment === '*' ) { |
| 646 |
if ( $target instanceof Collection ) { |
| 647 |
$target = $target->all(); |
| 648 |
} elseif ( ! is_array( $target ) ) { |
| 649 |
return $default instanceof \Closure ? $default() : $default; |
| 650 |
} |
| 651 |
$result = []; |
| 652 |
|
| 653 |
foreach ( $target as $item ) { |
| 654 |
$result[] = self::data_get( $item, $key ); |
| 655 |
} |
| 656 |
|
| 657 |
return in_array( '*', $key ) ? $result : $result; |
| 658 |
} |
| 659 |
|
| 660 |
if ( array_key_exists( $segment, $target ) ) { |
| 661 |
$target = $target[ $segment ]; |
| 662 |
} elseif ( is_object( $target ) && isset( $target->{$segment} ) ) { |
| 663 |
$target = $target->{$segment}; |
| 664 |
} else { |
| 665 |
return $default instanceof \Closure ? $default() : $default; |
| 666 |
} |
| 667 |
} |
| 668 |
|
| 669 |
return $target; |
| 670 |
} |
| 671 |
|
| 672 |
/** |
| 673 |
* Results array of items from Collection or Arrayable. |
| 674 |
* |
| 675 |
* @param $items |
| 676 |
* |
| 677 |
* @return mixed |
| 678 |
* @since 1.0.0 |
| 679 |
*/ |
| 680 |
protected function getArrayableItems( $items ) { |
| 681 |
if ( $items instanceof Collection ) { |
| 682 |
$items = $items->all(); |
| 683 |
} elseif ( $items instanceof Arrayable ) { |
| 684 |
$items = $items->toArray(); |
| 685 |
} |
| 686 |
|
| 687 |
return $items; |
| 688 |
} |
| 689 |
} |