diff options
author | polo <ordipolo@gmx.fr> | 2024-08-13 23:45:21 +0200 |
---|---|---|
committer | polo <ordipolo@gmx.fr> | 2024-08-13 23:45:21 +0200 |
commit | bf6655a534a6775d30cafa67bd801276bda1d98d (patch) | |
tree | c6381e3f6c81c33eab72508f410b165ba05f7e9c /vendor/doctrine/orm/src/LazyCriteriaCollection.php | |
parent | 94d67a4b51f8e62e7d518cce26a526ae1ec48278 (diff) | |
download | AppliGestionPHP-bf6655a534a6775d30cafa67bd801276bda1d98d.zip |
VERSION 0.2 doctrine ORM et entités
Diffstat (limited to 'vendor/doctrine/orm/src/LazyCriteriaCollection.php')
-rw-r--r-- | vendor/doctrine/orm/src/LazyCriteriaCollection.php | 96 |
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 | |||
3 | declare(strict_types=1); | ||
4 | |||
5 | namespace Doctrine\ORM; | ||
6 | |||
7 | use Doctrine\Common\Collections\AbstractLazyCollection; | ||
8 | use Doctrine\Common\Collections\ArrayCollection; | ||
9 | use Doctrine\Common\Collections\Criteria; | ||
10 | use Doctrine\Common\Collections\ReadableCollection; | ||
11 | use Doctrine\Common\Collections\Selectable; | ||
12 | use Doctrine\ORM\Persisters\Entity\EntityPersister; | ||
13 | |||
14 | use 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 | */ | ||
27 | class 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 | } | ||