diff options
Diffstat (limited to 'vendor/doctrine/collections/src/AbstractLazyCollection.php')
| -rw-r--r-- | vendor/doctrine/collections/src/AbstractLazyCollection.php | 414 |
1 files changed, 414 insertions, 0 deletions
diff --git a/vendor/doctrine/collections/src/AbstractLazyCollection.php b/vendor/doctrine/collections/src/AbstractLazyCollection.php new file mode 100644 index 0000000..56a340b --- /dev/null +++ b/vendor/doctrine/collections/src/AbstractLazyCollection.php | |||
| @@ -0,0 +1,414 @@ | |||
| 1 | <?php | ||
| 2 | |||
| 3 | declare(strict_types=1); | ||
| 4 | |||
| 5 | namespace Doctrine\Common\Collections; | ||
| 6 | |||
| 7 | use Closure; | ||
| 8 | use LogicException; | ||
| 9 | use ReturnTypeWillChange; | ||
| 10 | use Traversable; | ||
| 11 | |||
| 12 | /** | ||
| 13 | * Lazy collection that is backed by a concrete collection | ||
| 14 | * | ||
| 15 | * @psalm-template TKey of array-key | ||
| 16 | * @psalm-template T | ||
| 17 | * @template-implements Collection<TKey,T> | ||
| 18 | */ | ||
| 19 | abstract class AbstractLazyCollection implements Collection | ||
| 20 | { | ||
| 21 | /** | ||
| 22 | * The backed collection to use | ||
| 23 | * | ||
| 24 | * @psalm-var Collection<TKey,T>|null | ||
| 25 | * @var Collection<mixed>|null | ||
| 26 | */ | ||
| 27 | protected Collection|null $collection; | ||
| 28 | |||
| 29 | protected bool $initialized = false; | ||
| 30 | |||
| 31 | /** | ||
| 32 | * {@inheritDoc} | ||
| 33 | * | ||
| 34 | * @return int | ||
| 35 | */ | ||
| 36 | #[ReturnTypeWillChange] | ||
| 37 | public function count() | ||
| 38 | { | ||
| 39 | $this->initialize(); | ||
| 40 | |||
| 41 | return $this->collection->count(); | ||
| 42 | } | ||
| 43 | |||
| 44 | /** | ||
| 45 | * {@inheritDoc} | ||
| 46 | */ | ||
| 47 | public function add(mixed $element) | ||
| 48 | { | ||
| 49 | $this->initialize(); | ||
| 50 | |||
| 51 | $this->collection->add($element); | ||
| 52 | } | ||
| 53 | |||
| 54 | /** | ||
| 55 | * {@inheritDoc} | ||
| 56 | */ | ||
| 57 | public function clear() | ||
| 58 | { | ||
| 59 | $this->initialize(); | ||
| 60 | $this->collection->clear(); | ||
| 61 | } | ||
| 62 | |||
| 63 | /** | ||
| 64 | * {@inheritDoc} | ||
| 65 | */ | ||
| 66 | public function contains(mixed $element) | ||
| 67 | { | ||
| 68 | $this->initialize(); | ||
| 69 | |||
| 70 | return $this->collection->contains($element); | ||
| 71 | } | ||
| 72 | |||
| 73 | /** | ||
| 74 | * {@inheritDoc} | ||
| 75 | */ | ||
| 76 | public function isEmpty() | ||
| 77 | { | ||
| 78 | $this->initialize(); | ||
| 79 | |||
| 80 | return $this->collection->isEmpty(); | ||
| 81 | } | ||
| 82 | |||
| 83 | /** | ||
| 84 | * {@inheritDoc} | ||
| 85 | */ | ||
| 86 | public function remove(string|int $key) | ||
| 87 | { | ||
| 88 | $this->initialize(); | ||
| 89 | |||
| 90 | return $this->collection->remove($key); | ||
| 91 | } | ||
| 92 | |||
| 93 | /** | ||
| 94 | * {@inheritDoc} | ||
| 95 | */ | ||
| 96 | public function removeElement(mixed $element) | ||
| 97 | { | ||
| 98 | $this->initialize(); | ||
| 99 | |||
| 100 | return $this->collection->removeElement($element); | ||
| 101 | } | ||
| 102 | |||
| 103 | /** | ||
| 104 | * {@inheritDoc} | ||
| 105 | */ | ||
| 106 | public function containsKey(string|int $key) | ||
| 107 | { | ||
| 108 | $this->initialize(); | ||
| 109 | |||
| 110 | return $this->collection->containsKey($key); | ||
| 111 | } | ||
| 112 | |||
| 113 | /** | ||
| 114 | * {@inheritDoc} | ||
| 115 | */ | ||
| 116 | public function get(string|int $key) | ||
| 117 | { | ||
| 118 | $this->initialize(); | ||
| 119 | |||
| 120 | return $this->collection->get($key); | ||
| 121 | } | ||
| 122 | |||
| 123 | /** | ||
| 124 | * {@inheritDoc} | ||
| 125 | */ | ||
| 126 | public function getKeys() | ||
| 127 | { | ||
| 128 | $this->initialize(); | ||
| 129 | |||
| 130 | return $this->collection->getKeys(); | ||
| 131 | } | ||
| 132 | |||
| 133 | /** | ||
| 134 | * {@inheritDoc} | ||
| 135 | */ | ||
| 136 | public function getValues() | ||
| 137 | { | ||
| 138 | $this->initialize(); | ||
| 139 | |||
| 140 | return $this->collection->getValues(); | ||
| 141 | } | ||
| 142 | |||
| 143 | /** | ||
| 144 | * {@inheritDoc} | ||
| 145 | */ | ||
| 146 | public function set(string|int $key, mixed $value) | ||
| 147 | { | ||
| 148 | $this->initialize(); | ||
| 149 | $this->collection->set($key, $value); | ||
| 150 | } | ||
| 151 | |||
| 152 | /** | ||
| 153 | * {@inheritDoc} | ||
| 154 | */ | ||
| 155 | public function toArray() | ||
| 156 | { | ||
| 157 | $this->initialize(); | ||
| 158 | |||
| 159 | return $this->collection->toArray(); | ||
| 160 | } | ||
| 161 | |||
| 162 | /** | ||
| 163 | * {@inheritDoc} | ||
| 164 | */ | ||
| 165 | public function first() | ||
| 166 | { | ||
| 167 | $this->initialize(); | ||
| 168 | |||
| 169 | return $this->collection->first(); | ||
| 170 | } | ||
| 171 | |||
| 172 | /** | ||
| 173 | * {@inheritDoc} | ||
| 174 | */ | ||
| 175 | public function last() | ||
| 176 | { | ||
| 177 | $this->initialize(); | ||
| 178 | |||
| 179 | return $this->collection->last(); | ||
| 180 | } | ||
| 181 | |||
| 182 | /** | ||
| 183 | * {@inheritDoc} | ||
| 184 | */ | ||
| 185 | public function key() | ||
| 186 | { | ||
| 187 | $this->initialize(); | ||
| 188 | |||
| 189 | return $this->collection->key(); | ||
| 190 | } | ||
| 191 | |||
| 192 | /** | ||
| 193 | * {@inheritDoc} | ||
| 194 | */ | ||
| 195 | public function current() | ||
| 196 | { | ||
| 197 | $this->initialize(); | ||
| 198 | |||
| 199 | return $this->collection->current(); | ||
| 200 | } | ||
| 201 | |||
| 202 | /** | ||
| 203 | * {@inheritDoc} | ||
| 204 | */ | ||
| 205 | public function next() | ||
| 206 | { | ||
| 207 | $this->initialize(); | ||
| 208 | |||
| 209 | return $this->collection->next(); | ||
| 210 | } | ||
| 211 | |||
| 212 | /** | ||
| 213 | * {@inheritDoc} | ||
| 214 | */ | ||
| 215 | public function exists(Closure $p) | ||
| 216 | { | ||
| 217 | $this->initialize(); | ||
| 218 | |||
| 219 | return $this->collection->exists($p); | ||
| 220 | } | ||
| 221 | |||
| 222 | /** | ||
| 223 | * {@inheritDoc} | ||
| 224 | */ | ||
| 225 | public function findFirst(Closure $p) | ||
| 226 | { | ||
| 227 | $this->initialize(); | ||
| 228 | |||
| 229 | return $this->collection->findFirst($p); | ||
| 230 | } | ||
| 231 | |||
| 232 | /** | ||
| 233 | * {@inheritDoc} | ||
| 234 | */ | ||
| 235 | public function filter(Closure $p) | ||
| 236 | { | ||
| 237 | $this->initialize(); | ||
| 238 | |||
| 239 | return $this->collection->filter($p); | ||
| 240 | } | ||
| 241 | |||
| 242 | /** | ||
| 243 | * {@inheritDoc} | ||
| 244 | */ | ||
| 245 | public function forAll(Closure $p) | ||
| 246 | { | ||
| 247 | $this->initialize(); | ||
| 248 | |||
| 249 | return $this->collection->forAll($p); | ||
| 250 | } | ||
| 251 | |||
| 252 | /** | ||
| 253 | * {@inheritDoc} | ||
| 254 | */ | ||
| 255 | public function map(Closure $func) | ||
| 256 | { | ||
| 257 | $this->initialize(); | ||
| 258 | |||
| 259 | return $this->collection->map($func); | ||
| 260 | } | ||
| 261 | |||
| 262 | /** | ||
| 263 | * {@inheritDoc} | ||
| 264 | */ | ||
| 265 | public function reduce(Closure $func, mixed $initial = null) | ||
| 266 | { | ||
| 267 | $this->initialize(); | ||
| 268 | |||
| 269 | return $this->collection->reduce($func, $initial); | ||
| 270 | } | ||
| 271 | |||
| 272 | /** | ||
| 273 | * {@inheritDoc} | ||
| 274 | */ | ||
| 275 | public function partition(Closure $p) | ||
| 276 | { | ||
| 277 | $this->initialize(); | ||
| 278 | |||
| 279 | return $this->collection->partition($p); | ||
| 280 | } | ||
| 281 | |||
| 282 | /** | ||
| 283 | * {@inheritDoc} | ||
| 284 | * | ||
| 285 | * @template TMaybeContained | ||
| 286 | */ | ||
| 287 | public function indexOf(mixed $element) | ||
| 288 | { | ||
| 289 | $this->initialize(); | ||
| 290 | |||
| 291 | return $this->collection->indexOf($element); | ||
| 292 | } | ||
| 293 | |||
| 294 | /** | ||
| 295 | * {@inheritDoc} | ||
| 296 | */ | ||
| 297 | public function slice(int $offset, int|null $length = null) | ||
| 298 | { | ||
| 299 | $this->initialize(); | ||
| 300 | |||
| 301 | return $this->collection->slice($offset, $length); | ||
| 302 | } | ||
| 303 | |||
| 304 | /** | ||
| 305 | * {@inheritDoc} | ||
| 306 | * | ||
| 307 | * @return Traversable<int|string, mixed> | ||
| 308 | * @psalm-return Traversable<TKey,T> | ||
| 309 | */ | ||
| 310 | #[ReturnTypeWillChange] | ||
| 311 | public function getIterator() | ||
| 312 | { | ||
| 313 | $this->initialize(); | ||
| 314 | |||
| 315 | return $this->collection->getIterator(); | ||
| 316 | } | ||
| 317 | |||
| 318 | /** | ||
| 319 | * {@inheritDoc} | ||
| 320 | * | ||
| 321 | * @param TKey $offset | ||
| 322 | * | ||
| 323 | * @return bool | ||
| 324 | */ | ||
| 325 | #[ReturnTypeWillChange] | ||
| 326 | public function offsetExists(mixed $offset) | ||
| 327 | { | ||
| 328 | $this->initialize(); | ||
| 329 | |||
| 330 | return $this->collection->offsetExists($offset); | ||
| 331 | } | ||
| 332 | |||
| 333 | /** | ||
| 334 | * {@inheritDoc} | ||
| 335 | * | ||
| 336 | * @param TKey $offset | ||
| 337 | * | ||
| 338 | * @return T|null | ||
| 339 | */ | ||
| 340 | #[ReturnTypeWillChange] | ||
| 341 | public function offsetGet(mixed $offset) | ||
| 342 | { | ||
| 343 | $this->initialize(); | ||
| 344 | |||
| 345 | return $this->collection->offsetGet($offset); | ||
| 346 | } | ||
| 347 | |||
| 348 | /** | ||
| 349 | * {@inheritDoc} | ||
| 350 | * | ||
| 351 | * @param TKey|null $offset | ||
| 352 | * @param T $value | ||
| 353 | * | ||
| 354 | * @return void | ||
| 355 | */ | ||
| 356 | #[ReturnTypeWillChange] | ||
| 357 | public function offsetSet(mixed $offset, mixed $value) | ||
| 358 | { | ||
| 359 | $this->initialize(); | ||
| 360 | $this->collection->offsetSet($offset, $value); | ||
| 361 | } | ||
| 362 | |||
| 363 | /** | ||
| 364 | * @param TKey $offset | ||
| 365 | * | ||
| 366 | * @return void | ||
| 367 | */ | ||
| 368 | #[ReturnTypeWillChange] | ||
| 369 | public function offsetUnset(mixed $offset) | ||
| 370 | { | ||
| 371 | $this->initialize(); | ||
| 372 | $this->collection->offsetUnset($offset); | ||
| 373 | } | ||
| 374 | |||
| 375 | /** | ||
| 376 | * Is the lazy collection already initialized? | ||
| 377 | * | ||
| 378 | * @return bool | ||
| 379 | * | ||
| 380 | * @psalm-assert-if-true Collection<TKey,T> $this->collection | ||
| 381 | */ | ||
| 382 | public function isInitialized() | ||
| 383 | { | ||
| 384 | return $this->initialized; | ||
| 385 | } | ||
| 386 | |||
| 387 | /** | ||
| 388 | * Initialize the collection | ||
| 389 | * | ||
| 390 | * @return void | ||
| 391 | * | ||
| 392 | * @psalm-assert Collection<TKey,T> $this->collection | ||
| 393 | */ | ||
| 394 | protected function initialize() | ||
| 395 | { | ||
| 396 | if ($this->initialized) { | ||
| 397 | return; | ||
| 398 | } | ||
| 399 | |||
| 400 | $this->doInitialize(); | ||
| 401 | $this->initialized = true; | ||
| 402 | |||
| 403 | if ($this->collection === null) { | ||
| 404 | throw new LogicException('You must initialize the collection property in the doInitialize() method.'); | ||
| 405 | } | ||
| 406 | } | ||
| 407 | |||
| 408 | /** | ||
| 409 | * Do the initialization logic | ||
| 410 | * | ||
| 411 | * @return void | ||
| 412 | */ | ||
| 413 | abstract protected function doInitialize(); | ||
| 414 | } | ||
