1 | <?php |
||
32 | class DocumentRepository implements ObjectRepository, Selectable |
||
33 | { |
||
34 | /** @var string */ |
||
35 | protected $documentName; |
||
36 | |||
37 | /** @var DocumentManager */ |
||
38 | protected $dm; |
||
39 | |||
40 | /** @var UnitOfWork */ |
||
41 | protected $uow; |
||
42 | |||
43 | /** @var ClassMetadata */ |
||
44 | protected $class; |
||
45 | |||
46 | /** |
||
47 | * Initializes this instance with the specified document manager, unit of work and |
||
48 | * class metadata. |
||
49 | * |
||
50 | * @param DocumentManager $dm The DocumentManager to use. |
||
51 | * @param UnitOfWork $uow The UnitOfWork to use. |
||
52 | * @param ClassMetadata $classMetadata The class metadata. |
||
53 | */ |
||
54 | 356 | public function __construct(DocumentManager $dm, UnitOfWork $uow, ClassMetadata $classMetadata) |
|
55 | { |
||
56 | 356 | $this->documentName = $classMetadata->name; |
|
57 | 356 | $this->dm = $dm; |
|
58 | 356 | $this->uow = $uow; |
|
59 | 356 | $this->class = $classMetadata; |
|
60 | 356 | } |
|
61 | |||
62 | /** |
||
63 | * Creates a new Query\Builder instance that is preconfigured for this document name. |
||
64 | */ |
||
65 | 15 | public function createQueryBuilder() : QueryBuilder |
|
66 | { |
||
67 | 15 | return $this->dm->createQueryBuilder($this->documentName); |
|
68 | } |
||
69 | |||
70 | /** |
||
71 | * Creates a new Aggregation\Builder instance that is prepopulated for this document name. |
||
72 | */ |
||
73 | public function createAggregationBuilder() : AggregationBuilder |
||
74 | { |
||
75 | return $this->dm->createAggregationBuilder($this->documentName); |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * Clears the repository, causing all managed documents to become detached. |
||
80 | */ |
||
81 | public function clear() : void |
||
82 | { |
||
83 | $this->dm->clear($this->class->rootDocumentName); |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * Finds a document matching the specified identifier. Optionally a lock mode and |
||
88 | * expected version may be specified. |
||
89 | * |
||
90 | * @param mixed $id Identifier. |
||
91 | * |
||
92 | * @throws MappingException |
||
93 | * @throws LockException |
||
94 | */ |
||
95 | 249 | public function find($id, int $lockMode = LockMode::NONE, ?int $lockVersion = null) : ?object |
|
96 | { |
||
97 | 249 | if ($id === null) { |
|
98 | return null; |
||
99 | } |
||
100 | |||
101 | /* TODO: What if the ID object has a field with the same name as the |
||
102 | * class' mapped identifier field name? |
||
103 | */ |
||
104 | 249 | if (is_array($id)) { |
|
105 | 2 | [$identifierFieldName] = $this->class->getIdentifierFieldNames(); |
|
|
|||
106 | |||
107 | 2 | if (isset($id[$identifierFieldName])) { |
|
108 | $id = $id[$identifierFieldName]; |
||
109 | } |
||
110 | } |
||
111 | |||
112 | // Check identity map first |
||
113 | 249 | $document = $this->uow->tryGetById($id, $this->class); |
|
114 | 249 | if ($document) { |
|
115 | 21 | if ($lockMode !== LockMode::NONE) { |
|
116 | $this->dm->lock($document, $lockMode, $lockVersion); |
||
117 | } |
||
118 | |||
119 | 21 | return $document; // Hit! |
|
120 | } |
||
121 | |||
122 | 242 | $criteria = ['_id' => $id]; |
|
123 | |||
124 | 242 | if ($lockMode === LockMode::NONE) { |
|
125 | 242 | return $this->getDocumentPersister()->load($criteria); |
|
126 | } |
||
127 | |||
128 | if ($lockMode === LockMode::OPTIMISTIC) { |
||
129 | if (! $this->class->isVersioned) { |
||
130 | throw LockException::notVersioned($this->documentName); |
||
131 | } |
||
132 | |||
133 | $document = $this->getDocumentPersister()->load($criteria); |
||
134 | if ($document) { |
||
135 | $this->uow->lock($document, $lockMode, $lockVersion); |
||
136 | } |
||
137 | |||
138 | return $document; |
||
139 | } |
||
140 | |||
141 | return $this->getDocumentPersister()->load($criteria, null, [], $lockMode); |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * Finds all documents in the repository. |
||
146 | */ |
||
147 | 10 | public function findAll() : array |
|
151 | |||
152 | /** |
||
153 | * Finds documents by a set of criteria. |
||
154 | * |
||
155 | * @param int|null $limit |
||
156 | * @param int|null $skip |
||
157 | */ |
||
158 | 11 | public function findBy(array $criteria, ?array $sort = null, $limit = null, $skip = null) : array |
|
162 | |||
163 | /** |
||
164 | * Finds a single document by a set of criteria. |
||
165 | */ |
||
166 | 84 | public function findOneBy(array $criteria) : ?object |
|
170 | |||
171 | 8 | public function getDocumentName() : string |
|
175 | |||
176 | public function getDocumentManager() : DocumentManager |
||
180 | |||
181 | public function getClassMetadata() : ClassMetadata |
||
185 | |||
186 | 8 | public function getClassName() : string |
|
190 | |||
191 | /** |
||
192 | * Selects all elements from a selectable that match the expression and |
||
193 | * returns a new collection containing these elements. |
||
194 | * |
||
195 | * @see Selectable::matching() |
||
196 | */ |
||
197 | 4 | public function matching(Criteria $criteria) : ArrayCollection |
|
224 | |||
225 | 334 | protected function getDocumentPersister() : DocumentPersister |
|
229 | } |
||
230 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.