From bf6655a534a6775d30cafa67bd801276bda1d98d Mon Sep 17 00:00:00 2001 From: polo Date: Tue, 13 Aug 2024 23:45:21 +0200 Subject: =?UTF-8?q?VERSION=200.2=20doctrine=20ORM=20et=20entit=C3=A9s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vendor/doctrine/orm/src/LazyCriteriaCollection.php | 96 ++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 vendor/doctrine/orm/src/LazyCriteriaCollection.php (limited to 'vendor/doctrine/orm/src/LazyCriteriaCollection.php') 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 @@ + + * @implements Selectable + */ +class LazyCriteriaCollection extends AbstractLazyCollection implements Selectable +{ + private int|null $count = null; + + public function __construct( + protected EntityPersister $entityPersister, + protected Criteria $criteria, + ) { + } + + /** + * Do an efficient count on the collection + */ + public function count(): int + { + if ($this->isInitialized()) { + return $this->collection->count(); + } + + // Return cached result in case count query was already executed + if ($this->count !== null) { + return $this->count; + } + + return $this->count = $this->entityPersister->count($this->criteria); + } + + /** + * check if collection is empty without loading it + */ + public function isEmpty(): bool + { + if ($this->isInitialized()) { + return $this->collection->isEmpty(); + } + + return ! $this->count(); + } + + /** + * Do an optimized search of an element + * + * @param mixed $element The element to search for. + * + * @return bool TRUE if the collection contains $element, FALSE otherwise. + */ + public function contains(mixed $element): bool + { + if ($this->isInitialized()) { + return $this->collection->contains($element); + } + + return $this->entityPersister->exists($element, $this->criteria); + } + + /** @return ReadableCollection&Selectable */ + public function matching(Criteria $criteria): ReadableCollection&Selectable + { + $this->initialize(); + assert($this->collection instanceof Selectable); + + return $this->collection->matching($criteria); + } + + protected function doInitialize(): void + { + $elements = $this->entityPersister->loadCriteria($this->criteria); + $this->collection = new ArrayCollection($elements); + } +} -- cgit v1.2.3