Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
| 45 | class DocumentPersister |
||
| 46 | { |
||
| 47 | /** |
||
| 48 | * The PersistenceBuilder instance. |
||
| 49 | * |
||
| 50 | * @var PersistenceBuilder |
||
| 51 | */ |
||
| 52 | private $pb; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * The DocumentManager instance. |
||
| 56 | * |
||
| 57 | * @var DocumentManager |
||
| 58 | */ |
||
| 59 | private $dm; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * The EventManager instance |
||
| 63 | * |
||
| 64 | * @var EventManager |
||
| 65 | */ |
||
| 66 | private $evm; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * The UnitOfWork instance. |
||
| 70 | * |
||
| 71 | * @var UnitOfWork |
||
| 72 | */ |
||
| 73 | private $uow; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * The ClassMetadata instance for the document type being persisted. |
||
| 77 | * |
||
| 78 | * @var ClassMetadata |
||
| 79 | */ |
||
| 80 | private $class; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * The MongoCollection instance for this document. |
||
| 84 | * |
||
| 85 | * @var \MongoCollection |
||
| 86 | */ |
||
| 87 | private $collection; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Array of queued inserts for the persister to insert. |
||
| 91 | * |
||
| 92 | * @var array |
||
| 93 | */ |
||
| 94 | private $queuedInserts = array(); |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Array of queued inserts for the persister to insert. |
||
| 98 | * |
||
| 99 | * @var array |
||
| 100 | */ |
||
| 101 | private $queuedUpserts = array(); |
||
| 102 | |||
| 103 | /** |
||
| 104 | * The CriteriaMerger instance. |
||
| 105 | * |
||
| 106 | * @var CriteriaMerger |
||
| 107 | */ |
||
| 108 | private $cm; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * The CollectionPersister instance. |
||
| 112 | * |
||
| 113 | * @var CollectionPersister |
||
| 114 | */ |
||
| 115 | private $cp; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Initializes a new DocumentPersister instance. |
||
| 119 | * |
||
| 120 | * @param PersistenceBuilder $pb |
||
| 121 | * @param DocumentManager $dm |
||
| 122 | * @param EventManager $evm |
||
| 123 | * @param UnitOfWork $uow |
||
| 124 | * @param HydratorFactory $hydratorFactory |
||
| 125 | * @param ClassMetadata $class |
||
| 126 | */ |
||
| 127 | 673 | public function __construct(PersistenceBuilder $pb, DocumentManager $dm, EventManager $evm, UnitOfWork $uow, HydratorFactory $hydratorFactory, ClassMetadata $class, CriteriaMerger $cm = null) |
|
| 128 | { |
||
| 129 | 673 | $this->pb = $pb; |
|
| 130 | 673 | $this->dm = $dm; |
|
| 131 | 673 | $this->evm = $evm; |
|
| 132 | 673 | $this->cm = $cm ?: new CriteriaMerger(); |
|
| 133 | 673 | $this->uow = $uow; |
|
| 134 | 673 | $this->hydratorFactory = $hydratorFactory; |
|
|
|
|||
| 135 | 673 | $this->class = $class; |
|
| 136 | 673 | $this->collection = $dm->getDocumentCollection($class->name); |
|
| 137 | 673 | $this->cp = $this->uow->getCollectionPersister(); |
|
| 138 | 673 | } |
|
| 139 | |||
| 140 | /** |
||
| 141 | * @return array |
||
| 142 | */ |
||
| 143 | public function getInserts() |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @param object $document |
||
| 150 | * @return bool |
||
| 151 | */ |
||
| 152 | public function isQueuedForInsert($document) |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Adds a document to the queued insertions. |
||
| 159 | * The document remains queued until {@link executeInserts} is invoked. |
||
| 160 | * |
||
| 161 | * @param object $document The document to queue for insertion. |
||
| 162 | */ |
||
| 163 | 476 | public function addInsert($document) |
|
| 167 | |||
| 168 | /** |
||
| 169 | * @return array |
||
| 170 | */ |
||
| 171 | public function getUpserts() |
||
| 175 | |||
| 176 | /** |
||
| 177 | * @param object $document |
||
| 178 | * @return boolean |
||
| 179 | */ |
||
| 180 | public function isQueuedForUpsert($document) |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Adds a document to the queued upserts. |
||
| 187 | * The document remains queued until {@link executeUpserts} is invoked. |
||
| 188 | * |
||
| 189 | * @param object $document The document to queue for insertion. |
||
| 190 | */ |
||
| 191 | 76 | public function addUpsert($document) |
|
| 195 | |||
| 196 | /** |
||
| 197 | * Gets the ClassMetadata instance of the document class this persister is used for. |
||
| 198 | * |
||
| 199 | * @return ClassMetadata |
||
| 200 | */ |
||
| 201 | public function getClassMetadata() |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Executes all queued document insertions. |
||
| 208 | * |
||
| 209 | * Queued documents without an ID will inserted in a batch and queued |
||
| 210 | * documents with an ID will be upserted individually. |
||
| 211 | * |
||
| 212 | * If no inserts are queued, invoking this method is a NOOP. |
||
| 213 | * |
||
| 214 | * @param array $options Options for batchInsert() and update() driver methods |
||
| 215 | */ |
||
| 216 | 476 | public function executeInserts(array $options = array()) |
|
| 217 | { |
||
| 218 | 476 | if ( ! $this->queuedInserts) { |
|
| 219 | return; |
||
| 220 | } |
||
| 221 | |||
| 222 | 476 | $inserts = array(); |
|
| 223 | 476 | foreach ($this->queuedInserts as $oid => $document) { |
|
| 224 | 476 | $data = $this->pb->prepareInsertData($document); |
|
| 225 | |||
| 226 | // Set the initial version for each insert |
||
| 227 | 475 | View Code Duplication | if ($this->class->isVersioned) { |
| 228 | 38 | $versionMapping = $this->class->fieldMappings[$this->class->versionField]; |
|
| 229 | 38 | if ($versionMapping['type'] === 'int') { |
|
| 230 | 36 | $nextVersion = max(1, (int) $this->class->reflFields[$this->class->versionField]->getValue($document)); |
|
| 231 | 36 | $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersion); |
|
| 232 | 38 | } elseif ($versionMapping['type'] === 'date') { |
|
| 233 | 2 | $nextVersionDateTime = new \DateTime(); |
|
| 234 | 2 | $nextVersion = new \MongoDate($nextVersionDateTime->getTimestamp()); |
|
| 235 | 2 | $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersionDateTime); |
|
| 236 | 2 | } |
|
| 237 | 38 | $data[$versionMapping['name']] = $nextVersion; |
|
| 238 | 38 | } |
|
| 239 | |||
| 240 | 475 | $inserts[$oid] = $data; |
|
| 241 | 475 | } |
|
| 242 | |||
| 243 | 475 | if ($inserts) { |
|
| 244 | try { |
||
| 245 | 475 | $this->collection->batchInsert($inserts, $options); |
|
| 246 | 475 | } catch (\MongoException $e) { |
|
| 247 | 7 | $this->queuedInserts = array(); |
|
| 248 | 7 | throw $e; |
|
| 249 | } |
||
| 250 | 475 | } |
|
| 251 | |||
| 252 | /* All collections except for ones using addToSet have already been |
||
| 253 | * saved. We have left these to be handled separately to avoid checking |
||
| 254 | * collection for uniqueness on PHP side. |
||
| 255 | */ |
||
| 256 | 475 | foreach ($this->queuedInserts as $document) { |
|
| 257 | 475 | $this->handleCollections($document, $options); |
|
| 258 | 475 | } |
|
| 259 | |||
| 260 | 475 | $this->queuedInserts = array(); |
|
| 261 | 475 | } |
|
| 262 | |||
| 263 | /** |
||
| 264 | * Executes all queued document upserts. |
||
| 265 | * |
||
| 266 | * Queued documents with an ID are upserted individually. |
||
| 267 | * |
||
| 268 | * If no upserts are queued, invoking this method is a NOOP. |
||
| 269 | * |
||
| 270 | * @param array $options Options for batchInsert() and update() driver methods |
||
| 271 | */ |
||
| 272 | 76 | public function executeUpserts(array $options = array()) |
|
| 273 | { |
||
| 274 | 76 | if ( ! $this->queuedUpserts) { |
|
| 275 | return; |
||
| 276 | } |
||
| 277 | |||
| 278 | 76 | foreach ($this->queuedUpserts as $oid => $document) { |
|
| 279 | 76 | $data = $this->pb->prepareUpsertData($document); |
|
| 280 | |||
| 281 | // Set the initial version for each upsert |
||
| 282 | 76 | View Code Duplication | if ($this->class->isVersioned) { |
| 283 | 3 | $versionMapping = $this->class->fieldMappings[$this->class->versionField]; |
|
| 284 | 3 | if ($versionMapping['type'] === 'int') { |
|
| 285 | 2 | $nextVersion = max(1, (int) $this->class->reflFields[$this->class->versionField]->getValue($document)); |
|
| 286 | 2 | $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersion); |
|
| 287 | 3 | } elseif ($versionMapping['type'] === 'date') { |
|
| 288 | 1 | $nextVersionDateTime = new \DateTime(); |
|
| 289 | 1 | $nextVersion = new \MongoDate($nextVersionDateTime->getTimestamp()); |
|
| 290 | 1 | $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersionDateTime); |
|
| 291 | 1 | } |
|
| 292 | 3 | $data['$set'][$versionMapping['name']] = $nextVersion; |
|
| 293 | 3 | } |
|
| 294 | |||
| 295 | try { |
||
| 296 | 76 | $this->executeUpsert($data, $options); |
|
| 297 | 76 | $this->handleCollections($document, $options); |
|
| 298 | 76 | unset($this->queuedUpserts[$oid]); |
|
| 299 | 76 | } catch (\MongoException $e) { |
|
| 300 | unset($this->queuedUpserts[$oid]); |
||
| 301 | throw $e; |
||
| 302 | } |
||
| 303 | 76 | } |
|
| 304 | 76 | } |
|
| 305 | |||
| 306 | /** |
||
| 307 | * Executes a single upsert in {@link executeInserts} |
||
| 308 | * |
||
| 309 | * @param array $data |
||
| 310 | * @param array $options |
||
| 311 | */ |
||
| 312 | 76 | private function executeUpsert(array $data, array $options) |
|
| 352 | |||
| 353 | /** |
||
| 354 | * Updates the already persisted document if it has any new changesets. |
||
| 355 | * |
||
| 356 | * @param object $document |
||
| 357 | * @param array $options Array of options to be used with update() |
||
| 358 | * @throws \Doctrine\ODM\MongoDB\LockException |
||
| 359 | */ |
||
| 360 | public function update($document, array $options = array()) |
||
| 361 | { |
||
| 362 | $id = $this->uow->getDocumentIdentifier($document); |
||
| 363 | $update = $this->pb->prepareUpdateData($document); |
||
| 364 | |||
| 365 | $id = $this->class->getDatabaseIdentifierValue($id); |
||
| 366 | $query = array('_id' => $id); |
||
| 367 | |||
| 368 | // Include versioning logic to set the new version value in the database |
||
| 369 | // and to ensure the version has not changed since this document object instance |
||
| 370 | // was fetched from the database |
||
| 371 | if ($this->class->isVersioned) { |
||
| 372 | $versionMapping = $this->class->fieldMappings[$this->class->versionField]; |
||
| 373 | $currentVersion = $this->class->reflFields[$this->class->versionField]->getValue($document); |
||
| 374 | if ($versionMapping['type'] === 'int') { |
||
| 375 | $nextVersion = $currentVersion + 1; |
||
| 376 | $update['$inc'][$versionMapping['name']] = 1; |
||
| 377 | $query[$versionMapping['name']] = $currentVersion; |
||
| 378 | $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersion); |
||
| 379 | } elseif ($versionMapping['type'] === 'date') { |
||
| 380 | $nextVersion = new \DateTime(); |
||
| 381 | $update['$set'][$versionMapping['name']] = new \MongoDate($nextVersion->getTimestamp()); |
||
| 382 | $query[$versionMapping['name']] = new \MongoDate($currentVersion->getTimestamp()); |
||
| 383 | $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersion); |
||
| 384 | } |
||
| 385 | } |
||
| 386 | |||
| 387 | if ( ! empty($update)) { |
||
| 388 | // Include locking logic so that if the document object in memory is currently |
||
| 389 | // locked then it will remove it, otherwise it ensures the document is not locked. |
||
| 390 | if ($this->class->isLockable) { |
||
| 391 | $isLocked = $this->class->reflFields[$this->class->lockField]->getValue($document); |
||
| 392 | $lockMapping = $this->class->fieldMappings[$this->class->lockField]; |
||
| 393 | if ($isLocked) { |
||
| 394 | $update['$unset'] = array($lockMapping['name'] => true); |
||
| 395 | } else { |
||
| 396 | $query[$lockMapping['name']] = array('$exists' => false); |
||
| 397 | } |
||
| 398 | } |
||
| 399 | |||
| 400 | $result = $this->collection->update($query, $update, $options); |
||
| 401 | |||
| 402 | View Code Duplication | if (($this->class->isVersioned || $this->class->isLockable) && ! $result['n']) { |
|
| 403 | throw LockException::lockFailed($document); |
||
| 404 | } |
||
| 405 | } |
||
| 406 | |||
| 407 | $this->handleCollections($document, $options); |
||
| 408 | } |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Removes document from mongo |
||
| 412 | * |
||
| 413 | * @param mixed $document |
||
| 414 | * @param array $options Array of options to be used with remove() |
||
| 415 | * @throws \Doctrine\ODM\MongoDB\LockException |
||
| 416 | */ |
||
| 417 | 11 | public function delete($document, array $options = array()) |
|
| 432 | |||
| 433 | /** |
||
| 434 | * Refreshes a managed document. |
||
| 435 | * |
||
| 436 | * @param array $id The identifier of the document. |
||
| 437 | * @param object $document The document to refresh. |
||
| 438 | */ |
||
| 439 | 15 | public function refresh($id, $document) |
|
| 445 | |||
| 446 | /** |
||
| 447 | * Finds a document by a set of criteria. |
||
| 448 | * |
||
| 449 | * If a scalar or MongoId is provided for $criteria, it will be used to |
||
| 450 | * match an _id value. |
||
| 451 | * |
||
| 452 | * @param mixed $criteria Query criteria |
||
| 453 | * @param object $document Document to load the data into. If not specified, a new document is created. |
||
| 454 | * @param array $hints Hints for document creation |
||
| 455 | * @param integer $lockMode |
||
| 456 | * @param array $sort Sort array for Cursor::sort() |
||
| 457 | * @throws \Doctrine\ODM\MongoDB\LockException |
||
| 458 | * @return object|null The loaded and managed document instance or null if no document was found |
||
| 459 | * @todo Check identity map? loadById method? Try to guess whether $criteria is the id? |
||
| 460 | */ |
||
| 461 | 310 | public function load($criteria, $document = null, array $hints = array(), $lockMode = 0, array $sort = null) |
|
| 489 | |||
| 490 | /** |
||
| 491 | * Finds documents by a set of criteria. |
||
| 492 | * |
||
| 493 | * @param array $criteria Query criteria |
||
| 494 | * @param array $sort Sort array for Cursor::sort() |
||
| 495 | * @param integer|null $limit Limit for Cursor::limit() |
||
| 496 | * @param integer|null $skip Skip for Cursor::skip() |
||
| 497 | * @return Cursor |
||
| 498 | */ |
||
| 499 | 22 | public function loadAll(array $criteria = array(), array $sort = null, $limit = null, $skip = null) |
|
| 522 | |||
| 523 | /** |
||
| 524 | * Wraps the supplied base cursor in the corresponding ODM class. |
||
| 525 | * |
||
| 526 | * @param CursorInterface $baseCursor |
||
| 527 | * @return Cursor |
||
| 528 | */ |
||
| 529 | 22 | private function wrapCursor(CursorInterface $baseCursor) |
|
| 533 | |||
| 534 | /** |
||
| 535 | * Checks whether the given managed document exists in the database. |
||
| 536 | * |
||
| 537 | * @param object $document |
||
| 538 | * @return boolean TRUE if the document exists in the database, FALSE otherwise. |
||
| 539 | */ |
||
| 540 | 3 | public function exists($document) |
|
| 545 | |||
| 546 | /** |
||
| 547 | * Locks document by storing the lock mode on the mapped lock field. |
||
| 548 | * |
||
| 549 | * @param object $document |
||
| 550 | * @param int $lockMode |
||
| 551 | */ |
||
| 552 | 5 | public function lock($document, $lockMode) |
|
| 560 | |||
| 561 | /** |
||
| 562 | * Releases any lock that exists on this document. |
||
| 563 | * |
||
| 564 | * @param object $document |
||
| 565 | */ |
||
| 566 | 1 | public function unlock($document) |
|
| 574 | |||
| 575 | /** |
||
| 576 | * Creates or fills a single document object from an query result. |
||
| 577 | * |
||
| 578 | * @param object $result The query result. |
||
| 579 | * @param object $document The document object to fill, if any. |
||
| 580 | * @param array $hints Hints for document creation. |
||
| 581 | * @return object The filled and managed document object or NULL, if the query result is empty. |
||
| 582 | */ |
||
| 583 | 309 | private function createDocument($result, $document = null, array $hints = array()) |
|
| 597 | |||
| 598 | /** |
||
| 599 | * Loads a PersistentCollection data. Used in the initialize() method. |
||
| 600 | * |
||
| 601 | * @param PersistentCollection $collection |
||
| 602 | */ |
||
| 603 | 123 | public function loadCollection(PersistentCollection $collection) |
|
| 624 | |||
| 625 | 77 | private function loadEmbedManyCollection(PersistentCollection $collection) |
|
| 652 | |||
| 653 | 38 | private function loadReferenceManyCollectionOwningSide(PersistentCollection $collection) |
|
| 654 | { |
||
| 655 | 38 | $hints = $collection->getHints(); |
|
| 656 | 38 | $mapping = $collection->getMapping(); |
|
| 657 | 38 | $groupedIds = array(); |
|
| 658 | |||
| 659 | 38 | $sorted = isset($mapping['sort']) && $mapping['sort']; |
|
| 660 | |||
| 661 | 38 | foreach ($collection->getMongoData() as $key => $reference) { |
|
| 662 | 35 | if (isset($mapping['simple']) && $mapping['simple']) { |
|
| 663 | 4 | $className = $mapping['targetDocument']; |
|
| 664 | 4 | $mongoId = $reference; |
|
| 665 | 4 | } else { |
|
| 666 | 31 | $className = $this->uow->getClassNameForAssociation($mapping, $reference); |
|
| 667 | 31 | $mongoId = $reference['$id']; |
|
| 668 | } |
||
| 669 | 35 | $id = $this->dm->getClassMetadata($className)->getPHPIdentifierValue($mongoId); |
|
| 670 | |||
| 671 | // create a reference to the class and id |
||
| 672 | 35 | $reference = $this->dm->getReference($className, $id); |
|
| 673 | |||
| 674 | // no custom sort so add the references right now in the order they are embedded |
||
| 675 | 35 | if ( ! $sorted) { |
|
| 676 | 34 | if (CollectionHelper::isHash($mapping['strategy'])) { |
|
| 677 | $collection->set($key, $reference); |
||
| 678 | } else { |
||
| 679 | 34 | $collection->add($reference); |
|
| 680 | } |
||
| 681 | 34 | } |
|
| 682 | |||
| 683 | // only query for the referenced object if it is not already initialized or the collection is sorted |
||
| 684 | 35 | if (($reference instanceof Proxy && ! $reference->__isInitialized__) || $sorted) { |
|
| 685 | 28 | $groupedIds[$className][] = $mongoId; |
|
| 686 | 28 | } |
|
| 687 | 38 | } |
|
| 688 | 38 | foreach ($groupedIds as $className => $ids) { |
|
| 689 | 28 | $class = $this->dm->getClassMetadata($className); |
|
| 690 | 28 | $mongoCollection = $this->dm->getDocumentCollection($className); |
|
| 691 | 28 | $criteria = $this->cm->merge( |
|
| 692 | 28 | array('_id' => array('$in' => array_values($ids))), |
|
| 693 | 28 | $this->dm->getFilterCollection()->getFilterCriteria($class), |
|
| 694 | 28 | isset($mapping['criteria']) ? $mapping['criteria'] : array() |
|
| 695 | 28 | ); |
|
| 696 | 28 | $criteria = $this->uow->getDocumentPersister($className)->prepareQueryOrNewObj($criteria); |
|
| 697 | 28 | $cursor = $mongoCollection->find($criteria); |
|
| 698 | 28 | if (isset($mapping['sort'])) { |
|
| 699 | 28 | $cursor->sort($mapping['sort']); |
|
| 700 | 28 | } |
|
| 701 | 28 | if (isset($mapping['limit'])) { |
|
| 702 | $cursor->limit($mapping['limit']); |
||
| 703 | } |
||
| 704 | 28 | if (isset($mapping['skip'])) { |
|
| 705 | $cursor->skip($mapping['skip']); |
||
| 706 | } |
||
| 707 | 28 | if ( ! empty($hints[Query::HINT_SLAVE_OKAY])) { |
|
| 708 | $cursor->slaveOkay(true); |
||
| 709 | } |
||
| 710 | 28 | View Code Duplication | if ( ! empty($hints[Query::HINT_READ_PREFERENCE])) { |
| 711 | $cursor->setReadPreference($hints[Query::HINT_READ_PREFERENCE], $hints[Query::HINT_READ_PREFERENCE_TAGS]); |
||
| 712 | } |
||
| 713 | 28 | $documents = $cursor->toArray(false); |
|
| 714 | 28 | foreach ($documents as $documentData) { |
|
| 715 | 27 | $document = $this->uow->getById($documentData['_id'], $class); |
|
| 716 | 27 | $data = $this->hydratorFactory->hydrate($document, $documentData); |
|
| 717 | 27 | $this->uow->setOriginalDocumentData($document, $data); |
|
| 718 | 27 | $document->__isInitialized__ = true; |
|
| 719 | 27 | if ($sorted) { |
|
| 720 | 1 | $collection->add($document); |
|
| 721 | 1 | } |
|
| 722 | 28 | } |
|
| 723 | 38 | } |
|
| 724 | 38 | } |
|
| 725 | |||
| 726 | 14 | private function loadReferenceManyCollectionInverseSide(PersistentCollection $collection) |
|
| 734 | |||
| 735 | /** |
||
| 736 | * @param PersistentCollection $collection |
||
| 737 | * |
||
| 738 | * @return Query |
||
| 739 | */ |
||
| 740 | 16 | public function createReferenceManyInverseSideQuery(PersistentCollection $collection) |
|
| 776 | |||
| 777 | 3 | private function loadReferenceManyWithRepositoryMethod(PersistentCollection $collection) |
|
| 790 | |||
| 791 | /** |
||
| 792 | * @param PersistentCollection $collection |
||
| 793 | * |
||
| 794 | * @return CursorInterface |
||
| 795 | */ |
||
| 796 | 3 | public function createReferenceManyWithRepositoryMethodCursor(PersistentCollection $collection) |
|
| 825 | |||
| 826 | /** |
||
| 827 | * Prepare a sort or projection array by converting keys, which are PHP |
||
| 828 | * property names, to MongoDB field names. |
||
| 829 | * |
||
| 830 | * @param array $fields |
||
| 831 | * @return array |
||
| 832 | */ |
||
| 833 | 127 | public function prepareSortOrProjection(array $fields) |
|
| 843 | |||
| 844 | /** |
||
| 845 | * Prepare a mongodb field name and convert the PHP property names to MongoDB field names. |
||
| 846 | * |
||
| 847 | * @param string $fieldName |
||
| 848 | * @return string |
||
| 849 | */ |
||
| 850 | 85 | public function prepareFieldName($fieldName) |
|
| 856 | |||
| 857 | /** |
||
| 858 | * Adds discriminator criteria to an already-prepared query. |
||
| 859 | * |
||
| 860 | * This method should be used once for query criteria and not be used for |
||
| 861 | * nested expressions. It should be called before |
||
| 862 | * {@link DocumentPerister::addFilterToPreparedQuery()}. |
||
| 863 | * |
||
| 864 | * @param array $preparedQuery |
||
| 865 | * @return array |
||
| 866 | */ |
||
| 867 | 430 | public function addDiscriminatorToPreparedQuery(array $preparedQuery) |
|
| 883 | |||
| 884 | /** |
||
| 885 | * Adds filter criteria to an already-prepared query. |
||
| 886 | * |
||
| 887 | * This method should be used once for query criteria and not be used for |
||
| 888 | * nested expressions. It should be called after |
||
| 889 | * {@link DocumentPerister::addDiscriminatorToPreparedQuery()}. |
||
| 890 | * |
||
| 891 | * @param array $preparedQuery |
||
| 892 | * @return array |
||
| 893 | */ |
||
| 894 | 431 | public function addFilterToPreparedQuery(array $preparedQuery) |
|
| 908 | |||
| 909 | /** |
||
| 910 | * Prepares the query criteria or new document object. |
||
| 911 | * |
||
| 912 | * PHP field names and types will be converted to those used by MongoDB. |
||
| 913 | * |
||
| 914 | * @param array $query |
||
| 915 | * @return array |
||
| 916 | */ |
||
| 917 | 464 | public function prepareQueryOrNewObj(array $query) |
|
| 944 | |||
| 945 | /** |
||
| 946 | * Prepares a query value and converts the PHP value to the database value |
||
| 947 | * if it is an identifier. |
||
| 948 | * |
||
| 949 | * It also handles converting $fieldName to the database name if they are different. |
||
| 950 | * |
||
| 951 | * @param string $fieldName |
||
| 952 | * @param mixed $value |
||
| 953 | * @param ClassMetadata $class Defaults to $this->class |
||
| 954 | * @param boolean $prepareValue Whether or not to prepare the value |
||
| 955 | * @return array Prepared field name and value |
||
| 956 | */ |
||
| 957 | 456 | private function prepareQueryElement($fieldName, $value = null, $class = null, $prepareValue = true) |
|
| 958 | { |
||
| 959 | 456 | $class = isset($class) ? $class : $this->class; |
|
| 960 | |||
| 961 | // @todo Consider inlining calls to ClassMetadataInfo methods |
||
| 962 | |||
| 963 | // Process all non-identifier fields by translating field names |
||
| 964 | 456 | if ($class->hasField($fieldName) && ! $class->isIdentifier($fieldName)) { |
|
| 965 | 214 | $mapping = $class->fieldMappings[$fieldName]; |
|
| 966 | 214 | $fieldName = $mapping['name']; |
|
| 967 | |||
| 968 | 214 | if ( ! $prepareValue) { |
|
| 969 | 62 | return array($fieldName, $value); |
|
| 970 | } |
||
| 971 | |||
| 972 | // Prepare mapped, embedded objects |
||
| 973 | 172 | if ( ! empty($mapping['embedded']) && is_object($value) && |
|
| 974 | 172 | ! $this->dm->getMetadataFactory()->isTransient(get_class($value))) { |
|
| 975 | return array($fieldName, $this->pb->prepareEmbeddedDocumentValue($mapping, $value)); |
||
| 976 | } |
||
| 977 | |||
| 978 | // No further preparation unless we're dealing with a simple reference |
||
| 979 | // We can't have expressions in empty() with PHP < 5.5, so store it in a variable |
||
| 980 | 172 | $arrayValue = (array) $value; |
|
| 981 | 172 | if (empty($mapping['reference']) || empty($mapping['simple']) || empty($arrayValue)) { |
|
| 982 | 108 | return array($fieldName, $value); |
|
| 983 | } |
||
| 984 | |||
| 985 | // Additional preparation for one or more simple reference values |
||
| 986 | 89 | $targetClass = $this->dm->getClassMetadata($mapping['targetDocument']); |
|
| 987 | |||
| 988 | 89 | if ( ! is_array($value)) { |
|
| 989 | 84 | return array($fieldName, $targetClass->getDatabaseIdentifierValue($value)); |
|
| 990 | } |
||
| 991 | |||
| 992 | // Objects without operators or with DBRef fields can be converted immediately |
||
| 993 | 7 | View Code Duplication | if ( ! $this->hasQueryOperators($value) || $this->hasDBRefFields($value)) { |
| 994 | 3 | return array($fieldName, $targetClass->getDatabaseIdentifierValue($value)); |
|
| 995 | } |
||
| 996 | |||
| 997 | 7 | return array($fieldName, $this->prepareQueryExpression($value, $targetClass)); |
|
| 998 | } |
||
| 999 | |||
| 1000 | // Process identifier fields |
||
| 1001 | 341 | if (($class->hasField($fieldName) && $class->isIdentifier($fieldName)) || $fieldName === '_id') { |
|
| 1002 | 277 | $fieldName = '_id'; |
|
| 1003 | |||
| 1004 | 277 | if ( ! $prepareValue) { |
|
| 1005 | 16 | return array($fieldName, $value); |
|
| 1006 | } |
||
| 1007 | |||
| 1008 | 263 | if ( ! is_array($value)) { |
|
| 1009 | 241 | return array($fieldName, $class->getDatabaseIdentifierValue($value)); |
|
| 1010 | } |
||
| 1011 | |||
| 1012 | // Objects without operators or with DBRef fields can be converted immediately |
||
| 1013 | 47 | View Code Duplication | if ( ! $this->hasQueryOperators($value) || $this->hasDBRefFields($value)) { |
| 1014 | 6 | return array($fieldName, $class->getDatabaseIdentifierValue($value)); |
|
| 1015 | } |
||
| 1016 | |||
| 1017 | 42 | return array($fieldName, $this->prepareQueryExpression($value, $class)); |
|
| 1018 | } |
||
| 1019 | |||
| 1020 | // No processing for unmapped, non-identifier, non-dotted field names |
||
| 1021 | 96 | if (strpos($fieldName, '.') === false) { |
|
| 1022 | 42 | return array($fieldName, $value); |
|
| 1023 | } |
||
| 1024 | |||
| 1025 | /* Process "fieldName.objectProperty" queries (on arrays or objects). |
||
| 1026 | * |
||
| 1027 | * We can limit parsing here, since at most three segments are |
||
| 1028 | * significant: "fieldName.objectProperty" with an optional index or key |
||
| 1029 | * for collections stored as either BSON arrays or objects. |
||
| 1030 | */ |
||
| 1031 | 59 | $e = explode('.', $fieldName, 4); |
|
| 1032 | |||
| 1033 | // No further processing for unmapped fields |
||
| 1034 | 59 | if ( ! isset($class->fieldMappings[$e[0]])) { |
|
| 1035 | 3 | return array($fieldName, $value); |
|
| 1036 | } |
||
| 1037 | |||
| 1038 | 57 | $mapping = $class->fieldMappings[$e[0]]; |
|
| 1039 | 57 | $e[0] = $mapping['name']; |
|
| 1040 | |||
| 1041 | // Hash and raw fields will not be prepared beyond the field name |
||
| 1042 | 57 | if ($mapping['type'] === Type::HASH || $mapping['type'] === Type::RAW) { |
|
| 1043 | 1 | $fieldName = implode('.', $e); |
|
| 1044 | |||
| 1045 | 1 | return array($fieldName, $value); |
|
| 1046 | } |
||
| 1047 | |||
| 1048 | 56 | if (isset($mapping['strategy']) && CollectionHelper::isHash($mapping['strategy']) |
|
| 1049 | 56 | && isset($e[2])) { |
|
| 1050 | 1 | $objectProperty = $e[2]; |
|
| 1051 | 1 | $objectPropertyPrefix = $e[1] . '.'; |
|
| 1052 | 1 | $nextObjectProperty = implode('.', array_slice($e, 3)); |
|
| 1053 | 56 | } elseif ($e[1] != '$') { |
|
| 1054 | 54 | $fieldName = $e[0] . '.' . $e[1]; |
|
| 1055 | 54 | $objectProperty = $e[1]; |
|
| 1056 | 54 | $objectPropertyPrefix = ''; |
|
| 1057 | 54 | $nextObjectProperty = implode('.', array_slice($e, 2)); |
|
| 1058 | 55 | } elseif (isset($e[2])) { |
|
| 1059 | 1 | $fieldName = $e[0] . '.' . $e[1] . '.' . $e[2]; |
|
| 1060 | 1 | $objectProperty = $e[2]; |
|
| 1061 | 1 | $objectPropertyPrefix = $e[1] . '.'; |
|
| 1062 | 1 | $nextObjectProperty = implode('.', array_slice($e, 3)); |
|
| 1063 | 1 | } else { |
|
| 1064 | 1 | $fieldName = $e[0] . '.' . $e[1]; |
|
| 1065 | |||
| 1066 | 1 | return array($fieldName, $value); |
|
| 1067 | } |
||
| 1068 | |||
| 1069 | // No further processing for fields without a targetDocument mapping |
||
| 1070 | 56 | if ( ! isset($mapping['targetDocument'])) { |
|
| 1071 | 1 | if ($nextObjectProperty) { |
|
| 1072 | $fieldName .= '.'.$nextObjectProperty; |
||
| 1073 | } |
||
| 1074 | |||
| 1075 | 1 | return array($fieldName, $value); |
|
| 1076 | } |
||
| 1077 | |||
| 1078 | 55 | $targetClass = $this->dm->getClassMetadata($mapping['targetDocument']); |
|
| 1079 | |||
| 1080 | // No further processing for unmapped targetDocument fields |
||
| 1081 | 55 | if ( ! $targetClass->hasField($objectProperty)) { |
|
| 1082 | 23 | if ($nextObjectProperty) { |
|
| 1083 | $fieldName .= '.'.$nextObjectProperty; |
||
| 1084 | } |
||
| 1085 | |||
| 1086 | 23 | return array($fieldName, $value); |
|
| 1087 | } |
||
| 1088 | |||
| 1089 | 34 | $targetMapping = $targetClass->getFieldMapping($objectProperty); |
|
| 1090 | 34 | $objectPropertyIsId = $targetClass->isIdentifier($objectProperty); |
|
| 1091 | |||
| 1092 | // Prepare DBRef identifiers or the mapped field's property path |
||
| 1093 | 34 | $fieldName = ($objectPropertyIsId && ! empty($mapping['reference']) && empty($mapping['simple'])) |
|
| 1094 | 34 | ? $e[0] . '.$id' |
|
| 1095 | 34 | : $e[0] . '.' . $objectPropertyPrefix . $targetMapping['name']; |
|
| 1096 | |||
| 1097 | // Process targetDocument identifier fields |
||
| 1098 | 34 | if ($objectPropertyIsId) { |
|
| 1099 | 14 | if ( ! $prepareValue) { |
|
| 1100 | 1 | return array($fieldName, $value); |
|
| 1101 | } |
||
| 1102 | |||
| 1103 | 13 | if ( ! is_array($value)) { |
|
| 1104 | 2 | return array($fieldName, $targetClass->getDatabaseIdentifierValue($value)); |
|
| 1105 | } |
||
| 1106 | |||
| 1107 | // Objects without operators or with DBRef fields can be converted immediately |
||
| 1108 | 12 | View Code Duplication | if ( ! $this->hasQueryOperators($value) || $this->hasDBRefFields($value)) { |
| 1109 | 3 | return array($fieldName, $targetClass->getDatabaseIdentifierValue($value)); |
|
| 1110 | } |
||
| 1111 | |||
| 1112 | 12 | return array($fieldName, $this->prepareQueryExpression($value, $targetClass)); |
|
| 1113 | } |
||
| 1114 | |||
| 1115 | /* The property path may include a third field segment, excluding the |
||
| 1116 | * collection item pointer. If present, this next object property must |
||
| 1117 | * be processed recursively. |
||
| 1118 | */ |
||
| 1119 | 20 | if ($nextObjectProperty) { |
|
| 1120 | // Respect the targetDocument's class metadata when recursing |
||
| 1121 | 14 | $nextTargetClass = isset($targetMapping['targetDocument']) |
|
| 1122 | 14 | ? $this->dm->getClassMetadata($targetMapping['targetDocument']) |
|
| 1123 | 14 | : null; |
|
| 1124 | |||
| 1125 | 14 | list($key, $value) = $this->prepareQueryElement($nextObjectProperty, $value, $nextTargetClass, $prepareValue); |
|
| 1126 | |||
| 1127 | 14 | $fieldName .= '.' . $key; |
|
| 1128 | 14 | } |
|
| 1129 | |||
| 1130 | 20 | return array($fieldName, $value); |
|
| 1131 | } |
||
| 1132 | |||
| 1133 | /** |
||
| 1134 | * Prepares a query expression. |
||
| 1135 | * |
||
| 1136 | * @param array|object $expression |
||
| 1137 | * @param ClassMetadata $class |
||
| 1138 | * @return array |
||
| 1139 | */ |
||
| 1140 | 61 | private function prepareQueryExpression($expression, $class) |
|
| 1167 | |||
| 1168 | /** |
||
| 1169 | * Checks whether the value has DBRef fields. |
||
| 1170 | * |
||
| 1171 | * This method doesn't check if the the value is a complete DBRef object, |
||
| 1172 | * although it should return true for a DBRef. Rather, we're checking that |
||
| 1173 | * the value has one or more fields for a DBref. In practice, this could be |
||
| 1174 | * $elemMatch criteria for matching a DBRef. |
||
| 1175 | * |
||
| 1176 | * @param mixed $value |
||
| 1177 | * @return boolean |
||
| 1178 | */ |
||
| 1179 | 62 | private function hasDBRefFields($value) |
|
| 1197 | |||
| 1198 | /** |
||
| 1199 | * Checks whether the value has query operators. |
||
| 1200 | * |
||
| 1201 | * @param mixed $value |
||
| 1202 | * @return boolean |
||
| 1203 | */ |
||
| 1204 | 66 | private function hasQueryOperators($value) |
|
| 1222 | |||
| 1223 | /** |
||
| 1224 | * Gets the array of discriminator values for the given ClassMetadata |
||
| 1225 | * |
||
| 1226 | * @param ClassMetadata $metadata |
||
| 1227 | * @return array |
||
| 1228 | */ |
||
| 1229 | 21 | private function getClassDiscriminatorValues(ClassMetadata $metadata) |
|
| 1245 | |||
| 1246 | 539 | private function handleCollections($document, $options) |
|
| 1247 | { |
||
| 1248 | // Collection deletions (deletions of complete collections) |
||
| 1265 | } |
||
| 1266 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: