Collection.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace Doctrine\Common\Collections;
  3. use ArrayAccess;
  4. use Closure;
  5. /**
  6. * The missing (SPL) Collection/Array/OrderedMap interface.
  7. *
  8. * A Collection resembles the nature of a regular PHP array. That is,
  9. * it is essentially an <b>ordered map</b> that can also be used
  10. * like a list.
  11. *
  12. * A Collection has an internal iterator just like a PHP array. In addition,
  13. * a Collection can be iterated with external iterators, which is preferable.
  14. * To use an external iterator simply use the foreach language construct to
  15. * iterate over the collection (which calls {@link getIterator()} internally) or
  16. * explicitly retrieve an iterator though {@link getIterator()} which can then be
  17. * used to iterate over the collection.
  18. * You can not rely on the internal iterator of the collection being at a certain
  19. * position unless you explicitly positioned it before. Prefer iteration with
  20. * external iterators.
  21. *
  22. * @psalm-template TKey of array-key
  23. * @psalm-template T
  24. * @template-extends ReadableCollection<TKey, T>
  25. * @template-extends ArrayAccess<TKey, T>
  26. */
  27. interface Collection extends ReadableCollection, ArrayAccess
  28. {
  29. /**
  30. * Adds an element at the end of the collection.
  31. *
  32. * @param mixed $element The element to add.
  33. * @psalm-param T $element
  34. *
  35. * @return true Always TRUE.
  36. */
  37. public function add($element);
  38. /**
  39. * Clears the collection, removing all elements.
  40. *
  41. * @return void
  42. */
  43. public function clear();
  44. /**
  45. * Removes the element at the specified index from the collection.
  46. *
  47. * @param string|int $key The key/index of the element to remove.
  48. * @psalm-param TKey $key
  49. *
  50. * @return mixed The removed element or NULL, if the collection did not contain the element.
  51. * @psalm-return T|null
  52. */
  53. public function remove($key);
  54. /**
  55. * Removes the specified element from the collection, if it is found.
  56. *
  57. * @param mixed $element The element to remove.
  58. * @psalm-param T $element
  59. *
  60. * @return bool TRUE if this collection contained the specified element, FALSE otherwise.
  61. */
  62. public function removeElement($element);
  63. /**
  64. * Sets an element in the collection at the specified key/index.
  65. *
  66. * @param string|int $key The key/index of the element to set.
  67. * @param mixed $value The element to set.
  68. * @psalm-param TKey $key
  69. * @psalm-param T $value
  70. *
  71. * @return void
  72. */
  73. public function set($key, $value);
  74. /**
  75. * {@inheritdoc}
  76. *
  77. * @return Collection<mixed> A collection with the results of the filter operation.
  78. * @psalm-return Collection<TKey, T>
  79. */
  80. public function filter(Closure $p);
  81. /**
  82. * {@inheritdoc}
  83. *
  84. * @return Collection<mixed>[] An array with two elements. The first element contains the collection
  85. * of elements where the predicate returned TRUE, the second element
  86. * contains the collection of elements where the predicate returned FALSE.
  87. * @psalm-return array{0: Collection<TKey, T>, 1: Collection<TKey, T>}
  88. */
  89. public function partition(Closure $p);
  90. }