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