Collection.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Collection;
  16. use ArrayIterator;
  17. use InvalidArgumentException;
  18. use IteratorIterator;
  19. use LogicException;
  20. use Serializable;
  21. use Traversable;
  22. /**
  23. * A collection is an immutable list of elements with a handful of functions to
  24. * iterate, group, transform and extract information from it.
  25. */
  26. class Collection extends IteratorIterator implements CollectionInterface, Serializable
  27. {
  28. use CollectionTrait;
  29. /**
  30. * Constructor. You can provide an array or any traversable object
  31. *
  32. * @param array|\Traversable $items Items.
  33. * @throws \InvalidArgumentException If passed incorrect type for items.
  34. */
  35. public function __construct($items)
  36. {
  37. if (is_array($items)) {
  38. $items = new ArrayIterator($items);
  39. }
  40. if (!($items instanceof Traversable)) {
  41. $msg = 'Only an array or \Traversable is allowed for Collection';
  42. throw new InvalidArgumentException($msg);
  43. }
  44. parent::__construct($items);
  45. }
  46. /**
  47. * Returns a string representation of this object that can be used
  48. * to reconstruct it
  49. *
  50. * @return string
  51. */
  52. public function serialize()
  53. {
  54. return serialize($this->buffered());
  55. }
  56. /**
  57. * Unserializes the passed string and rebuilds the Collection instance
  58. *
  59. * @param string $collection The serialized collection
  60. * @return void
  61. */
  62. public function unserialize($collection)
  63. {
  64. $this->__construct(unserialize($collection));
  65. }
  66. /**
  67. * Throws an exception.
  68. *
  69. * Issuing a count on a Collection can have many side effects, some making the
  70. * Collection unusable after the count operation.
  71. *
  72. * @return void
  73. * @throws \LogicException
  74. */
  75. public function count()
  76. {
  77. throw new LogicException('You cannot issue a count on a Collection.');
  78. }
  79. /**
  80. * Returns an array that can be used to describe the internal state of this
  81. * object.
  82. *
  83. * @return array
  84. */
  85. public function __debugInfo()
  86. {
  87. return [
  88. 'count' => iterator_count($this),
  89. ];
  90. }
  91. }