| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Doctrine\Common\Collections; |
| 6 |
|
| 7 |
use Closure; |
| 8 |
use Countable; |
| 9 |
use IteratorAggregate; |
| 10 |
|
| 11 |
/** |
| 12 |
* @phpstan-template TKey of array-key |
| 13 |
* @template-covariant T |
| 14 |
* @template-extends IteratorAggregate<TKey, T> |
| 15 |
*/ |
| 16 |
interface ReadableCollection extends Countable, IteratorAggregate |
| 17 |
{ |
| 18 |
/** |
| 19 |
* Checks whether an element is contained in the collection. |
| 20 |
* This is an O(n) operation, where n is the size of the collection. |
| 21 |
* |
| 22 |
* @param mixed $element The element to search for. |
| 23 |
* @phpstan-param TMaybeContained $element |
| 24 |
* |
| 25 |
* @return bool TRUE if the collection contains the element, FALSE otherwise. |
| 26 |
* @phpstan-return (TMaybeContained is T ? bool : false) |
| 27 |
* |
| 28 |
* @template TMaybeContained |
| 29 |
*/ |
| 30 |
public function contains(mixed $element); |
| 31 |
|
| 32 |
/** |
| 33 |
* Checks whether the collection is empty (contains no elements). |
| 34 |
* |
| 35 |
* @return bool TRUE if the collection is empty, FALSE otherwise. |
| 36 |
*/ |
| 37 |
public function isEmpty(); |
| 38 |
|
| 39 |
/** |
| 40 |
* Checks whether the collection contains an element with the specified key/index. |
| 41 |
* |
| 42 |
* @param string|int $key The key/index to check for. |
| 43 |
* @phpstan-param TKey $key |
| 44 |
* |
| 45 |
* @return bool TRUE if the collection contains an element with the specified key/index, |
| 46 |
* FALSE otherwise. |
| 47 |
*/ |
| 48 |
public function containsKey(string|int $key); |
| 49 |
|
| 50 |
/** |
| 51 |
* Gets the element at the specified key/index. |
| 52 |
* |
| 53 |
* @param string|int $key The key/index of the element to retrieve. |
| 54 |
* @phpstan-param TKey $key |
| 55 |
* |
| 56 |
* @return mixed |
| 57 |
* @phpstan-return T|null |
| 58 |
*/ |
| 59 |
public function get(string|int $key); |
| 60 |
|
| 61 |
/** |
| 62 |
* Gets all keys/indices of the collection. |
| 63 |
* |
| 64 |
* @return int[]|string[] The keys/indices of the collection, in the order of the corresponding |
| 65 |
* elements in the collection. |
| 66 |
* @phpstan-return list<TKey> |
| 67 |
*/ |
| 68 |
public function getKeys(); |
| 69 |
|
| 70 |
/** |
| 71 |
* Gets all values of the collection. |
| 72 |
* |
| 73 |
* @return mixed[] The values of all elements in the collection, in the |
| 74 |
* order they appear in the collection. |
| 75 |
* @phpstan-return list<T> |
| 76 |
*/ |
| 77 |
public function getValues(); |
| 78 |
|
| 79 |
/** |
| 80 |
* Gets a native PHP array representation of the collection. |
| 81 |
* |
| 82 |
* @return mixed[] |
| 83 |
* @phpstan-return array<TKey,T> |
| 84 |
*/ |
| 85 |
public function toArray(); |
| 86 |
|
| 87 |
/** |
| 88 |
* Sets the internal iterator to the first element in the collection and returns this element. |
| 89 |
* |
| 90 |
* @return mixed |
| 91 |
* @phpstan-return T|false |
| 92 |
*/ |
| 93 |
public function first(); |
| 94 |
|
| 95 |
/** |
| 96 |
* Sets the internal iterator to the last element in the collection and returns this element. |
| 97 |
* |
| 98 |
* @return mixed |
| 99 |
* @phpstan-return T|false |
| 100 |
*/ |
| 101 |
public function last(); |
| 102 |
|
| 103 |
/** |
| 104 |
* Gets the key/index of the element at the current iterator position. |
| 105 |
* |
| 106 |
* @return int|string|null |
| 107 |
* @phpstan-return TKey|null |
| 108 |
*/ |
| 109 |
public function key(); |
| 110 |
|
| 111 |
/** |
| 112 |
* Gets the element of the collection at the current iterator position. |
| 113 |
* |
| 114 |
* @return mixed |
| 115 |
* @phpstan-return T|false |
| 116 |
*/ |
| 117 |
public function current(); |
| 118 |
|
| 119 |
/** |
| 120 |
* Moves the internal iterator position to the next element and returns this element. |
| 121 |
* |
| 122 |
* @return mixed |
| 123 |
* @phpstan-return T|false |
| 124 |
*/ |
| 125 |
public function next(); |
| 126 |
|
| 127 |
/** |
| 128 |
* Extracts a slice of $length elements starting at position $offset from the Collection. |
| 129 |
* |
| 130 |
* If $length is null it returns all elements from $offset to the end of the Collection. |
| 131 |
* Keys have to be preserved by this method. Calling this method will only return the |
| 132 |
* selected slice and NOT change the elements contained in the collection slice is called on. |
| 133 |
* |
| 134 |
* @param int $offset The offset to start from. |
| 135 |
* @param int|null $length The maximum number of elements to return, or null for no limit. |
| 136 |
* |
| 137 |
* @return mixed[] |
| 138 |
* @phpstan-return array<TKey,T> |
| 139 |
*/ |
| 140 |
public function slice(int $offset, int|null $length = null); |
| 141 |
|
| 142 |
/** |
| 143 |
* Tests for the existence of an element that satisfies the given predicate. |
| 144 |
* |
| 145 |
* @param Closure $p The predicate. |
| 146 |
* @phpstan-param Closure(TKey, T):bool $p |
| 147 |
* |
| 148 |
* @return bool TRUE if the predicate is TRUE for at least one element, FALSE otherwise. |
| 149 |
*/ |
| 150 |
public function exists(Closure $p); |
| 151 |
|
| 152 |
/** |
| 153 |
* Returns all the elements of this collection that satisfy the predicate p. |
| 154 |
* The order of the elements is preserved. |
| 155 |
* |
| 156 |
* @param Closure $p The predicate used for filtering. |
| 157 |
* @phpstan-param Closure(T, TKey):bool $p |
| 158 |
* |
| 159 |
* @return ReadableCollection<mixed> A collection with the results of the filter operation. |
| 160 |
* @phpstan-return ReadableCollection<TKey, T> |
| 161 |
*/ |
| 162 |
public function filter(Closure $p); |
| 163 |
|
| 164 |
/** |
| 165 |
* Applies the given function to each element in the collection and returns |
| 166 |
* a new collection with the elements returned by the function. |
| 167 |
* |
| 168 |
* @phpstan-param Closure(T):U $func |
| 169 |
* |
| 170 |
* @return ReadableCollection<mixed> |
| 171 |
* @phpstan-return ReadableCollection<TKey, U> |
| 172 |
* |
| 173 |
* @phpstan-template U |
| 174 |
*/ |
| 175 |
public function map(Closure $func); |
| 176 |
|
| 177 |
/** |
| 178 |
* Partitions this collection in two collections according to a predicate. |
| 179 |
* Keys are preserved in the resulting collections. |
| 180 |
* |
| 181 |
* @param Closure $p The predicate on which to partition. |
| 182 |
* @phpstan-param Closure(TKey, T):bool $p |
| 183 |
* |
| 184 |
* @return ReadableCollection<mixed>[] An array with two elements. The first element contains the collection |
| 185 |
* of elements where the predicate returned TRUE, the second element |
| 186 |
* contains the collection of elements where the predicate returned FALSE. |
| 187 |
* @phpstan-return array{0: ReadableCollection<TKey, T>, 1: ReadableCollection<TKey, T>} |
| 188 |
*/ |
| 189 |
public function partition(Closure $p); |
| 190 |
|
| 191 |
/** |
| 192 |
* Tests whether the given predicate p holds for all elements of this collection. |
| 193 |
* |
| 194 |
* @param Closure $p The predicate. |
| 195 |
* @phpstan-param Closure(TKey, T):bool $p |
| 196 |
* |
| 197 |
* @return bool TRUE, if the predicate yields TRUE for all elements, FALSE otherwise. |
| 198 |
*/ |
| 199 |
public function forAll(Closure $p); |
| 200 |
|
| 201 |
/** |
| 202 |
* Gets the index/key of a given element. The comparison of two elements is strict, |
| 203 |
* that means not only the value but also the type must match. |
| 204 |
* For objects this means reference equality. |
| 205 |
* |
| 206 |
* @param mixed $element The element to search for. |
| 207 |
* @phpstan-param TMaybeContained $element |
| 208 |
* |
| 209 |
* @return int|string|bool The key/index of the element or FALSE if the element was not found. |
| 210 |
* @phpstan-return (TMaybeContained is T ? TKey|false : false) |
| 211 |
* |
| 212 |
* @template TMaybeContained |
| 213 |
*/ |
| 214 |
public function indexOf(mixed $element); |
| 215 |
|
| 216 |
/** |
| 217 |
* Returns the first element of this collection that satisfies the predicate p. |
| 218 |
* |
| 219 |
* @param Closure $p The predicate. |
| 220 |
* @phpstan-param Closure(TKey, T):bool $p |
| 221 |
* |
| 222 |
* @return mixed The first element respecting the predicate, |
| 223 |
* null if no element respects the predicate. |
| 224 |
* @phpstan-return T|null |
| 225 |
*/ |
| 226 |
public function findFirst(Closure $p); |
| 227 |
|
| 228 |
/** |
| 229 |
* Applies iteratively the given function to each element in the collection, |
| 230 |
* so as to reduce the collection to a single value. |
| 231 |
* |
| 232 |
* @phpstan-param Closure(TReturn|TInitial, T):TReturn $func |
| 233 |
* @phpstan-param TInitial $initial |
| 234 |
* |
| 235 |
* @return mixed |
| 236 |
* @phpstan-return TReturn|TInitial |
| 237 |
* |
| 238 |
* @phpstan-template TReturn |
| 239 |
* @phpstan-template TInitial |
| 240 |
*/ |
| 241 |
public function reduce(Closure $func, mixed $initial = null); |
| 242 |
} |