summaryrefslogtreecommitdiff
path: root/vendor/doctrine/orm/src/LazyCriteriaCollection.php
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/doctrine/orm/src/LazyCriteriaCollection.php')
-rw-r--r--vendor/doctrine/orm/src/LazyCriteriaCollection.php96
1 files changed, 96 insertions, 0 deletions
diff --git a/vendor/doctrine/orm/src/LazyCriteriaCollection.php b/vendor/doctrine/orm/src/LazyCriteriaCollection.php
new file mode 100644
index 0000000..ca67914
--- /dev/null
+++ b/vendor/doctrine/orm/src/LazyCriteriaCollection.php
@@ -0,0 +1,96 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Doctrine\ORM;
6
7use Doctrine\Common\Collections\AbstractLazyCollection;
8use Doctrine\Common\Collections\ArrayCollection;
9use Doctrine\Common\Collections\Criteria;
10use Doctrine\Common\Collections\ReadableCollection;
11use Doctrine\Common\Collections\Selectable;
12use Doctrine\ORM\Persisters\Entity\EntityPersister;
13
14use function assert;
15
16/**
17 * A lazy collection that allows a fast count when using criteria object
18 * Once count gets executed once without collection being initialized, result
19 * is cached and returned on subsequent calls until collection gets loaded,
20 * then returning the number of loaded results.
21 *
22 * @template TKey of array-key
23 * @template TValue of object
24 * @extends AbstractLazyCollection<TKey, TValue>
25 * @implements Selectable<TKey, TValue>
26 */
27class LazyCriteriaCollection extends AbstractLazyCollection implements Selectable
28{
29 private int|null $count = null;
30
31 public function __construct(
32 protected EntityPersister $entityPersister,
33 protected Criteria $criteria,
34 ) {
35 }
36
37 /**
38 * Do an efficient count on the collection
39 */
40 public function count(): int
41 {
42 if ($this->isInitialized()) {
43 return $this->collection->count();
44 }
45
46 // Return cached result in case count query was already executed
47 if ($this->count !== null) {
48 return $this->count;
49 }
50
51 return $this->count = $this->entityPersister->count($this->criteria);
52 }
53
54 /**
55 * check if collection is empty without loading it
56 */
57 public function isEmpty(): bool
58 {
59 if ($this->isInitialized()) {
60 return $this->collection->isEmpty();
61 }
62
63 return ! $this->count();
64 }
65
66 /**
67 * Do an optimized search of an element
68 *
69 * @param mixed $element The element to search for.
70 *
71 * @return bool TRUE if the collection contains $element, FALSE otherwise.
72 */
73 public function contains(mixed $element): bool
74 {
75 if ($this->isInitialized()) {
76 return $this->collection->contains($element);
77 }
78
79 return $this->entityPersister->exists($element, $this->criteria);
80 }
81
82 /** @return ReadableCollection<TKey, TValue>&Selectable<TKey, TValue> */
83 public function matching(Criteria $criteria): ReadableCollection&Selectable
84 {
85 $this->initialize();
86 assert($this->collection instanceof Selectable);
87
88 return $this->collection->matching($criteria);
89 }
90
91 protected function doInitialize(): void
92 {
93 $elements = $this->entityPersister->loadCriteria($this->criteria);
94 $this->collection = new ArrayCollection($elements);
95 }
96}