Complex classes like DocumentPersister often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use DocumentPersister, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 73 | final class DocumentPersister |
||
| 74 | { |
||
| 75 | /** @var PersistenceBuilder */ |
||
| 76 | private $pb; |
||
| 77 | |||
| 78 | /** @var DocumentManager */ |
||
| 79 | private $dm; |
||
| 80 | |||
| 81 | /** @var UnitOfWork */ |
||
| 82 | private $uow; |
||
| 83 | |||
| 84 | /** @var ClassMetadata */ |
||
| 85 | private $class; |
||
| 86 | |||
| 87 | /** @var Collection|null */ |
||
| 88 | private $collection; |
||
| 89 | |||
| 90 | /** @var Bucket|null */ |
||
| 91 | private $bucket; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Array of queued inserts for the persister to insert. |
||
| 95 | * |
||
| 96 | * @var array |
||
| 97 | */ |
||
| 98 | private $queuedInserts = []; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Array of queued inserts for the persister to insert. |
||
| 102 | * |
||
| 103 | * @var array |
||
| 104 | */ |
||
| 105 | private $queuedUpserts = []; |
||
| 106 | |||
| 107 | /** @var CriteriaMerger */ |
||
| 108 | private $cm; |
||
| 109 | |||
| 110 | /** @var CollectionPersister */ |
||
| 111 | private $cp; |
||
| 112 | |||
| 113 | /** @var HydratorFactory */ |
||
| 114 | private $hydratorFactory; |
||
| 115 | |||
| 116 | 1234 | public function __construct( |
|
| 117 | PersistenceBuilder $pb, |
||
| 118 | DocumentManager $dm, |
||
| 119 | UnitOfWork $uow, |
||
| 120 | HydratorFactory $hydratorFactory, |
||
| 121 | ClassMetadata $class, |
||
| 122 | ?CriteriaMerger $cm = null |
||
| 123 | ) { |
||
| 124 | 1234 | $this->pb = $pb; |
|
| 125 | 1234 | $this->dm = $dm; |
|
| 126 | 1234 | $this->cm = $cm ?: new CriteriaMerger(); |
|
| 127 | 1234 | $this->uow = $uow; |
|
| 128 | 1234 | $this->hydratorFactory = $hydratorFactory; |
|
| 129 | 1234 | $this->class = $class; |
|
| 130 | 1234 | $this->cp = $this->uow->getCollectionPersister(); |
|
| 131 | |||
| 132 | 1234 | if ($class->isEmbeddedDocument || $class->isQueryResultDocument) { |
|
| 133 | 95 | return; |
|
| 134 | } |
||
| 135 | |||
| 136 | 1231 | $this->collection = $dm->getDocumentCollection($class->name); |
|
| 137 | |||
| 138 | 1231 | if (! $class->isFile) { |
|
| 139 | 1218 | return; |
|
| 140 | } |
||
| 141 | |||
| 142 | 21 | $this->bucket = $dm->getDocumentBucket($class->name); |
|
| 143 | 21 | } |
|
| 144 | |||
| 145 | public function getInserts() : array |
||
| 146 | { |
||
| 147 | return $this->queuedInserts; |
||
| 148 | } |
||
| 149 | |||
| 150 | public function isQueuedForInsert(object $document) : bool |
||
| 151 | { |
||
| 152 | return isset($this->queuedInserts[spl_object_hash($document)]); |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Adds a document to the queued insertions. |
||
| 157 | * The document remains queued until {@link executeInserts} is invoked. |
||
| 158 | */ |
||
| 159 | 543 | public function addInsert(object $document) : void |
|
| 160 | { |
||
| 161 | 543 | $this->queuedInserts[spl_object_hash($document)] = $document; |
|
| 162 | 543 | } |
|
| 163 | |||
| 164 | public function getUpserts() : array |
||
| 165 | { |
||
| 166 | return $this->queuedUpserts; |
||
| 167 | } |
||
| 168 | |||
| 169 | public function isQueuedForUpsert(object $document) : bool |
||
| 170 | { |
||
| 171 | return isset($this->queuedUpserts[spl_object_hash($document)]); |
||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Adds a document to the queued upserts. |
||
| 176 | * The document remains queued until {@link executeUpserts} is invoked. |
||
| 177 | */ |
||
| 178 | 88 | public function addUpsert(object $document) : void |
|
| 179 | { |
||
| 180 | 88 | $this->queuedUpserts[spl_object_hash($document)] = $document; |
|
| 181 | 88 | } |
|
| 182 | |||
| 183 | /** |
||
| 184 | * Gets the ClassMetadata instance of the document class this persister is |
||
| 185 | * used for. |
||
| 186 | */ |
||
| 187 | public function getClassMetadata() : ClassMetadata |
||
| 188 | { |
||
| 189 | return $this->class; |
||
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Executes all queued document insertions. |
||
| 194 | * |
||
| 195 | * Queued documents without an ID will inserted in a batch and queued |
||
| 196 | * documents with an ID will be upserted individually. |
||
| 197 | * |
||
| 198 | * If no inserts are queued, invoking this method is a NOOP. |
||
| 199 | * |
||
| 200 | * @throws DriverException |
||
| 201 | */ |
||
| 202 | 543 | public function executeInserts(array $options = []) : void |
|
| 203 | { |
||
| 204 | 543 | if (! $this->queuedInserts) { |
|
|
|
|||
| 205 | return; |
||
| 206 | } |
||
| 207 | |||
| 208 | 543 | $inserts = []; |
|
| 209 | 543 | $options = $this->getWriteOptions($options); |
|
| 210 | 543 | foreach ($this->queuedInserts as $oid => $document) { |
|
| 211 | 543 | $data = $this->pb->prepareInsertData($document); |
|
| 212 | |||
| 213 | // Set the initial version for each insert |
||
| 214 | 532 | if ($this->class->isVersioned) { |
|
| 215 | 44 | $versionMapping = $this->class->fieldMappings[$this->class->versionField]; |
|
| 216 | 44 | $nextVersion = null; |
|
| 217 | 44 | if ($versionMapping['type'] === Type::INT || $versionMapping['type'] === Type::INTEGER) { |
|
| 218 | 38 | $nextVersion = max(1, (int) $this->class->reflFields[$this->class->versionField]->getValue($document)); |
|
| 219 | 38 | $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersion); |
|
| 220 | 6 | } elseif ($versionMapping['type'] === Type::DATE || $versionMapping['type'] === Type::DATE_IMMUTABLE) { |
|
| 221 | 4 | $nextVersionDateTime = $versionMapping['type'] === Type::DATE ? new DateTime() : new DateTimeImmutable(); |
|
| 222 | 4 | $nextVersion = Type::convertPHPToDatabaseValue($nextVersionDateTime); |
|
| 223 | 4 | $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersionDateTime); |
|
| 224 | 2 | } elseif ($versionMapping['type'] === Type::DECIMAL128) { |
|
| 225 | 2 | $current = (string) $this->class->reflFields[$this->class->versionField]->getValue($document); |
|
| 226 | 2 | $nextVersion = bccomp('1', $current) === 1 ? '1' : $current; |
|
| 227 | 2 | $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersion); |
|
| 228 | } |
||
| 229 | 44 | $data[$versionMapping['name']] = $nextVersion; |
|
| 230 | } |
||
| 231 | |||
| 232 | 532 | $inserts[] = $data; |
|
| 233 | } |
||
| 234 | |||
| 235 | 532 | if ($inserts) { |
|
| 236 | try { |
||
| 237 | 532 | assert($this->collection instanceof Collection); |
|
| 238 | 532 | $this->collection->insertMany($inserts, $options); |
|
| 239 | 6 | } catch (DriverException $e) { |
|
| 240 | 6 | $this->queuedInserts = []; |
|
| 241 | 6 | throw $e; |
|
| 242 | } |
||
| 243 | } |
||
| 244 | |||
| 245 | /* All collections except for ones using addToSet have already been |
||
| 246 | * saved. We have left these to be handled separately to avoid checking |
||
| 247 | * collection for uniqueness on PHP side. |
||
| 248 | */ |
||
| 249 | 532 | foreach ($this->queuedInserts as $document) { |
|
| 250 | 532 | $this->handleCollections($document, $options); |
|
| 251 | } |
||
| 252 | |||
| 253 | 532 | $this->queuedInserts = []; |
|
| 254 | 532 | } |
|
| 255 | |||
| 256 | /** |
||
| 257 | * Executes all queued document upserts. |
||
| 258 | * |
||
| 259 | * Queued documents with an ID are upserted individually. |
||
| 260 | * |
||
| 261 | * If no upserts are queued, invoking this method is a NOOP. |
||
| 262 | */ |
||
| 263 | 88 | public function executeUpserts(array $options = []) : void |
|
| 264 | { |
||
| 265 | 88 | if (! $this->queuedUpserts) { |
|
| 266 | return; |
||
| 267 | } |
||
| 268 | |||
| 269 | 88 | $options = $this->getWriteOptions($options); |
|
| 270 | 88 | foreach ($this->queuedUpserts as $oid => $document) { |
|
| 271 | try { |
||
| 272 | 88 | $this->executeUpsert($document, $options); |
|
| 273 | 88 | $this->handleCollections($document, $options); |
|
| 274 | 88 | unset($this->queuedUpserts[$oid]); |
|
| 275 | } catch (WriteException $e) { |
||
| 276 | unset($this->queuedUpserts[$oid]); |
||
| 277 | throw $e; |
||
| 278 | } |
||
| 279 | } |
||
| 280 | 88 | } |
|
| 281 | |||
| 282 | /** |
||
| 283 | * Executes a single upsert in {@link executeUpserts} |
||
| 284 | */ |
||
| 285 | 88 | private function executeUpsert(object $document, array $options) : void |
|
| 286 | { |
||
| 287 | 88 | $options['upsert'] = true; |
|
| 288 | 88 | $criteria = $this->getQueryForDocument($document); |
|
| 289 | |||
| 290 | 88 | $data = $this->pb->prepareUpsertData($document); |
|
| 291 | |||
| 292 | // Set the initial version for each upsert |
||
| 293 | 88 | if ($this->class->isVersioned) { |
|
| 294 | 5 | $versionMapping = $this->class->fieldMappings[$this->class->versionField]; |
|
| 295 | 5 | $nextVersion = null; |
|
| 296 | 5 | if ($versionMapping['type'] === Type::INT || $versionMapping === Type::INTEGER) { |
|
| 297 | 2 | $nextVersion = max(1, (int) $this->class->reflFields[$this->class->versionField]->getValue($document)); |
|
| 298 | 2 | $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersion); |
|
| 299 | 3 | } elseif ($versionMapping['type'] === Type::DATE || $versionMapping['type'] === Type::DATE_IMMUTABLE) { |
|
| 300 | 2 | $nextVersionDateTime = $versionMapping['type'] === Type::DATE ? new DateTime() : new DateTimeImmutable(); |
|
| 301 | 2 | $nextVersion = Type::convertPHPToDatabaseValue($nextVersionDateTime); |
|
| 302 | 2 | $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersionDateTime); |
|
| 303 | 1 | } elseif ($versionMapping['type'] === Type::DECIMAL128) { |
|
| 304 | 1 | $current = (string) $this->class->reflFields[$this->class->versionField]->getValue($document); |
|
| 305 | 1 | $nextVersion = bccomp('1', $current) === 1 ? '1' : $current; |
|
| 306 | 1 | $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersion); |
|
| 307 | } |
||
| 308 | 5 | $data['$set'][$versionMapping['name']] = $nextVersion; |
|
| 309 | } |
||
| 310 | |||
| 311 | 88 | foreach (array_keys($criteria) as $field) { |
|
| 312 | 88 | unset($data['$set'][$field]); |
|
| 313 | 88 | unset($data['$inc'][$field]); |
|
| 314 | 88 | unset($data['$setOnInsert'][$field]); |
|
| 315 | } |
||
| 316 | |||
| 317 | // Do not send empty update operators |
||
| 318 | 88 | foreach (['$set', '$inc', '$setOnInsert'] as $operator) { |
|
| 319 | 88 | if (! empty($data[$operator])) { |
|
| 320 | 73 | continue; |
|
| 321 | } |
||
| 322 | |||
| 323 | 88 | unset($data[$operator]); |
|
| 324 | } |
||
| 325 | |||
| 326 | /* If there are no modifiers remaining, we're upserting a document with |
||
| 327 | * an identifier as its only field. Since a document with the identifier |
||
| 328 | * may already exist, the desired behavior is "insert if not exists" and |
||
| 329 | * NOOP otherwise. MongoDB 2.6+ does not allow empty modifiers, so $set |
||
| 330 | * the identifier to the same value in our criteria. |
||
| 331 | * |
||
| 332 | * This will fail for versions before MongoDB 2.6, which require an |
||
| 333 | * empty $set modifier. The best we can do (without attempting to check |
||
| 334 | * server versions in advance) is attempt the 2.6+ behavior and retry |
||
| 335 | * after the relevant exception. |
||
| 336 | * |
||
| 337 | * See: https://jira.mongodb.org/browse/SERVER-12266 |
||
| 338 | */ |
||
| 339 | 88 | if (empty($data)) { |
|
| 340 | 16 | $retry = true; |
|
| 341 | 16 | $data = ['$set' => ['_id' => $criteria['_id']]]; |
|
| 342 | } |
||
| 343 | |||
| 344 | try { |
||
| 345 | 88 | assert($this->collection instanceof Collection); |
|
| 346 | 88 | $this->collection->updateOne($criteria, $data, $options); |
|
| 347 | |||
| 348 | 88 | return; |
|
| 349 | } catch (WriteException $e) { |
||
| 350 | if (empty($retry) || strpos($e->getMessage(), 'Mod on _id not allowed') === false) { |
||
| 351 | throw $e; |
||
| 352 | } |
||
| 353 | } |
||
| 354 | |||
| 355 | assert($this->collection instanceof Collection); |
||
| 356 | $this->collection->updateOne($criteria, ['$set' => new stdClass()], $options); |
||
| 357 | } |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Updates the already persisted document if it has any new changesets. |
||
| 361 | * |
||
| 362 | * @throws LockException |
||
| 363 | */ |
||
| 364 | 240 | public function update(object $document, array $options = []) : void |
|
| 365 | { |
||
| 366 | 240 | $update = $this->pb->prepareUpdateData($document); |
|
| 367 | |||
| 368 | 240 | $query = $this->getQueryForDocument($document); |
|
| 369 | |||
| 370 | 238 | foreach (array_keys($query) as $field) { |
|
| 371 | 238 | unset($update['$set'][$field]); |
|
| 372 | } |
||
| 373 | |||
| 374 | 238 | if (empty($update['$set'])) { |
|
| 375 | 101 | unset($update['$set']); |
|
| 376 | } |
||
| 377 | |||
| 378 | // Include versioning logic to set the new version value in the database |
||
| 379 | // and to ensure the version has not changed since this document object instance |
||
| 380 | // was fetched from the database |
||
| 381 | 238 | $nextVersion = null; |
|
| 382 | 238 | if ($this->class->isVersioned) { |
|
| 383 | 39 | $versionMapping = $this->class->fieldMappings[$this->class->versionField]; |
|
| 384 | 39 | $currentVersion = $this->class->reflFields[$this->class->versionField]->getValue($document); |
|
| 385 | 39 | if ($versionMapping['type'] === Type::INT || $versionMapping['type'] === Type::INTEGER) { |
|
| 386 | 30 | $nextVersion = $currentVersion + 1; |
|
| 387 | 30 | $update['$inc'][$versionMapping['name']] = 1; |
|
| 388 | 30 | $query[$versionMapping['name']] = $currentVersion; |
|
| 389 | 9 | } elseif ($versionMapping['type'] === Type::DATE || $versionMapping['type'] === Type::DATE_IMMUTABLE) { |
|
| 390 | 6 | $nextVersion = $versionMapping['type'] === Type::DATE ? new DateTime() : new DateTimeImmutable(); |
|
| 391 | 6 | $update['$set'][$versionMapping['name']] = Type::convertPHPToDatabaseValue($nextVersion); |
|
| 392 | 6 | $query[$versionMapping['name']] = Type::convertPHPToDatabaseValue($currentVersion); |
|
| 393 | 3 | } elseif ($versionMapping['type'] === Type::DECIMAL128) { |
|
| 394 | 3 | $current = $this->class->reflFields[$this->class->versionField]->getValue($document); |
|
| 395 | 3 | $nextVersion = bcadd($current, '1'); |
|
| 396 | 3 | $type = Type::getType(Type::DECIMAL128); |
|
| 397 | 3 | $update['$set'][$versionMapping['name']] = $type->convertPHPToDatabaseValue($nextVersion); |
|
| 398 | 3 | $query[$versionMapping['name']] = $type->convertPHPToDatabaseValue($currentVersion); |
|
| 399 | } |
||
| 400 | } |
||
| 401 | |||
| 402 | 238 | if (! empty($update)) { |
|
| 403 | // Include locking logic so that if the document object in memory is currently |
||
| 404 | // locked then it will remove it, otherwise it ensures the document is not locked. |
||
| 405 | 164 | if ($this->class->isLockable) { |
|
| 406 | 17 | $isLocked = $this->class->reflFields[$this->class->lockField]->getValue($document); |
|
| 407 | 17 | $lockMapping = $this->class->fieldMappings[$this->class->lockField]; |
|
| 408 | 17 | if ($isLocked) { |
|
| 409 | 2 | $update['$unset'] = [$lockMapping['name'] => true]; |
|
| 410 | } else { |
||
| 411 | 15 | $query[$lockMapping['name']] = ['$exists' => false]; |
|
| 412 | } |
||
| 413 | } |
||
| 414 | |||
| 415 | 164 | $options = $this->getWriteOptions($options); |
|
| 416 | |||
| 417 | 164 | assert($this->collection instanceof Collection); |
|
| 418 | 164 | $result = $this->collection->updateOne($query, $update, $options); |
|
| 419 | |||
| 420 | 164 | if (($this->class->isVersioned || $this->class->isLockable) && $result->getModifiedCount() !== 1) { |
|
| 421 | 8 | throw LockException::lockFailed($document); |
|
| 422 | } |
||
| 423 | |||
| 424 | 157 | if ($this->class->isVersioned) { |
|
| 425 | 32 | $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersion); |
|
| 426 | } |
||
| 427 | } |
||
| 428 | |||
| 429 | 231 | $this->handleCollections($document, $options); |
|
| 430 | 231 | } |
|
| 431 | |||
| 432 | /** |
||
| 433 | * Removes document from mongo |
||
| 434 | * |
||
| 435 | * @throws LockException |
||
| 436 | */ |
||
| 437 | 36 | public function delete(object $document, array $options = []) : void |
|
| 438 | { |
||
| 439 | 36 | if ($this->bucket instanceof Bucket) { |
|
| 440 | 1 | $documentIdentifier = $this->uow->getDocumentIdentifier($document); |
|
| 441 | 1 | $databaseIdentifier = $this->class->getDatabaseIdentifierValue($documentIdentifier); |
|
| 442 | |||
| 443 | 1 | $this->bucket->delete($databaseIdentifier); |
|
| 444 | |||
| 445 | 1 | return; |
|
| 446 | } |
||
| 447 | |||
| 448 | 35 | $query = $this->getQueryForDocument($document); |
|
| 449 | |||
| 450 | 35 | if ($this->class->isLockable) { |
|
| 451 | 2 | $query[$this->class->lockField] = ['$exists' => false]; |
|
| 452 | } |
||
| 453 | |||
| 454 | 35 | $options = $this->getWriteOptions($options); |
|
| 455 | |||
| 456 | 35 | assert($this->collection instanceof Collection); |
|
| 457 | 35 | $result = $this->collection->deleteOne($query, $options); |
|
| 458 | |||
| 459 | 35 | if (($this->class->isVersioned || $this->class->isLockable) && ! $result->getDeletedCount()) { |
|
| 460 | 2 | throw LockException::lockFailed($document); |
|
| 461 | } |
||
| 462 | 33 | } |
|
| 463 | |||
| 464 | /** |
||
| 465 | * Refreshes a managed document. |
||
| 466 | */ |
||
| 467 | 23 | public function refresh(object $document) : void |
|
| 468 | { |
||
| 469 | 23 | assert($this->collection instanceof Collection); |
|
| 470 | 23 | $query = $this->getQueryForDocument($document); |
|
| 471 | 23 | $data = $this->collection->findOne($query); |
|
| 472 | 23 | if ($data === null) { |
|
| 473 | throw MongoDBException::cannotRefreshDocument(); |
||
| 474 | } |
||
| 475 | 23 | $data = $this->hydratorFactory->hydrate($document, (array) $data); |
|
| 476 | 23 | $this->uow->setOriginalDocumentData($document, $data); |
|
| 477 | 23 | } |
|
| 478 | |||
| 479 | /** |
||
| 480 | * Finds a document by a set of criteria. |
||
| 481 | * |
||
| 482 | * If a scalar or MongoDB\BSON\ObjectId is provided for $criteria, it will |
||
| 483 | * be used to match an _id value. |
||
| 484 | * |
||
| 485 | * @param mixed $criteria Query criteria |
||
| 486 | * |
||
| 487 | * @throws LockException |
||
| 488 | * |
||
| 489 | * @todo Check identity map? loadById method? Try to guess whether |
||
| 490 | * $criteria is the id? |
||
| 491 | */ |
||
| 492 | 369 | public function load($criteria, ?object $document = null, array $hints = [], int $lockMode = 0, ?array $sort = null) : ?object |
|
| 493 | { |
||
| 494 | // TODO: remove this |
||
| 495 | 369 | if ($criteria === null || is_scalar($criteria) || $criteria instanceof ObjectId) { |
|
| 496 | $criteria = ['_id' => $criteria]; |
||
| 497 | } |
||
| 498 | |||
| 499 | 369 | $criteria = $this->prepareQueryOrNewObj($criteria); |
|
| 500 | 369 | $criteria = $this->addDiscriminatorToPreparedQuery($criteria); |
|
| 501 | 369 | $criteria = $this->addFilterToPreparedQuery($criteria); |
|
| 502 | |||
| 503 | 369 | $options = []; |
|
| 504 | 369 | if ($sort !== null) { |
|
| 505 | 96 | $options['sort'] = $this->prepareSort($sort); |
|
| 506 | } |
||
| 507 | 369 | assert($this->collection instanceof Collection); |
|
| 508 | 369 | $result = $this->collection->findOne($criteria, $options); |
|
| 509 | 369 | $result = $result !== null ? (array) $result : null; |
|
| 510 | |||
| 511 | 369 | if ($this->class->isLockable) { |
|
| 512 | 1 | $lockMapping = $this->class->fieldMappings[$this->class->lockField]; |
|
| 513 | 1 | if (isset($result[$lockMapping['name']]) && $result[$lockMapping['name']] === LockMode::PESSIMISTIC_WRITE) { |
|
| 514 | 1 | throw LockException::lockFailed($document); |
|
| 515 | } |
||
| 516 | } |
||
| 517 | |||
| 518 | 368 | if ($result === null) { |
|
| 519 | 115 | return null; |
|
| 520 | } |
||
| 521 | |||
| 522 | 324 | return $this->createDocument($result, $document, $hints); |
|
| 523 | } |
||
| 524 | |||
| 525 | /** |
||
| 526 | * Finds documents by a set of criteria. |
||
| 527 | */ |
||
| 528 | 24 | public function loadAll(array $criteria = [], ?array $sort = null, ?int $limit = null, ?int $skip = null) : Iterator |
|
| 529 | { |
||
| 530 | 24 | $criteria = $this->prepareQueryOrNewObj($criteria); |
|
| 531 | 24 | $criteria = $this->addDiscriminatorToPreparedQuery($criteria); |
|
| 532 | 24 | $criteria = $this->addFilterToPreparedQuery($criteria); |
|
| 533 | |||
| 534 | 24 | $options = []; |
|
| 535 | 24 | if ($sort !== null) { |
|
| 536 | 11 | $options['sort'] = $this->prepareSort($sort); |
|
| 537 | } |
||
| 538 | |||
| 539 | 24 | if ($limit !== null) { |
|
| 540 | 10 | $options['limit'] = $limit; |
|
| 541 | } |
||
| 542 | |||
| 543 | 24 | if ($skip !== null) { |
|
| 544 | 1 | $options['skip'] = $skip; |
|
| 545 | } |
||
| 546 | |||
| 547 | 24 | assert($this->collection instanceof Collection); |
|
| 548 | 24 | $baseCursor = $this->collection->find($criteria, $options); |
|
| 549 | |||
| 550 | 24 | return $this->wrapCursor($baseCursor); |
|
| 551 | } |
||
| 552 | |||
| 553 | /** |
||
| 554 | * @throws MongoDBException |
||
| 555 | */ |
||
| 556 | 321 | private function getShardKeyQuery(object $document) : array |
|
| 557 | { |
||
| 558 | 321 | if (! $this->class->isSharded()) { |
|
| 559 | 311 | return []; |
|
| 560 | } |
||
| 561 | |||
| 562 | 10 | $shardKey = $this->class->getShardKey(); |
|
| 563 | 10 | $keys = array_keys($shardKey['keys']); |
|
| 564 | 10 | $data = $this->uow->getDocumentActualData($document); |
|
| 565 | |||
| 566 | 10 | $shardKeyQueryPart = []; |
|
| 567 | 10 | foreach ($keys as $key) { |
|
| 568 | 10 | assert(is_string($key)); |
|
| 569 | 10 | $mapping = $this->class->getFieldMappingByDbFieldName($key); |
|
| 570 | 10 | $this->guardMissingShardKey($document, $key, $data); |
|
| 571 | |||
| 572 | 8 | if (isset($mapping['association']) && $mapping['association'] === ClassMetadata::REFERENCE_ONE) { |
|
| 573 | 1 | $reference = $this->prepareReference( |
|
| 574 | 1 | $key, |
|
| 575 | 1 | $data[$mapping['fieldName']], |
|
| 576 | 1 | $mapping, |
|
| 577 | 1 | false |
|
| 578 | ); |
||
| 579 | 1 | foreach ($reference as $keyValue) { |
|
| 580 | 1 | $shardKeyQueryPart[$keyValue[0]] = $keyValue[1]; |
|
| 581 | } |
||
| 582 | } else { |
||
| 583 | 7 | $value = Type::getType($mapping['type'])->convertToDatabaseValue($data[$mapping['fieldName']]); |
|
| 584 | 7 | $shardKeyQueryPart[$key] = $value; |
|
| 585 | } |
||
| 586 | } |
||
| 587 | |||
| 588 | 8 | return $shardKeyQueryPart; |
|
| 589 | } |
||
| 590 | |||
| 591 | /** |
||
| 592 | * Wraps the supplied base cursor in the corresponding ODM class. |
||
| 593 | */ |
||
| 594 | 24 | private function wrapCursor(Cursor $baseCursor) : Iterator |
|
| 595 | { |
||
| 596 | 24 | return new CachingIterator(new HydratingIterator($baseCursor, $this->dm->getUnitOfWork(), $this->class)); |
|
| 597 | } |
||
| 598 | |||
| 599 | /** |
||
| 600 | * Checks whether the given managed document exists in the database. |
||
| 601 | */ |
||
| 602 | 3 | public function exists(object $document) : bool |
|
| 603 | { |
||
| 604 | 3 | $id = $this->class->getIdentifierObject($document); |
|
| 605 | 3 | assert($this->collection instanceof Collection); |
|
| 606 | |||
| 607 | 3 | return (bool) $this->collection->findOne(['_id' => $id], ['_id']); |
|
| 608 | } |
||
| 609 | |||
| 610 | /** |
||
| 611 | * Locks document by storing the lock mode on the mapped lock field. |
||
| 612 | */ |
||
| 613 | 5 | public function lock(object $document, int $lockMode) : void |
|
| 614 | { |
||
| 615 | 5 | $id = $this->uow->getDocumentIdentifier($document); |
|
| 616 | 5 | $criteria = ['_id' => $this->class->getDatabaseIdentifierValue($id)]; |
|
| 617 | 5 | $lockMapping = $this->class->fieldMappings[$this->class->lockField]; |
|
| 618 | 5 | assert($this->collection instanceof Collection); |
|
| 619 | 5 | $this->collection->updateOne($criteria, ['$set' => [$lockMapping['name'] => $lockMode]]); |
|
| 620 | 5 | $this->class->reflFields[$this->class->lockField]->setValue($document, $lockMode); |
|
| 621 | 5 | } |
|
| 622 | |||
| 623 | /** |
||
| 624 | * Releases any lock that exists on this document. |
||
| 625 | */ |
||
| 626 | 1 | public function unlock(object $document) : void |
|
| 627 | { |
||
| 628 | 1 | $id = $this->uow->getDocumentIdentifier($document); |
|
| 629 | 1 | $criteria = ['_id' => $this->class->getDatabaseIdentifierValue($id)]; |
|
| 630 | 1 | $lockMapping = $this->class->fieldMappings[$this->class->lockField]; |
|
| 631 | 1 | assert($this->collection instanceof Collection); |
|
| 632 | 1 | $this->collection->updateOne($criteria, ['$unset' => [$lockMapping['name'] => true]]); |
|
| 633 | 1 | $this->class->reflFields[$this->class->lockField]->setValue($document, null); |
|
| 634 | 1 | } |
|
| 635 | |||
| 636 | /** |
||
| 637 | * Creates or fills a single document object from an query result. |
||
| 638 | * |
||
| 639 | * @param array $result The query result. |
||
| 640 | * @param object $document The document object to fill, if any. |
||
| 641 | * @param array $hints Hints for document creation. |
||
| 642 | * |
||
| 643 | * @return object|null The filled and managed document object or NULL, if the query result is empty. |
||
| 644 | */ |
||
| 645 | 324 | private function createDocument(array $result, ?object $document = null, array $hints = []) : ?object |
|
| 646 | { |
||
| 647 | 324 | if ($document !== null) { |
|
| 648 | 29 | $hints[Query::HINT_REFRESH] = true; |
|
| 649 | 29 | $id = $this->class->getPHPIdentifierValue($result['_id']); |
|
| 650 | 29 | $this->uow->registerManaged($document, $id, $result); |
|
| 651 | } |
||
| 652 | |||
| 653 | 324 | return $this->uow->getOrCreateDocument($this->class->name, $result, $hints, $document); |
|
| 654 | } |
||
| 655 | |||
| 656 | /** |
||
| 657 | * Loads a PersistentCollection data. Used in the initialize() method. |
||
| 658 | */ |
||
| 659 | 181 | public function loadCollection(PersistentCollectionInterface $collection) : void |
|
| 660 | { |
||
| 661 | 181 | $mapping = $collection->getMapping(); |
|
| 662 | 181 | switch ($mapping['association']) { |
|
| 663 | case ClassMetadata::EMBED_MANY: |
||
| 664 | 127 | $this->loadEmbedManyCollection($collection); |
|
| 665 | 126 | break; |
|
| 666 | |||
| 667 | case ClassMetadata::REFERENCE_MANY: |
||
| 668 | 77 | if (isset($mapping['repositoryMethod']) && $mapping['repositoryMethod']) { |
|
| 669 | 5 | $this->loadReferenceManyWithRepositoryMethod($collection); |
|
| 670 | } else { |
||
| 671 | 73 | if ($mapping['isOwningSide']) { |
|
| 672 | 61 | $this->loadReferenceManyCollectionOwningSide($collection); |
|
| 673 | } else { |
||
| 674 | 18 | $this->loadReferenceManyCollectionInverseSide($collection); |
|
| 675 | } |
||
| 676 | } |
||
| 677 | 76 | break; |
|
| 678 | } |
||
| 679 | 179 | } |
|
| 680 | |||
| 681 | 127 | private function loadEmbedManyCollection(PersistentCollectionInterface $collection) : void |
|
| 682 | { |
||
| 683 | 127 | $embeddedDocuments = $collection->getMongoData(); |
|
| 684 | 127 | $mapping = $collection->getMapping(); |
|
| 685 | 127 | $owner = $collection->getOwner(); |
|
| 686 | |||
| 687 | 127 | if (! $embeddedDocuments) { |
|
| 688 | 75 | return; |
|
| 689 | } |
||
| 690 | |||
| 691 | 98 | if ($owner === null) { |
|
| 692 | throw PersistentCollectionException::ownerRequiredToLoadCollection(); |
||
| 693 | } |
||
| 694 | |||
| 695 | 98 | foreach ($embeddedDocuments as $key => $embeddedDocument) { |
|
| 696 | 98 | $className = $this->uow->getClassNameForAssociation($mapping, $embeddedDocument); |
|
| 697 | 98 | $embeddedMetadata = $this->dm->getClassMetadata($className); |
|
| 698 | 98 | $embeddedDocumentObject = $embeddedMetadata->newInstance(); |
|
| 699 | |||
| 700 | 98 | if (! is_array($embeddedDocument)) { |
|
| 701 | 1 | throw HydratorException::associationItemTypeMismatch(get_class($owner), $mapping['name'], $key, 'array', gettype($embeddedDocument)); |
|
| 702 | } |
||
| 703 | |||
| 704 | 97 | $this->uow->setParentAssociation($embeddedDocumentObject, $mapping, $owner, $mapping['name'] . '.' . $key); |
|
| 705 | |||
| 706 | 97 | $data = $this->hydratorFactory->hydrate($embeddedDocumentObject, $embeddedDocument, $collection->getHints()); |
|
| 707 | 97 | $id = $data[$embeddedMetadata->identifier] ?? null; |
|
| 708 | |||
| 709 | 97 | if (empty($collection->getHints()[Query::HINT_READ_ONLY])) { |
|
| 710 | 96 | $this->uow->registerManaged($embeddedDocumentObject, $id, $data); |
|
| 711 | } |
||
| 712 | 97 | if (CollectionHelper::isHash($mapping['strategy'])) { |
|
| 713 | 25 | $collection->set($key, $embeddedDocumentObject); |
|
| 714 | } else { |
||
| 715 | 80 | $collection->add($embeddedDocumentObject); |
|
| 716 | } |
||
| 717 | } |
||
| 718 | 97 | } |
|
| 719 | |||
| 720 | 61 | private function loadReferenceManyCollectionOwningSide(PersistentCollectionInterface $collection) : void |
|
| 721 | { |
||
| 722 | 61 | $hints = $collection->getHints(); |
|
| 723 | 61 | $mapping = $collection->getMapping(); |
|
| 724 | 61 | $owner = $collection->getOwner(); |
|
| 725 | 61 | $groupedIds = []; |
|
| 726 | |||
| 727 | 61 | if ($owner === null) { |
|
| 728 | throw PersistentCollectionException::ownerRequiredToLoadCollection(); |
||
| 729 | } |
||
| 730 | |||
| 731 | 61 | $sorted = isset($mapping['sort']) && $mapping['sort']; |
|
| 732 | |||
| 733 | 61 | foreach ($collection->getMongoData() as $key => $reference) { |
|
| 734 | 55 | $className = $this->uow->getClassNameForAssociation($mapping, $reference); |
|
| 735 | |||
| 736 | 55 | if ($mapping['storeAs'] !== ClassMetadata::REFERENCE_STORE_AS_ID && ! is_array($reference)) { |
|
| 737 | 1 | throw HydratorException::associationItemTypeMismatch(get_class($owner), $mapping['name'], $key, 'array', gettype($reference)); |
|
| 738 | } |
||
| 739 | |||
| 740 | 54 | $identifier = ClassMetadata::getReferenceId($reference, $mapping['storeAs']); |
|
| 741 | 54 | $id = $this->dm->getClassMetadata($className)->getPHPIdentifierValue($identifier); |
|
| 742 | |||
| 743 | // create a reference to the class and id |
||
| 744 | 54 | $reference = $this->dm->getReference($className, $id); |
|
| 745 | |||
| 746 | // no custom sort so add the references right now in the order they are embedded |
||
| 747 | 54 | if (! $sorted) { |
|
| 748 | 53 | if (CollectionHelper::isHash($mapping['strategy'])) { |
|
| 749 | 2 | $collection->set($key, $reference); |
|
| 750 | } else { |
||
| 751 | 51 | $collection->add($reference); |
|
| 752 | } |
||
| 753 | } |
||
| 754 | |||
| 755 | // only query for the referenced object if it is not already initialized or the collection is sorted |
||
| 756 | 54 | if (! (($reference instanceof GhostObjectInterface && ! $reference->isProxyInitialized())) && ! $sorted) { |
|
| 757 | 23 | continue; |
|
| 758 | } |
||
| 759 | |||
| 760 | 39 | $groupedIds[$className][] = $identifier; |
|
| 761 | } |
||
| 762 | 60 | foreach ($groupedIds as $className => $ids) { |
|
| 763 | 39 | $class = $this->dm->getClassMetadata($className); |
|
| 764 | 39 | $mongoCollection = $this->dm->getDocumentCollection($className); |
|
| 765 | 39 | $criteria = $this->cm->merge( |
|
| 766 | 39 | ['_id' => ['$in' => array_values($ids)]], |
|
| 767 | 39 | $this->dm->getFilterCollection()->getFilterCriteria($class), |
|
| 768 | 39 | $mapping['criteria'] ?? [] |
|
| 769 | ); |
||
| 770 | 39 | $criteria = $this->uow->getDocumentPersister($className)->prepareQueryOrNewObj($criteria); |
|
| 771 | |||
| 772 | 39 | $options = []; |
|
| 773 | 39 | if (isset($mapping['sort'])) { |
|
| 774 | 39 | $options['sort'] = $this->prepareSort($mapping['sort']); |
|
| 775 | } |
||
| 776 | 39 | if (isset($mapping['limit'])) { |
|
| 777 | $options['limit'] = $mapping['limit']; |
||
| 778 | } |
||
| 779 | 39 | if (isset($mapping['skip'])) { |
|
| 780 | $options['skip'] = $mapping['skip']; |
||
| 781 | } |
||
| 782 | 39 | if (! empty($hints[Query::HINT_READ_PREFERENCE])) { |
|
| 783 | $options['readPreference'] = $hints[Query::HINT_READ_PREFERENCE]; |
||
| 784 | } |
||
| 785 | |||
| 786 | 39 | $cursor = $mongoCollection->find($criteria, $options); |
|
| 787 | 39 | $documents = $cursor->toArray(); |
|
| 788 | 39 | foreach ($documents as $documentData) { |
|
| 789 | 38 | $document = $this->uow->getById($documentData['_id'], $class); |
|
| 790 | 38 | if ($document instanceof GhostObjectInterface && ! $document->isProxyInitialized()) { |
|
| 791 | 38 | $data = $this->hydratorFactory->hydrate($document, $documentData); |
|
| 792 | 38 | $this->uow->setOriginalDocumentData($document, $data); |
|
| 793 | } |
||
| 794 | |||
| 795 | 38 | if (! $sorted) { |
|
| 796 | 37 | continue; |
|
| 797 | } |
||
| 798 | |||
| 799 | 1 | $collection->add($document); |
|
| 800 | } |
||
| 801 | } |
||
| 802 | 60 | } |
|
| 803 | |||
| 804 | 18 | private function loadReferenceManyCollectionInverseSide(PersistentCollectionInterface $collection) : void |
|
| 805 | { |
||
| 806 | 18 | $query = $this->createReferenceManyInverseSideQuery($collection); |
|
| 807 | 18 | $iterator = $query->execute(); |
|
| 808 | 18 | assert($iterator instanceof Iterator); |
|
| 809 | 18 | $documents = $iterator->toArray(); |
|
| 810 | 18 | foreach ($documents as $key => $document) { |
|
| 811 | 17 | $collection->add($document); |
|
| 812 | } |
||
| 813 | 18 | } |
|
| 814 | |||
| 815 | 18 | public function createReferenceManyInverseSideQuery(PersistentCollectionInterface $collection) : Query |
|
| 816 | { |
||
| 817 | 18 | $hints = $collection->getHints(); |
|
| 818 | 18 | $mapping = $collection->getMapping(); |
|
| 819 | 18 | $owner = $collection->getOwner(); |
|
| 820 | |||
| 821 | 18 | if ($owner === null) { |
|
| 822 | throw PersistentCollectionException::ownerRequiredToLoadCollection(); |
||
| 823 | } |
||
| 824 | |||
| 825 | 18 | $ownerClass = $this->dm->getClassMetadata(get_class($owner)); |
|
| 826 | 18 | $targetClass = $this->dm->getClassMetadata($mapping['targetDocument']); |
|
| 827 | 18 | $mappedByMapping = $targetClass->fieldMappings[$mapping['mappedBy']] ?? []; |
|
| 828 | 18 | $mappedByFieldName = ClassMetadata::getReferenceFieldName($mappedByMapping['storeAs'] ?? ClassMetadata::REFERENCE_STORE_AS_DB_REF, $mapping['mappedBy']); |
|
| 829 | |||
| 830 | 18 | $criteria = $this->cm->merge( |
|
| 831 | 18 | [$mappedByFieldName => $ownerClass->getIdentifierObject($owner)], |
|
| 832 | 18 | $this->dm->getFilterCollection()->getFilterCriteria($targetClass), |
|
| 833 | 18 | $mapping['criteria'] ?? [] |
|
| 834 | ); |
||
| 835 | 18 | $criteria = $this->uow->getDocumentPersister($mapping['targetDocument'])->prepareQueryOrNewObj($criteria); |
|
| 836 | 18 | $qb = $this->dm->createQueryBuilder($mapping['targetDocument']) |
|
| 837 | 18 | ->setQueryArray($criteria); |
|
| 838 | |||
| 839 | 18 | if (isset($mapping['sort'])) { |
|
| 840 | 18 | $qb->sort($mapping['sort']); |
|
| 841 | } |
||
| 842 | 18 | if (isset($mapping['limit'])) { |
|
| 843 | 2 | $qb->limit($mapping['limit']); |
|
| 844 | } |
||
| 845 | 18 | if (isset($mapping['skip'])) { |
|
| 846 | $qb->skip($mapping['skip']); |
||
| 847 | } |
||
| 848 | |||
| 849 | 18 | if (! empty($hints[Query::HINT_READ_PREFERENCE])) { |
|
| 850 | $qb->setReadPreference($hints[Query::HINT_READ_PREFERENCE]); |
||
| 851 | } |
||
| 852 | |||
| 853 | 18 | foreach ($mapping['prime'] as $field) { |
|
| 854 | 4 | $qb->field($field)->prime(true); |
|
| 855 | } |
||
| 856 | |||
| 857 | 18 | return $qb->getQuery(); |
|
| 858 | } |
||
| 859 | |||
| 860 | 5 | private function loadReferenceManyWithRepositoryMethod(PersistentCollectionInterface $collection) : void |
|
| 861 | { |
||
| 862 | 5 | $cursor = $this->createReferenceManyWithRepositoryMethodCursor($collection); |
|
| 863 | 5 | $mapping = $collection->getMapping(); |
|
| 864 | 5 | $documents = $cursor->toArray(); |
|
| 865 | 5 | foreach ($documents as $key => $obj) { |
|
| 866 | 5 | if (CollectionHelper::isHash($mapping['strategy'])) { |
|
| 867 | 1 | $collection->set($key, $obj); |
|
| 868 | } else { |
||
| 869 | 4 | $collection->add($obj); |
|
| 870 | } |
||
| 871 | } |
||
| 872 | 5 | } |
|
| 873 | |||
| 874 | 5 | public function createReferenceManyWithRepositoryMethodCursor(PersistentCollectionInterface $collection) : Iterator |
|
| 875 | { |
||
| 876 | 5 | $mapping = $collection->getMapping(); |
|
| 877 | 5 | $repositoryMethod = $mapping['repositoryMethod']; |
|
| 878 | 5 | $cursor = $this->dm->getRepository($mapping['targetDocument']) |
|
| 879 | 5 | ->$repositoryMethod($collection->getOwner()); |
|
| 880 | |||
| 881 | 5 | if (! $cursor instanceof Iterator) { |
|
| 882 | throw new BadMethodCallException(sprintf('Expected repository method %s to return an iterable object', $repositoryMethod)); |
||
| 883 | } |
||
| 884 | |||
| 885 | 5 | if (! empty($mapping['prime'])) { |
|
| 886 | 1 | $referencePrimer = new ReferencePrimer($this->dm, $this->dm->getUnitOfWork()); |
|
| 887 | 1 | $primers = array_combine($mapping['prime'], array_fill(0, count($mapping['prime']), true)); |
|
| 888 | 1 | $class = $this->dm->getClassMetadata($mapping['targetDocument']); |
|
| 889 | |||
| 890 | 1 | assert(is_array($primers)); |
|
| 891 | |||
| 892 | 1 | $cursor = new PrimingIterator($cursor, $class, $referencePrimer, $primers, $collection->getHints()); |
|
| 893 | } |
||
| 894 | |||
| 895 | 5 | return $cursor; |
|
| 896 | } |
||
| 897 | |||
| 898 | /** |
||
| 899 | * Prepare a projection array by converting keys, which are PHP property |
||
| 900 | * names, to MongoDB field names. |
||
| 901 | */ |
||
| 902 | 15 | public function prepareProjection(array $fields) : array |
|
| 903 | { |
||
| 904 | 15 | $preparedFields = []; |
|
| 905 | |||
| 906 | 15 | foreach ($fields as $key => $value) { |
|
| 907 | 15 | $preparedFields[$this->prepareFieldName($key)] = $value; |
|
| 908 | } |
||
| 909 | |||
| 910 | 15 | return $preparedFields; |
|
| 911 | } |
||
| 912 | |||
| 913 | /** |
||
| 914 | * @param int|string $sort |
||
| 915 | * |
||
| 916 | * @return int|string|null |
||
| 917 | */ |
||
| 918 | 27 | private function getSortDirection($sort) |
|
| 919 | { |
||
| 920 | 27 | switch (strtolower((string) $sort)) { |
|
| 921 | 27 | case 'desc': |
|
| 922 | 15 | return -1; |
|
| 923 | 24 | case 'asc': |
|
| 924 | 13 | return 1; |
|
| 925 | } |
||
| 926 | |||
| 927 | 14 | return $sort; |
|
| 928 | } |
||
| 929 | |||
| 930 | /** |
||
| 931 | * Prepare a sort specification array by converting keys to MongoDB field |
||
| 932 | * names and changing direction strings to int. |
||
| 933 | */ |
||
| 934 | 144 | public function prepareSort(array $fields) : array |
|
| 935 | { |
||
| 936 | 144 | $sortFields = []; |
|
| 937 | |||
| 938 | 144 | foreach ($fields as $key => $value) { |
|
| 939 | 27 | if (is_array($value)) { |
|
| 940 | 1 | $sortFields[$this->prepareFieldName($key)] = $value; |
|
| 941 | } else { |
||
| 942 | 27 | $sortFields[$this->prepareFieldName($key)] = $this->getSortDirection($value); |
|
| 943 | } |
||
| 944 | } |
||
| 945 | |||
| 946 | 144 | return $sortFields; |
|
| 947 | } |
||
| 948 | |||
| 949 | /** |
||
| 950 | * Prepare a mongodb field name and convert the PHP property names to |
||
| 951 | * MongoDB field names. |
||
| 952 | */ |
||
| 953 | 475 | public function prepareFieldName(string $fieldName) : string |
|
| 959 | |||
| 960 | /** |
||
| 961 | * Adds discriminator criteria to an already-prepared query. |
||
| 962 | * |
||
| 963 | * If the class we're querying has a discriminator field set, we add all |
||
| 964 | * possible discriminator values to the query. The list of possible |
||
| 965 | * discriminator values is based on the discriminatorValue of the class |
||
| 966 | * itself as well as those of all its subclasses. |
||
| 967 | * |
||
| 968 | * This method should be used once for query criteria and not be used for |
||
| 969 | * nested expressions. It should be called before |
||
| 970 | * {@link DocumentPerister::addFilterToPreparedQuery()}. |
||
| 971 | */ |
||
| 972 | 540 | public function addDiscriminatorToPreparedQuery(array $preparedQuery) : array |
|
| 973 | { |
||
| 974 | 540 | if (isset($preparedQuery[$this->class->discriminatorField]) || $this->class->discriminatorField === null) { |
|
| 975 | 517 | return $preparedQuery; |
|
| 976 | } |
||
| 977 | |||
| 978 | 32 | $discriminatorValues = $this->getClassDiscriminatorValues($this->class); |
|
| 979 | |||
| 980 | 32 | if ($discriminatorValues === []) { |
|
| 981 | 1 | return $preparedQuery; |
|
| 982 | } |
||
| 983 | |||
| 984 | 32 | if (count($discriminatorValues) === 1) { |
|
| 985 | 21 | $preparedQuery[$this->class->discriminatorField] = $discriminatorValues[0]; |
|
| 986 | } else { |
||
| 987 | 14 | $preparedQuery[$this->class->discriminatorField] = ['$in' => $discriminatorValues]; |
|
| 988 | } |
||
| 989 | |||
| 990 | 32 | return $preparedQuery; |
|
| 991 | } |
||
| 992 | |||
| 993 | /** |
||
| 994 | * Adds filter criteria to an already-prepared query. |
||
| 995 | * |
||
| 996 | * This method should be used once for query criteria and not be used for |
||
| 997 | * nested expressions. It should be called after |
||
| 998 | * {@link DocumentPerister::addDiscriminatorToPreparedQuery()}. |
||
| 999 | */ |
||
| 1000 | 541 | public function addFilterToPreparedQuery(array $preparedQuery) : array |
|
| 1015 | |||
| 1016 | /** |
||
| 1017 | * Prepares the query criteria or new document object. |
||
| 1018 | * |
||
| 1019 | * PHP field names and types will be converted to those used by MongoDB. |
||
| 1020 | */ |
||
| 1021 | 611 | public function prepareQueryOrNewObj(array $query, bool $isNewObj = false) : array |
|
| 1051 | |||
| 1052 | /** |
||
| 1053 | * Converts a single value to its database representation based on the mapping type |
||
| 1054 | * |
||
| 1055 | * @param mixed $value |
||
| 1056 | * |
||
| 1057 | * @return mixed |
||
| 1058 | */ |
||
| 1059 | 246 | private function convertToDatabaseValue(string $fieldName, $value) |
|
| 1090 | |||
| 1091 | /** |
||
| 1092 | * Prepares a query value and converts the PHP value to the database value |
||
| 1093 | * if it is an identifier. |
||
| 1094 | * |
||
| 1095 | * It also handles converting $fieldName to the database name if they are |
||
| 1096 | * different. |
||
| 1097 | * |
||
| 1098 | * @param mixed $value |
||
| 1099 | */ |
||
| 1100 | 998 | private function prepareQueryElement(string $fieldName, $value = null, ?ClassMetadata $class = null, bool $prepareValue = true, bool $inNewObj = false) : array |
|
| 1293 | |||
| 1294 | 82 | private function prepareQueryExpression(array $expression, ClassMetadata $class) : array |
|
| 1334 | |||
| 1335 | /** |
||
| 1336 | * Checks whether the value has DBRef fields. |
||
| 1337 | * |
||
| 1338 | * This method doesn't check if the the value is a complete DBRef object, |
||
| 1339 | * although it should return true for a DBRef. Rather, we're checking that |
||
| 1340 | * the value has one or more fields for a DBref. In practice, this could be |
||
| 1341 | * $elemMatch criteria for matching a DBRef. |
||
| 1342 | * |
||
| 1343 | * @param mixed $value |
||
| 1344 | */ |
||
| 1345 | 83 | private function hasDBRefFields($value) : bool |
|
| 1363 | |||
| 1364 | /** |
||
| 1365 | * Checks whether the value has query operators. |
||
| 1366 | * |
||
| 1367 | * @param mixed $value |
||
| 1368 | */ |
||
| 1369 | 87 | private function hasQueryOperators($value) : bool |
|
| 1387 | |||
| 1388 | /** |
||
| 1389 | * Returns the list of discriminator values for the given ClassMetadata |
||
| 1390 | */ |
||
| 1391 | 32 | private function getClassDiscriminatorValues(ClassMetadata $metadata) : array |
|
| 1415 | |||
| 1416 | 607 | private function handleCollections(object $document, array $options) : void |
|
| 1447 | |||
| 1448 | /** |
||
| 1449 | * If the document is new, ignore shard key field value, otherwise throw an |
||
| 1450 | * exception. Also, shard key field should be present in actual document |
||
| 1451 | * data. |
||
| 1452 | * |
||
| 1453 | * @throws MongoDBException |
||
| 1454 | */ |
||
| 1455 | 10 | private function guardMissingShardKey(object $document, string $shardKeyField, array $actualDocumentData) : void |
|
| 1471 | |||
| 1472 | /** |
||
| 1473 | * Get shard key aware query for single document. |
||
| 1474 | */ |
||
| 1475 | 317 | private function getQueryForDocument(object $document) : array |
|
| 1484 | |||
| 1485 | 618 | private function getWriteOptions(array $options = []) : array |
|
| 1495 | |||
| 1496 | 16 | private function prepareReference(string $fieldName, $value, array $mapping, bool $inNewObj) : array |
|
| 1536 | } |
||
| 1537 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.