Collection.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Collection;
  16. use ArrayIterator;
  17. use InvalidArgumentException;
  18. use IteratorIterator;
  19. use Serializable;
  20. use Traversable;
  21. /**
  22. * A collection is an immutable list of elements with a handful of functions to
  23. * iterate, group, transform and extract information from it.
  24. */
  25. class Collection extends IteratorIterator implements CollectionInterface, Serializable
  26. {
  27. use CollectionTrait;
  28. /**
  29. * Constructor. You can provide an array or any traversable object
  30. *
  31. * @param array|\Traversable $items Items.
  32. * @throws \InvalidArgumentException If passed incorrect type for items.
  33. */
  34. public function __construct($items)
  35. {
  36. if (is_array($items)) {
  37. $items = new ArrayIterator($items);
  38. }
  39. if (!($items instanceof Traversable)) {
  40. $msg = 'Only an array or \Traversable is allowed for Collection';
  41. throw new InvalidArgumentException($msg);
  42. }
  43. parent::__construct($items);
  44. }
  45. /**
  46. * Returns a string representation of this object that can be used
  47. * to reconstruct it
  48. *
  49. * @return string
  50. */
  51. public function serialize()
  52. {
  53. return serialize($this->buffered());
  54. }
  55. /**
  56. * Unserializes the passed string and rebuilds the Collection instance
  57. *
  58. * @param string $collection The serialized collection
  59. * @return void
  60. */
  61. public function unserialize($collection)
  62. {
  63. $this->__construct(unserialize($collection));
  64. }
  65. /**
  66. * {@inheritDoc}
  67. *
  68. * @return int
  69. */
  70. public function count()
  71. {
  72. $traversable = $this->optimizeUnwrap();
  73. if (is_array($traversable)) {
  74. return count($traversable);
  75. }
  76. return iterator_count($traversable);
  77. }
  78. /**
  79. * {@inheritDoc}
  80. *
  81. * @return int
  82. */
  83. public function countKeys()
  84. {
  85. return count($this->toArray());
  86. }
  87. /**
  88. * Returns an array that can be used to describe the internal state of this
  89. * object.
  90. *
  91. * @return array
  92. */
  93. public function __debugInfo()
  94. {
  95. return [
  96. 'count' => $this->count(),
  97. ];
  98. }
  99. }