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