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