Complex classes like Collection often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Collection, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 36 | class Collection implements ArrayAccess, Iterator, Countable, JsonSerializable |
||
| 37 | { |
||
| 38 | /** @var array The items for this collection */ |
||
| 39 | protected $items; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Collection constructor. |
||
| 43 | * |
||
| 44 | * Although most methods in this class are more forgiving and accept anything that is traversable rather than |
||
| 45 | * strictly an array, the constructor is an exception. It expects an array. If you have an Array-ish object and it |
||
| 46 | * is traversable, you may use the factory method instead to generate a collection from it. |
||
| 47 | * |
||
| 48 | * @param array $items The items to include in the collection |
||
| 49 | */ |
||
| 50 | 101 | public function __construct(array $items = []) |
|
| 55 | |||
| 56 | /** |
||
| 57 | * Generate a collection from any iterable |
||
| 58 | * |
||
| 59 | * This is the method used internally to generate new collections. This allows for this class to be extended if |
||
| 60 | * necessary. This way, the child class will use its own factory method to generate new collections (or otherwise |
||
| 61 | * use this one). |
||
| 62 | * |
||
| 63 | * @param array|Traversable $items The items to include in the collection |
||
| 64 | * |
||
| 65 | * @return Collection |
||
| 66 | */ |
||
| 67 | 38 | public static function factory($items = null) |
|
| 71 | |||
| 72 | /** |
||
| 73 | * Get collection as an array |
||
| 74 | * |
||
| 75 | * @return array |
||
| 76 | */ |
||
| 77 | 51 | public function toArray() |
|
| 81 | |||
| 82 | /** |
||
| 83 | * Determine if collection has a given key |
||
| 84 | * |
||
| 85 | * @param mixed $key The key to check for |
||
| 86 | * |
||
| 87 | * @return bool |
||
| 88 | */ |
||
| 89 | 51 | public function has($key) |
|
| 93 | |||
| 94 | /** |
||
| 95 | * Determine if collection has a value at given position |
||
| 96 | * |
||
| 97 | * If the $position argument is positive, counting will start at the beginning and start from one (rather than zero). |
||
| 98 | * If $position is negative, counting will start at the end and work backwards. This is not the same as array |
||
| 99 | * indexing, as that begins from zero. |
||
| 100 | * |
||
| 101 | * @param int $position The numeric position to check for a value at |
||
| 102 | * |
||
| 103 | * @return bool |
||
| 104 | */ |
||
| 105 | 2 | public function hasValueAt($position) |
|
| 114 | |||
| 115 | /** |
||
| 116 | * Get key at given position |
||
| 117 | * |
||
| 118 | * If the $position argument is positive, counting will start at the beginning and start from one (rather than zero). |
||
| 119 | * If $position is negative, counting will start at the end and work backwards. If an item exists at the specified |
||
| 120 | * position, its key will be returned. Otherwise a RuntimeException will be thrown. |
||
| 121 | * |
||
| 122 | * @param int $position The numeric position to get a key at |
||
| 123 | * |
||
| 124 | * @return mixed |
||
| 125 | * |
||
| 126 | * @throws RuntimeException |
||
| 127 | */ |
||
| 128 | 6 | public function getKeyAt($position) |
|
| 142 | |||
| 143 | /** |
||
| 144 | * Get value at given position |
||
| 145 | * |
||
| 146 | * If the $position argument is positive, counting will start at the beginning and start from one (rather than zero). |
||
| 147 | * If $position is negative, counting will start at the end and work backwards. If an item exists at the specified |
||
| 148 | * position, its value will be returned. Otherwise a RuntimeException will be thrown. |
||
| 149 | * |
||
| 150 | * @param int $position The numeric position to get a value at |
||
| 151 | * |
||
| 152 | * @return mixed |
||
| 153 | * |
||
| 154 | * @throws RuntimeException |
||
| 155 | */ |
||
| 156 | 2 | public function getValueAt($position) |
|
| 160 | |||
| 161 | /** |
||
| 162 | * Get the key of first item exactly equal to $item |
||
| 163 | * |
||
| 164 | * Searches the collection for an item exactly equal to $item, returning its key if found. If a callback is provided |
||
| 165 | * rather than a value, it will be passed the conventional three arguments ($value, $key, $index) and returning true |
||
| 166 | * from this callback would be considered a "match". If no match is found, a RuntimeException will be thrown. |
||
| 167 | * |
||
| 168 | * @param mixed|callable $item The value to look for or a callback |
||
| 169 | * |
||
| 170 | * @throws RuntimeException |
||
| 171 | * |
||
| 172 | * @return mixed |
||
| 173 | */ |
||
| 174 | 3 | public function keyOf($item) |
|
| 189 | |||
| 190 | /** |
||
| 191 | * Get the numeric index of first item exactly equal to $item |
||
| 192 | * |
||
| 193 | * Searches the collection for an item exactly equal to $item, returning its numeric index if found. If a callback |
||
| 194 | * is provided rather than a value, it will be passed the conventional three arguments ($value, $key, $index) and |
||
| 195 | * returning true from this callback would be considered a "match". If no match is found, a RuntimeException will be |
||
| 196 | * thrown. |
||
| 197 | * |
||
| 198 | * @param mixed|callable $item The value to look for or a callback |
||
| 199 | * |
||
| 200 | * @throws RuntimeException |
||
| 201 | * |
||
| 202 | * @return int |
||
| 203 | */ |
||
| 204 | 3 | public function indexOf($item) |
|
| 222 | |||
| 223 | /** |
||
| 224 | * Get item by key |
||
| 225 | * |
||
| 226 | * Fetches an item from the collection by key. If no item is found with the given key, a default may be provided as |
||
| 227 | * the second argument. If no default is provided, null will be returned instead. |
||
| 228 | * |
||
| 229 | * @param mixed $key The key of the item you want returned |
||
| 230 | * @param mixed $default A default value to return if key does not exist |
||
| 231 | * |
||
| 232 | * @return mixed |
||
| 233 | */ |
||
| 234 | 8 | public function get($key, $default = null) |
|
| 242 | |||
| 243 | /** |
||
| 244 | * Add an item with no regard to key |
||
| 245 | * |
||
| 246 | * Simply adds a value at the end of the collection. A numeric index will be created automatically. |
||
| 247 | * |
||
| 248 | * @param mixed $value The value to add to the collection |
||
| 249 | * |
||
| 250 | * @return self |
||
| 251 | */ |
||
| 252 | 8 | public function add($value) |
|
| 258 | |||
| 259 | /** |
||
| 260 | * Assign a value to the given key |
||
| 261 | * |
||
| 262 | * Sets the specified key to the specified value. By default the key will be overwritten if it already exists, but |
||
| 263 | * this behavior may be changed by setting the third parameter ($overwrite) to false. |
||
| 264 | * |
||
| 265 | * @param mixed $key The key to assign a value to |
||
| 266 | * @param mixed $value The value to assign to $key |
||
| 267 | * @param bool $overwrite Whether to overwrite existing values (default is true) |
||
| 268 | * |
||
| 269 | * @return self |
||
| 270 | */ |
||
| 271 | 15 | public function set($key, $value, $overwrite = true) |
|
| 279 | |||
| 280 | /** |
||
| 281 | * Delete an item by key |
||
| 282 | * |
||
| 283 | * Remove the item at the given key from the collection. |
||
| 284 | * |
||
| 285 | * @param mixed $key The key of the item to remove |
||
| 286 | * |
||
| 287 | * @return self |
||
| 288 | */ |
||
| 289 | 3 | public function delete($key) |
|
| 295 | |||
| 296 | /** |
||
| 297 | * Clear (remove) all items from the collection. |
||
| 298 | * |
||
| 299 | * @return self |
||
| 300 | */ |
||
| 301 | 2 | public function clear() |
|
| 307 | |||
| 308 | /** |
||
| 309 | * Determine if collection contains given value |
||
| 310 | * |
||
| 311 | * Checks the collection for an item exactly equal to $value. If $value is a callback function, it will be passed |
||
| 312 | * the typical arguments ($value, $key, $index) and a true return value will count as a match. |
||
| 313 | * |
||
| 314 | * If $key argument is provided, key must match it as well. By default key is not required. |
||
| 315 | * |
||
| 316 | * @param mixed|callable $value The value to check for or a callback function |
||
| 317 | * @param mixed $key The key to check for in addition to the value (optional) |
||
| 318 | * |
||
| 319 | * @return bool |
||
| 320 | */ |
||
| 321 | 4 | public function contains($value, $key = null) |
|
| 339 | |||
| 340 | /** |
||
| 341 | * Pull an item out of the collection and return it |
||
| 342 | * |
||
| 343 | * @param mixed $key The key whose value should be removed and returned |
||
| 344 | * |
||
| 345 | * @return mixed |
||
| 346 | */ |
||
| 347 | 1 | public function pull($key) |
|
| 355 | |||
| 356 | /** |
||
| 357 | * Join collection items using a delimiter |
||
| 358 | * |
||
| 359 | * Similar to implode() or join(), this method will attempt to return every item in the collection delimited |
||
| 360 | * (separated) by the specified character(s). |
||
| 361 | * |
||
| 362 | * @param string $delim The character(s) to delimit (separate) the results with |
||
| 363 | * |
||
| 364 | * @return string |
||
| 365 | */ |
||
| 366 | 1 | public function join($delim = '') |
|
| 370 | |||
| 371 | /** |
||
| 372 | * Determine if collection is empty (has no items) |
||
| 373 | * |
||
| 374 | * @return bool |
||
| 375 | */ |
||
| 376 | 5 | public function isEmpty() |
|
| 380 | |||
| 381 | /** |
||
| 382 | * Get new collection with only values |
||
| 383 | * |
||
| 384 | * Return a new collection with only the current collection's values. The keys will be indexed numerically from zero |
||
| 385 | * |
||
| 386 | * @return Collection |
||
| 387 | */ |
||
| 388 | 2 | public function values() |
|
| 392 | |||
| 393 | /** |
||
| 394 | * Get new collection with only keys |
||
| 395 | * |
||
| 396 | * Return a new collection with only the current collection's keys as its values. |
||
| 397 | * |
||
| 398 | * @return Collection |
||
| 399 | */ |
||
| 400 | 1 | public function keys() |
|
| 404 | |||
| 405 | /** |
||
| 406 | * Get a collection of key/value pairs |
||
| 407 | * |
||
| 408 | * Returns a new collection containing arrays of key/value pairs in the format [key, value]. |
||
| 409 | * |
||
| 410 | * @return Collection |
||
| 411 | */ |
||
| 412 | 1 | public function pairs() |
|
| 418 | |||
| 419 | /** |
||
| 420 | * Get a collection with order reversed |
||
| 421 | * |
||
| 422 | * @return Collection |
||
| 423 | */ |
||
| 424 | 6 | public function reverse() |
|
| 428 | |||
| 429 | /** |
||
| 430 | * Get a collection with keys and values flipped. |
||
| 431 | * |
||
| 432 | * Returns a new collection containing the keys as values and the values as keys. |
||
| 433 | * |
||
| 434 | * @return Collection |
||
| 435 | */ |
||
| 436 | 1 | public function flip() |
|
| 444 | |||
| 445 | /** |
||
| 446 | * Shuffle (randomize) the order of this collection's values (in-place) |
||
| 447 | * |
||
| 448 | * @return Collection |
||
| 449 | */ |
||
| 450 | 1 | public function shuffle() |
|
| 455 | |||
| 456 | /** |
||
| 457 | * Get a random value from the collection |
||
| 458 | * |
||
| 459 | * @return mixed |
||
| 460 | */ |
||
| 461 | 1 | public function random() |
|
| 465 | |||
| 466 | /** |
||
| 467 | * Sort the collection by value (in-place) |
||
| 468 | * |
||
| 469 | * Sorts the collection by value using the provided algorithm (which can be either the name of a native php function |
||
| 470 | * or a callable). |
||
| 471 | * |
||
| 472 | * @note The sorting methods are exceptions to the usual callback signature. The callback for this method accepts |
||
| 473 | * the standard arguments for sorting algorithms ( string $str1 , string $str2 ) and should return an integer. |
||
| 474 | * |
||
| 475 | * @see http://php.net/manual/en/function.strcmp.php |
||
| 476 | * |
||
| 477 | * @param callable $alg The sorting algorithm (defaults to strcmp) |
||
| 478 | * |
||
| 479 | * @return self |
||
| 480 | */ |
||
| 481 | 2 | public function sort(callable $alg = null) |
|
| 491 | |||
| 492 | /** |
||
| 493 | * Sort the collection by key (in-place) |
||
| 494 | * |
||
| 495 | * Sorts the collection by key using the provided algorithm (which can be either the name of a native php function |
||
| 496 | * or a callable). |
||
| 497 | * |
||
| 498 | * @note The sorting methods are exceptions to the usual callback signature. The callback for this method accepts |
||
| 499 | * the standard arguments for sorting algorithms ( string $str1 , string $str2 ) and should return an integer. |
||
| 500 | * |
||
| 501 | * @see http://php.net/manual/en/function.strcmp.php |
||
| 502 | * |
||
| 503 | * @param callable $alg The sorting algorithm (defaults to strcmp) |
||
| 504 | * |
||
| 505 | * @return self |
||
| 506 | */ |
||
| 507 | 2 | public function ksort(callable $alg = null) |
|
| 517 | |||
| 518 | /** |
||
| 519 | * Append items to collection without regard to key |
||
| 520 | * |
||
| 521 | * Much like Collection::add(), except that it accepts multiple items to append rather than just one. |
||
| 522 | * |
||
| 523 | * @param array|Traversable $items A list of values to append to the collection |
||
| 524 | * |
||
| 525 | * @return self |
||
| 526 | */ |
||
| 527 | 2 | public function append($items) |
|
| 539 | |||
| 540 | /** |
||
| 541 | * Return first item or first item where callback returns true |
||
| 542 | * |
||
| 543 | * Returns the first item in the collection. If a callback is provided, it will accept the standard arguments |
||
| 544 | * ($value, $key, $index) and returning true will be considered a "match". |
||
| 545 | * |
||
| 546 | * @param callable|null $callback A callback to compare items with (optional) |
||
| 547 | * |
||
| 548 | * @return mixed|null |
||
| 549 | */ |
||
| 550 | 8 | public function first(callable $callback = null) |
|
| 561 | |||
| 562 | /** |
||
| 563 | * Return last item or last item where callback returns true |
||
| 564 | * |
||
| 565 | * Returns the last item in the collection. If a callback is provided, it will accept the standard arguments |
||
| 566 | * ($value, $key, $index) and returning true will be considered a "match". |
||
| 567 | * |
||
| 568 | * @param callable|null $callback A callback to compare items with (optional) |
||
| 569 | * |
||
| 570 | * @return mixed|null |
||
| 571 | */ |
||
| 572 | 3 | public function last(callable $callback = null) |
|
| 576 | |||
| 577 | /** |
||
| 578 | * Create a new collection by applying a callback to each item in the collection |
||
| 579 | * |
||
| 580 | * The callback for this method should accept the standard arguments ($value, $key, $index). It will be called once |
||
| 581 | * for every item in the collection and a new collection will be created with the results. |
||
| 582 | * |
||
| 583 | * @note It is worth noting that keys will be preserved in the resulting collection, so if you do not want this |
||
| 584 | * behavior, simply call values() on the resulting collection and it will be indexed numerically. |
||
| 585 | * |
||
| 586 | * @param callable $callback A callback that is applied to every item in the collection |
||
| 587 | * |
||
| 588 | * @return Collection |
||
| 589 | */ |
||
| 590 | 4 | public function map(callable $callback) |
|
| 601 | |||
| 602 | /** |
||
| 603 | * Combine collection with another collection/array/traversable |
||
| 604 | * |
||
| 605 | * Using this collection's keys, and the incoming collection's values, a new collection is created and returned. |
||
| 606 | * |
||
| 607 | * @param array|Traversable $items The values to combine with this collection's keys |
||
| 608 | * |
||
| 609 | * @return Collection |
||
| 610 | */ |
||
| 611 | 5 | public function combine($items) |
|
| 624 | |||
| 625 | /** |
||
| 626 | * Get a new collection with only distinct values |
||
| 627 | * |
||
| 628 | * @return Collection |
||
| 629 | */ |
||
| 630 | 1 | public function distinct() |
|
| 641 | |||
| 642 | /** |
||
| 643 | * Remove all duplicate values from collection (in-place) |
||
| 644 | * |
||
| 645 | * @return Collection |
||
| 646 | */ |
||
| 647 | 1 | public function deduplicate() |
|
| 653 | |||
| 654 | /** |
||
| 655 | * Get new collection with only filtered values |
||
| 656 | * |
||
| 657 | * Loops through every item in the collection, applying the given callback and creating a new collection with only |
||
| 658 | * those items which return true from the callback. The callback should accept the standard arguments |
||
| 659 | * ($value, $key, $index). If no callback is provided, items with "truthy" values will be kept. |
||
| 660 | * |
||
| 661 | * @param callable $callback A callback function used to determine which items are kept (optional) |
||
| 662 | * |
||
| 663 | * @return Collection |
||
| 664 | */ |
||
| 665 | 2 | public function filter(callable $callback = null) |
|
| 683 | |||
| 684 | /** |
||
| 685 | * Fold collection into a single value (a.k.a. reduce) |
||
| 686 | * |
||
| 687 | * Apply a callback function to each item in the collection, passing the result to the next call until only a single |
||
| 688 | * value remains. The arguments provided to this callback are ($folded, $val, $key, $index) where $folded is the |
||
| 689 | * result of the previous call (or if the first call it is equal to the $initial param). |
||
| 690 | * |
||
| 691 | * @param callable $callback The callback function used to "fold" or "reduce" the collection into a single value |
||
| 692 | * @param mixed $initial The (optional) initial value to pass to the callback |
||
| 693 | * |
||
| 694 | * @return mixed |
||
| 695 | */ |
||
| 696 | 1 | public function fold(callable $callback, $initial = null) |
|
| 706 | |||
| 707 | /** |
||
| 708 | * Return a merge of this collection and $items |
||
| 709 | * |
||
| 710 | * Returns a new collection with a merge of this collection and $items. Values from $items will overwrite values in |
||
| 711 | * the current collection. |
||
| 712 | * |
||
| 713 | * @param array|Traversable $items The items to merge with the collection |
||
| 714 | * |
||
| 715 | * @return Collection |
||
| 716 | */ |
||
| 717 | 3 | public function merge($items) |
|
| 730 | |||
| 731 | /** |
||
| 732 | * Create a new collection with a union of this collection and $items |
||
| 733 | * |
||
| 734 | * This method is similar to merge, except that existing values will not be overwritten. |
||
| 735 | * |
||
| 736 | * @param $items |
||
| 737 | */ |
||
| 738 | 2 | public function union($items) |
|
| 751 | |||
| 752 | /** |
||
| 753 | * Apply a callback function to each item in the collection passively |
||
| 754 | * |
||
| 755 | * To stop looping through the items in the collection, return false from the callback. |
||
| 756 | * |
||
| 757 | * @param callable $callback The callback to use on each item in the collection |
||
| 758 | * |
||
| 759 | * @return self |
||
| 760 | */ |
||
| 761 | 2 | public function each(callable $callback) |
|
| 772 | |||
| 773 | /** |
||
| 774 | * Assert callback returns $expected value for each item in collection. |
||
| 775 | * |
||
| 776 | * This method will loop over each item in the collection, passing them to the callback. If the callback doesn't |
||
| 777 | * return $expected value for every item in the collection, it will return false. |
||
| 778 | * |
||
| 779 | * @param callable $callback Assertion callback |
||
| 780 | * @param bool $expected Expected value from callback |
||
| 781 | * |
||
| 782 | * @return bool |
||
| 783 | */ |
||
| 784 | 2 | public function assert(callable $callback, $expected = true) |
|
| 795 | |||
| 796 | /** |
||
| 797 | * Pipe collection through a callback |
||
| 798 | * |
||
| 799 | * Simply passes the collection as an argument to the given callback. |
||
| 800 | * |
||
| 801 | * @param callable $callback The callback function (passed only one arg, the collection itself) |
||
| 802 | * |
||
| 803 | * @return mixed |
||
| 804 | */ |
||
| 805 | 1 | public function pipe(callable $callback) |
|
| 809 | |||
| 810 | /** |
||
| 811 | * Get new collection in chunks of $size |
||
| 812 | * |
||
| 813 | * Creates a new collection of arrays of $size length. The remainder items will be placed at the end. |
||
| 814 | * |
||
| 815 | * @param int $size The size of the arrays you want returned |
||
| 816 | * |
||
| 817 | * @return Collection |
||
| 818 | */ |
||
| 819 | 2 | public function chunk($size) |
|
| 823 | |||
| 824 | /** |
||
| 825 | * Get a new collection of $count chunks |
||
| 826 | * |
||
| 827 | * Returns a collection of $count number of equally-sized arrays, placing remainders at the end. |
||
| 828 | * |
||
| 829 | * @param int $count The number of arrays you want returned |
||
| 830 | * |
||
| 831 | * @return Collection |
||
| 832 | */ |
||
| 833 | 1 | public function split($count = 1) |
|
| 837 | |||
| 838 | /** |
||
| 839 | * Get a slice of this collection. |
||
| 840 | * |
||
| 841 | * Returns a collection with a slice of this collection's items, starting at $offset and continuing until $length |
||
| 842 | * |
||
| 843 | * @param int $offset The offset at which you want the slice to begin |
||
| 844 | * @param int|null $length The length of the slice (number of items) |
||
| 845 | * |
||
| 846 | * @return Collection |
||
| 847 | */ |
||
| 848 | 1 | public function slice($offset, $length = null) |
|
| 852 | |||
| 853 | /** |
||
| 854 | * Get collection with only differing items |
||
| 855 | * |
||
| 856 | * Returns a collection containing only the items not present in *both* this collection and $items. |
||
| 857 | * |
||
| 858 | * @param array|Traversable $items The items to compare with |
||
| 859 | * |
||
| 860 | * @return Collection |
||
| 861 | */ |
||
| 862 | 1 | public function diff($items) |
|
| 866 | |||
| 867 | /** |
||
| 868 | * Get collection with only differing items (by key) |
||
| 869 | * |
||
| 870 | * Returns a collection containing only the values whose keys are not present in *both* this collection and $items. |
||
| 871 | * |
||
| 872 | * @param array|Traversable $items The items to compare with |
||
| 873 | * |
||
| 874 | * @return Collection |
||
| 875 | */ |
||
| 876 | 2 | public function kdiff($items) |
|
| 880 | |||
| 881 | /** |
||
| 882 | * Get collection with only intersecting items |
||
| 883 | * |
||
| 884 | * Returns a collection containing only the values present in *both* this collection and $items |
||
| 885 | * |
||
| 886 | * @param array|Traversable $items The items to compare with |
||
| 887 | * |
||
| 888 | * @return Collection |
||
| 889 | */ |
||
| 890 | 1 | public function intersect($items) |
|
| 894 | |||
| 895 | /** |
||
| 896 | * Get collection with only intersecting items (by key) |
||
| 897 | * |
||
| 898 | * Returns a collection containing only the values whose keys are present in *both* this collection and $items |
||
| 899 | * |
||
| 900 | * @param array|Traversable $items The items to compare with |
||
| 901 | * |
||
| 902 | * @return Collection |
||
| 903 | */ |
||
| 904 | 1 | public function kintersect($items) |
|
| 908 | |||
| 909 | /** |
||
| 910 | * Remove last item in collection and return it |
||
| 911 | * |
||
| 912 | * @return mixed |
||
| 913 | */ |
||
| 914 | 1 | public function pop() |
|
| 918 | |||
| 919 | /** |
||
| 920 | * Remove first item in collection and return it |
||
| 921 | * |
||
| 922 | * If the collection is numerically indexed, this method will re-index it from 0 after returning the item. |
||
| 923 | * |
||
| 924 | * @return mixed |
||
| 925 | */ |
||
| 926 | 2 | public function shift() |
|
| 930 | |||
| 931 | /** |
||
| 932 | * Add item to the end of the collection |
||
| 933 | * |
||
| 934 | * @note This method is no different than add() but I included it for consistency's sake since I have the others |
||
| 935 | * |
||
| 936 | * @param mixed $item The item to add to the collection |
||
| 937 | * |
||
| 938 | * @return self |
||
| 939 | */ |
||
| 940 | 1 | public function push($item) |
|
| 944 | |||
| 945 | /** |
||
| 946 | * Add item to the beginning of the collection |
||
| 947 | * |
||
| 948 | * The collection will be re-indexed if it has numeric keys. |
||
| 949 | * |
||
| 950 | * @param mixed $item The item to add to the collection |
||
| 951 | * |
||
| 952 | * @return self |
||
| 953 | */ |
||
| 954 | 5 | public function unshift($item) |
|
| 960 | |||
| 961 | /** |
||
| 962 | * Get new collection padded to specified $size with $value |
||
| 963 | * |
||
| 964 | * Using $value, pad the collection to specified $size. If $size is smaller or equal to the size of the collection, |
||
| 965 | * then no padding takes place. If $size is positive, padding is added to the end, while if negative, padding will |
||
| 966 | * be added to the beginning. |
||
| 967 | * |
||
| 968 | * @param int $size The number of items collection should have |
||
| 969 | * @param mixed $value The value to pad with |
||
| 970 | * |
||
| 971 | * @return Collection |
||
| 972 | */ |
||
| 973 | 3 | public function pad($size, $value = null) |
|
| 986 | |||
| 987 | /** |
||
| 988 | * Partition collection into two collections using a callback |
||
| 989 | * |
||
| 990 | * Iterates over each element in the collection with a callback. Items where callback returns true are placed in one |
||
| 991 | * collection and the rest in another. Finally, the two collections are placed in an array and returned for easy use |
||
| 992 | * with the list() function. ( `list($a, $b) = $col->partition(function($val, $key, $index) {})` ) |
||
| 993 | * |
||
| 994 | * @param callable $callback The comparison callback |
||
| 995 | * |
||
| 996 | * @return Collection[] |
||
| 997 | */ |
||
| 998 | 2 | public function partition(callable $callback) |
|
| 1014 | |||
| 1015 | /** |
||
| 1016 | * Get column values by key |
||
| 1017 | * |
||
| 1018 | * This method expects the collection's data to be tabular in nature (two-dimensional and for the rows to have |
||
| 1019 | * consistently named keys). If the data is not structured this way, it will do the best it can but it is not meant |
||
| 1020 | * for unstructured, non-tabular data so don't expect consistent results. |
||
| 1021 | * |
||
| 1022 | * @param string|int $column The key of the column you want to get |
||
| 1023 | * |
||
| 1024 | * @return Collection |
||
| 1025 | */ |
||
| 1026 | 3 | public function getColumn($column) |
|
| 1030 | |||
| 1031 | /** |
||
| 1032 | * Is collection tabular? |
||
| 1033 | * |
||
| 1034 | * Returns true if the data in the collection is tabular in nature, meaning it is at least two-dimensional and each |
||
| 1035 | * row contains the same number of values with the same keys. |
||
| 1036 | * |
||
| 1037 | * @return bool |
||
| 1038 | */ |
||
| 1039 | 1 | public function isTabular() |
|
| 1051 | |||
| 1052 | /** ++++ ++++ **/ |
||
| 1053 | /** ++ Interface Compliance ++ **/ |
||
| 1054 | /** ++++ ++++ **/ |
||
| 1055 | |||
| 1056 | /** |
||
| 1057 | * JSON serialize |
||
| 1058 | * |
||
| 1059 | * @ignore |
||
| 1060 | * |
||
| 1061 | * @return array |
||
| 1062 | */ |
||
| 1063 | 1 | public function jsonSerialize() |
|
| 1067 | |||
| 1068 | /** ++++ ++++ **/ |
||
| 1069 | /** ++ Array Access Methods ++ **/ |
||
| 1070 | /** ++++ ++++ **/ |
||
| 1071 | |||
| 1072 | /** |
||
| 1073 | * Does offset exist? |
||
| 1074 | * |
||
| 1075 | * @ignore |
||
| 1076 | * |
||
| 1077 | * @param mixed $offset |
||
| 1078 | * |
||
| 1079 | * @return bool |
||
| 1080 | */ |
||
| 1081 | 1 | public function offsetExists($offset) |
|
| 1085 | |||
| 1086 | /** |
||
| 1087 | * Get item at offset |
||
| 1088 | * |
||
| 1089 | * @ignore |
||
| 1090 | * |
||
| 1091 | * @param mixed $offset |
||
| 1092 | * |
||
| 1093 | * @return mixed |
||
| 1094 | */ |
||
| 1095 | 2 | public function offsetGet($offset) |
|
| 1103 | |||
| 1104 | /** |
||
| 1105 | * Unset item at offset |
||
| 1106 | * |
||
| 1107 | * @ignore |
||
| 1108 | * |
||
| 1109 | * @param mixed $offset |
||
| 1110 | * |
||
| 1111 | * @return void |
||
| 1112 | */ |
||
| 1113 | 1 | public function offsetUnset($offset) |
|
| 1117 | |||
| 1118 | /** |
||
| 1119 | * Set item at offset |
||
| 1120 | * |
||
| 1121 | * @ignore |
||
| 1122 | * |
||
| 1123 | * @param mixed $offset |
||
| 1124 | * @param mixed $value |
||
| 1125 | * |
||
| 1126 | * @return self |
||
| 1127 | */ |
||
| 1128 | 1 | public function offsetSet($offset, $value) |
|
| 1136 | |||
| 1137 | /** ++++ ++++ **/ |
||
| 1138 | /** ++ Iterator Methods ++ **/ |
||
| 1139 | /** ++++ ++++ **/ |
||
| 1140 | |||
| 1141 | /** |
||
| 1142 | * @ignore |
||
| 1143 | */ |
||
| 1144 | 39 | public function current() |
|
| 1148 | |||
| 1149 | /** |
||
| 1150 | * @ignore |
||
| 1151 | */ |
||
| 1152 | 39 | public function key() |
|
| 1156 | |||
| 1157 | /** |
||
| 1158 | * @ignore |
||
| 1159 | */ |
||
| 1160 | 38 | public function next() |
|
| 1164 | |||
| 1165 | /** |
||
| 1166 | * @ignore |
||
| 1167 | */ |
||
| 1168 | 101 | public function rewind() |
|
| 1172 | |||
| 1173 | /** |
||
| 1174 | * @ignore |
||
| 1175 | */ |
||
| 1176 | 40 | public function valid() |
|
| 1180 | |||
| 1181 | /** ++++ ++++ **/ |
||
| 1182 | /** ++ Countable Method ++ **/ |
||
| 1183 | /** ++++ ++++ **/ |
||
| 1184 | |||
| 1185 | /** |
||
| 1186 | * Get number of items in the collection |
||
| 1187 | * |
||
| 1188 | * @return int |
||
| 1189 | */ |
||
| 1190 | 11 | public function count() |
|
| 1194 | } |