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\Selectable; |
||
11 | use Doctrine\ORM\Persisters\Entity\BasicEntityPersister; |
||
12 | use Doctrine\ORM\Persisters\Entity\EntityPersister; |
||
13 | |||
14 | /** |
||
15 | * A lazy collection that allow a fast count when using criteria object |
||
16 | * Once count gets executed once without collection being initialized, result |
||
17 | * is cached and returned on subsequent calls until collection gets loaded, |
||
18 | * then returning the number of loaded results. |
||
19 | */ |
||
20 | class LazyCriteriaCollection extends AbstractLazyCollection implements Selectable |
||
21 | { |
||
22 | /** @var BasicEntityPersister */ |
||
23 | protected $entityPersister; |
||
24 | |||
25 | /** @var Criteria */ |
||
26 | protected $criteria; |
||
27 | |||
28 | /** @var int|null */ |
||
29 | private $count; |
||
30 | |||
31 | 35 | public function __construct(EntityPersister $entityPersister, Criteria $criteria) |
|
32 | { |
||
33 | 35 | $this->entityPersister = $entityPersister; |
|
34 | 35 | $this->criteria = $criteria; |
|
35 | 35 | } |
|
36 | |||
37 | /** |
||
38 | * Do an efficient count on the collection |
||
39 | * |
||
40 | * @return int |
||
41 | */ |
||
42 | 30 | public function count() |
|
43 | { |
||
44 | 30 | if ($this->isInitialized()) { |
|
45 | 2 | return $this->collection->count(); |
|
46 | } |
||
47 | |||
48 | // Return cached result in case count query was already executed |
||
49 | 29 | if ($this->count !== null) { |
|
50 | 2 | return $this->count; |
|
51 | } |
||
52 | |||
53 | 29 | return $this->count = $this->entityPersister->count($this->criteria); |
|
54 | } |
||
55 | |||
56 | /** |
||
57 | * check if collection is empty without loading it |
||
58 | * |
||
59 | * @return bool TRUE if the collection is empty, FALSE otherwise. |
||
60 | */ |
||
61 | 3 | public function isEmpty() |
|
62 | { |
||
63 | 3 | if ($this->isInitialized()) { |
|
64 | 1 | return $this->collection->isEmpty(); |
|
65 | } |
||
66 | |||
67 | 2 | return ! $this->count(); |
|
68 | } |
||
69 | |||
70 | /** |
||
71 | * Do an optimized search of an element |
||
72 | * |
||
73 | * @param object $element |
||
74 | * |
||
75 | * @return bool |
||
76 | */ |
||
77 | 1 | public function contains($element) |
|
78 | { |
||
79 | 1 | if ($this->isInitialized()) { |
|
80 | return $this->collection->contains($element); |
||
81 | } |
||
82 | |||
83 | 1 | return $this->entityPersister->exists($element, $this->criteria); |
|
84 | } |
||
85 | |||
86 | /** |
||
87 | * {@inheritDoc} |
||
88 | */ |
||
89 | 1 | public function matching(Criteria $criteria) |
|
90 | { |
||
91 | 1 | $this->initialize(); |
|
92 | |||
93 | 1 | return $this->collection->matching($criteria); |
|
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
94 | } |
||
95 | |||
96 | /** |
||
97 | * {@inheritDoc} |
||
98 | */ |
||
99 | 9 | protected function doInitialize() |
|
100 | { |
||
101 | 9 | $elements = $this->entityPersister->loadCriteria($this->criteria); |
|
102 | 7 | $this->collection = new ArrayCollection($elements); |
|
103 | 7 | } |
|
104 | } |
||
105 |