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 |
||
| 59 | class DocumentPersister |
||
| 60 | { |
||
| 61 | /** |
||
| 62 | * The PersistenceBuilder instance. |
||
| 63 | * |
||
| 64 | * @var PersistenceBuilder |
||
| 65 | */ |
||
| 66 | private $pb; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * The DocumentManager instance. |
||
| 70 | * |
||
| 71 | * @var DocumentManager |
||
| 72 | */ |
||
| 73 | private $dm; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * The UnitOfWork instance. |
||
| 77 | * |
||
| 78 | * @var UnitOfWork |
||
| 79 | */ |
||
| 80 | private $uow; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * The ClassMetadata instance for the document type being persisted. |
||
| 84 | * |
||
| 85 | * @var ClassMetadata |
||
| 86 | */ |
||
| 87 | private $class; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * The MongoCollection instance for this document. |
||
| 91 | * |
||
| 92 | * @var Collection |
||
| 93 | */ |
||
| 94 | private $collection; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Array of queued inserts for the persister to insert. |
||
| 98 | * |
||
| 99 | * @var array |
||
| 100 | */ |
||
| 101 | private $queuedInserts = []; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Array of queued inserts for the persister to insert. |
||
| 105 | * |
||
| 106 | * @var array |
||
| 107 | */ |
||
| 108 | private $queuedUpserts = []; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * The CriteriaMerger instance. |
||
| 112 | * |
||
| 113 | * @var CriteriaMerger |
||
| 114 | */ |
||
| 115 | private $cm; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * The CollectionPersister instance. |
||
| 119 | * |
||
| 120 | * @var CollectionPersister |
||
| 121 | */ |
||
| 122 | private $cp; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * The HydratorFactory instance. |
||
| 126 | * |
||
| 127 | * @var HydratorFactory |
||
| 128 | */ |
||
| 129 | private $hydratorFactory; |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Initializes this instance. |
||
| 133 | * |
||
| 134 | */ |
||
| 135 | 1078 | public function __construct( |
|
| 136 | PersistenceBuilder $pb, |
||
| 137 | DocumentManager $dm, |
||
| 138 | UnitOfWork $uow, |
||
| 139 | HydratorFactory $hydratorFactory, |
||
| 140 | ClassMetadata $class, |
||
| 141 | ?CriteriaMerger $cm = null |
||
| 142 | ) { |
||
| 143 | 1078 | $this->pb = $pb; |
|
| 144 | 1078 | $this->dm = $dm; |
|
| 145 | 1078 | $this->cm = $cm ?: new CriteriaMerger(); |
|
| 146 | 1078 | $this->uow = $uow; |
|
| 147 | 1078 | $this->hydratorFactory = $hydratorFactory; |
|
| 148 | 1078 | $this->class = $class; |
|
| 149 | 1078 | $this->collection = $dm->getDocumentCollection($class->name); |
|
| 150 | 1078 | $this->cp = $this->uow->getCollectionPersister(); |
|
| 151 | 1078 | } |
|
| 152 | |||
| 153 | /** |
||
| 154 | * @return array |
||
| 155 | */ |
||
| 156 | public function getInserts() |
||
| 157 | { |
||
| 158 | return $this->queuedInserts; |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @param object $document |
||
| 163 | * @return bool |
||
| 164 | */ |
||
| 165 | public function isQueuedForInsert($document) |
||
| 166 | { |
||
| 167 | return isset($this->queuedInserts[spl_object_hash($document)]); |
||
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Adds a document to the queued insertions. |
||
| 172 | * The document remains queued until {@link executeInserts} is invoked. |
||
| 173 | * |
||
| 174 | * @param object $document The document to queue for insertion. |
||
| 175 | */ |
||
| 176 | 480 | public function addInsert($document) |
|
| 177 | { |
||
| 178 | 480 | $this->queuedInserts[spl_object_hash($document)] = $document; |
|
| 179 | 480 | } |
|
| 180 | |||
| 181 | /** |
||
| 182 | * @return array |
||
| 183 | */ |
||
| 184 | public function getUpserts() |
||
| 185 | { |
||
| 186 | return $this->queuedUpserts; |
||
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @param object $document |
||
| 191 | * @return bool |
||
| 192 | */ |
||
| 193 | public function isQueuedForUpsert($document) |
||
| 194 | { |
||
| 195 | return isset($this->queuedUpserts[spl_object_hash($document)]); |
||
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Adds a document to the queued upserts. |
||
| 200 | * The document remains queued until {@link executeUpserts} is invoked. |
||
| 201 | * |
||
| 202 | * @param object $document The document to queue for insertion. |
||
| 203 | */ |
||
| 204 | 83 | public function addUpsert($document) |
|
| 205 | { |
||
| 206 | 83 | $this->queuedUpserts[spl_object_hash($document)] = $document; |
|
| 207 | 83 | } |
|
| 208 | |||
| 209 | /** |
||
| 210 | * Gets the ClassMetadata instance of the document class this persister is used for. |
||
| 211 | * |
||
| 212 | * @return ClassMetadata |
||
| 213 | */ |
||
| 214 | public function getClassMetadata() |
||
| 215 | { |
||
| 216 | return $this->class; |
||
| 217 | } |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Executes all queued document insertions. |
||
| 221 | * |
||
| 222 | * Queued documents without an ID will inserted in a batch and queued |
||
| 223 | * documents with an ID will be upserted individually. |
||
| 224 | * |
||
| 225 | * If no inserts are queued, invoking this method is a NOOP. |
||
| 226 | * |
||
| 227 | * @param array $options Options for batchInsert() and update() driver methods |
||
| 228 | */ |
||
| 229 | 480 | public function executeInserts(array $options = []) |
|
| 230 | { |
||
| 231 | 480 | if (! $this->queuedInserts) { |
|
|
|
|||
| 232 | return; |
||
| 233 | } |
||
| 234 | |||
| 235 | 480 | $inserts = []; |
|
| 236 | 480 | $options = $this->getWriteOptions($options); |
|
| 237 | 480 | foreach ($this->queuedInserts as $oid => $document) { |
|
| 238 | 480 | $data = $this->pb->prepareInsertData($document); |
|
| 239 | |||
| 240 | // Set the initial version for each insert |
||
| 241 | 479 | if ($this->class->isVersioned) { |
|
| 242 | 20 | $versionMapping = $this->class->fieldMappings[$this->class->versionField]; |
|
| 243 | 20 | $nextVersion = null; |
|
| 244 | 20 | if ($versionMapping['type'] === 'int') { |
|
| 245 | 18 | $nextVersion = max(1, (int) $this->class->reflFields[$this->class->versionField]->getValue($document)); |
|
| 246 | 18 | $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersion); |
|
| 247 | 2 | } elseif ($versionMapping['type'] === 'date') { |
|
| 248 | 2 | $nextVersionDateTime = new \DateTime(); |
|
| 249 | 2 | $nextVersion = Type::convertPHPToDatabaseValue($nextVersionDateTime); |
|
| 250 | 2 | $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersionDateTime); |
|
| 251 | } |
||
| 252 | 20 | $data[$versionMapping['name']] = $nextVersion; |
|
| 253 | } |
||
| 254 | |||
| 255 | 479 | $inserts[] = $data; |
|
| 256 | } |
||
| 257 | |||
| 258 | 479 | if ($inserts) { |
|
| 259 | try { |
||
| 260 | 479 | $this->collection->insertMany($inserts, $options); |
|
| 261 | 6 | } catch (DriverException $e) { |
|
| 262 | 6 | $this->queuedInserts = []; |
|
| 263 | 6 | throw $e; |
|
| 264 | } |
||
| 265 | } |
||
| 266 | |||
| 267 | /* All collections except for ones using addToSet have already been |
||
| 268 | * saved. We have left these to be handled separately to avoid checking |
||
| 269 | * collection for uniqueness on PHP side. |
||
| 270 | */ |
||
| 271 | 479 | foreach ($this->queuedInserts as $document) { |
|
| 272 | 479 | $this->handleCollections($document, $options); |
|
| 273 | } |
||
| 274 | |||
| 275 | 479 | $this->queuedInserts = []; |
|
| 276 | 479 | } |
|
| 277 | |||
| 278 | /** |
||
| 279 | * Executes all queued document upserts. |
||
| 280 | * |
||
| 281 | * Queued documents with an ID are upserted individually. |
||
| 282 | * |
||
| 283 | * If no upserts are queued, invoking this method is a NOOP. |
||
| 284 | * |
||
| 285 | * @param array $options Options for batchInsert() and update() driver methods |
||
| 286 | */ |
||
| 287 | 83 | public function executeUpserts(array $options = []) |
|
| 288 | { |
||
| 289 | 83 | if (! $this->queuedUpserts) { |
|
| 290 | return; |
||
| 291 | } |
||
| 292 | |||
| 293 | 83 | $options = $this->getWriteOptions($options); |
|
| 294 | 83 | foreach ($this->queuedUpserts as $oid => $document) { |
|
| 295 | try { |
||
| 296 | 83 | $this->executeUpsert($document, $options); |
|
| 297 | 83 | $this->handleCollections($document, $options); |
|
| 298 | 83 | unset($this->queuedUpserts[$oid]); |
|
| 299 | } catch (WriteException $e) { |
||
| 300 | unset($this->queuedUpserts[$oid]); |
||
| 301 | 83 | throw $e; |
|
| 302 | } |
||
| 303 | } |
||
| 304 | 83 | } |
|
| 305 | |||
| 306 | /** |
||
| 307 | * Executes a single upsert in {@link executeUpserts} |
||
| 308 | * |
||
| 309 | * @param object $document |
||
| 310 | * @param array $options |
||
| 311 | */ |
||
| 312 | 83 | private function executeUpsert($document, array $options) |
|
| 313 | { |
||
| 314 | 83 | $options['upsert'] = true; |
|
| 315 | 83 | $criteria = $this->getQueryForDocument($document); |
|
| 316 | |||
| 317 | 83 | $data = $this->pb->prepareUpsertData($document); |
|
| 318 | |||
| 319 | // Set the initial version for each upsert |
||
| 320 | 83 | if ($this->class->isVersioned) { |
|
| 321 | 2 | $versionMapping = $this->class->fieldMappings[$this->class->versionField]; |
|
| 322 | 2 | $nextVersion = null; |
|
| 323 | 2 | if ($versionMapping['type'] === 'int') { |
|
| 324 | 1 | $nextVersion = max(1, (int) $this->class->reflFields[$this->class->versionField]->getValue($document)); |
|
| 325 | 1 | $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersion); |
|
| 326 | 1 | } elseif ($versionMapping['type'] === 'date') { |
|
| 327 | 1 | $nextVersionDateTime = new \DateTime(); |
|
| 328 | 1 | $nextVersion = Type::convertPHPToDatabaseValue($nextVersionDateTime); |
|
| 329 | 1 | $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersionDateTime); |
|
| 330 | } |
||
| 331 | 2 | $data['$set'][$versionMapping['name']] = $nextVersion; |
|
| 332 | } |
||
| 333 | |||
| 334 | 83 | foreach (array_keys($criteria) as $field) { |
|
| 335 | 83 | unset($data['$set'][$field]); |
|
| 336 | 83 | unset($data['$inc'][$field]); |
|
| 337 | 83 | unset($data['$setOnInsert'][$field]); |
|
| 338 | } |
||
| 339 | |||
| 340 | // Do not send empty update operators |
||
| 341 | 83 | foreach (['$set', '$inc', '$setOnInsert'] as $operator) { |
|
| 342 | 83 | if (! empty($data[$operator])) { |
|
| 343 | 68 | continue; |
|
| 344 | } |
||
| 345 | |||
| 346 | 83 | unset($data[$operator]); |
|
| 347 | } |
||
| 348 | |||
| 349 | /* If there are no modifiers remaining, we're upserting a document with |
||
| 350 | * an identifier as its only field. Since a document with the identifier |
||
| 351 | * may already exist, the desired behavior is "insert if not exists" and |
||
| 352 | * NOOP otherwise. MongoDB 2.6+ does not allow empty modifiers, so $set |
||
| 353 | * the identifier to the same value in our criteria. |
||
| 354 | * |
||
| 355 | * This will fail for versions before MongoDB 2.6, which require an |
||
| 356 | * empty $set modifier. The best we can do (without attempting to check |
||
| 357 | * server versions in advance) is attempt the 2.6+ behavior and retry |
||
| 358 | * after the relevant exception. |
||
| 359 | * |
||
| 360 | * See: https://jira.mongodb.org/browse/SERVER-12266 |
||
| 361 | */ |
||
| 362 | 83 | if (empty($data)) { |
|
| 363 | 16 | $retry = true; |
|
| 364 | 16 | $data = ['$set' => ['_id' => $criteria['_id']]]; |
|
| 365 | } |
||
| 366 | |||
| 367 | try { |
||
| 368 | 83 | $this->collection->updateOne($criteria, $data, $options); |
|
| 369 | 83 | return; |
|
| 370 | } catch (WriteException $e) { |
||
| 371 | if (empty($retry) || strpos($e->getMessage(), 'Mod on _id not allowed') === false) { |
||
| 372 | throw $e; |
||
| 373 | } |
||
| 374 | } |
||
| 375 | |||
| 376 | $this->collection->updateOne($criteria, ['$set' => new \stdClass()], $options); |
||
| 377 | } |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Updates the already persisted document if it has any new changesets. |
||
| 381 | * |
||
| 382 | * @param object $document |
||
| 383 | * @param array $options Array of options to be used with update() |
||
| 384 | * @throws LockException |
||
| 385 | */ |
||
| 386 | 196 | public function update($document, array $options = []) |
|
| 387 | { |
||
| 388 | 196 | $update = $this->pb->prepareUpdateData($document); |
|
| 389 | |||
| 390 | 196 | $query = $this->getQueryForDocument($document); |
|
| 391 | |||
| 392 | 196 | foreach (array_keys($query) as $field) { |
|
| 393 | 196 | unset($update['$set'][$field]); |
|
| 394 | } |
||
| 395 | |||
| 396 | 196 | if (empty($update['$set'])) { |
|
| 397 | 90 | unset($update['$set']); |
|
| 398 | } |
||
| 399 | |||
| 400 | // Include versioning logic to set the new version value in the database |
||
| 401 | // and to ensure the version has not changed since this document object instance |
||
| 402 | // was fetched from the database |
||
| 403 | 196 | $nextVersion = null; |
|
| 404 | 196 | if ($this->class->isVersioned) { |
|
| 405 | 13 | $versionMapping = $this->class->fieldMappings[$this->class->versionField]; |
|
| 406 | 13 | $currentVersion = $this->class->reflFields[$this->class->versionField]->getValue($document); |
|
| 407 | 13 | if ($versionMapping['type'] === 'int') { |
|
| 408 | 10 | $nextVersion = $currentVersion + 1; |
|
| 409 | 10 | $update['$inc'][$versionMapping['name']] = 1; |
|
| 410 | 10 | $query[$versionMapping['name']] = $currentVersion; |
|
| 411 | 3 | } elseif ($versionMapping['type'] === 'date') { |
|
| 412 | 3 | $nextVersion = new \DateTime(); |
|
| 413 | 3 | $update['$set'][$versionMapping['name']] = Type::convertPHPToDatabaseValue($nextVersion); |
|
| 414 | 3 | $query[$versionMapping['name']] = Type::convertPHPToDatabaseValue($currentVersion); |
|
| 415 | } |
||
| 416 | } |
||
| 417 | |||
| 418 | 196 | if (! empty($update)) { |
|
| 419 | // Include locking logic so that if the document object in memory is currently |
||
| 420 | // locked then it will remove it, otherwise it ensures the document is not locked. |
||
| 421 | 129 | if ($this->class->isLockable) { |
|
| 422 | 11 | $isLocked = $this->class->reflFields[$this->class->lockField]->getValue($document); |
|
| 423 | 11 | $lockMapping = $this->class->fieldMappings[$this->class->lockField]; |
|
| 424 | 11 | if ($isLocked) { |
|
| 425 | 2 | $update['$unset'] = [$lockMapping['name'] => true]; |
|
| 426 | } else { |
||
| 427 | 9 | $query[$lockMapping['name']] = ['$exists' => false]; |
|
| 428 | } |
||
| 429 | } |
||
| 430 | |||
| 431 | 129 | $options = $this->getWriteOptions($options); |
|
| 432 | |||
| 433 | 129 | $result = $this->collection->updateOne($query, $update, $options); |
|
| 434 | |||
| 435 | 129 | if (($this->class->isVersioned || $this->class->isLockable) && $result->getModifiedCount() !== 1) { |
|
| 436 | 4 | throw LockException::lockFailed($document); |
|
| 437 | 125 | } elseif ($this->class->isVersioned) { |
|
| 438 | 9 | $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersion); |
|
| 439 | } |
||
| 440 | } |
||
| 441 | |||
| 442 | 192 | $this->handleCollections($document, $options); |
|
| 443 | 192 | } |
|
| 444 | |||
| 445 | /** |
||
| 446 | * Removes document from mongo |
||
| 447 | * |
||
| 448 | * @param mixed $document |
||
| 449 | * @param array $options Array of options to be used with remove() |
||
| 450 | * @throws LockException |
||
| 451 | */ |
||
| 452 | 34 | public function delete($document, array $options = []) |
|
| 453 | { |
||
| 454 | 34 | $query = $this->getQueryForDocument($document); |
|
| 455 | |||
| 456 | 34 | if ($this->class->isLockable) { |
|
| 457 | 2 | $query[$this->class->lockField] = ['$exists' => false]; |
|
| 458 | } |
||
| 459 | |||
| 460 | 34 | $options = $this->getWriteOptions($options); |
|
| 461 | |||
| 462 | 34 | $result = $this->collection->deleteOne($query, $options); |
|
| 463 | |||
| 464 | 34 | if (($this->class->isVersioned || $this->class->isLockable) && ! $result->getDeletedCount()) { |
|
| 465 | 2 | throw LockException::lockFailed($document); |
|
| 466 | } |
||
| 467 | 32 | } |
|
| 468 | |||
| 469 | /** |
||
| 470 | * Refreshes a managed document. |
||
| 471 | * |
||
| 472 | * @param object $document The document to refresh. |
||
| 473 | */ |
||
| 474 | 20 | public function refresh($document) |
|
| 475 | { |
||
| 476 | 20 | $query = $this->getQueryForDocument($document); |
|
| 477 | 20 | $data = $this->collection->findOne($query); |
|
| 478 | 20 | if ($data === null) { |
|
| 479 | throw MongoDBException::cannotRefreshDocument(); |
||
| 480 | } |
||
| 481 | 20 | $data = $this->hydratorFactory->hydrate($document, (array) $data); |
|
| 482 | 20 | $this->uow->setOriginalDocumentData($document, $data); |
|
| 483 | 20 | } |
|
| 484 | |||
| 485 | /** |
||
| 486 | * Finds a document by a set of criteria. |
||
| 487 | * |
||
| 488 | * If a scalar or MongoDB\BSON\ObjectId is provided for $criteria, it will |
||
| 489 | * be used to match an _id value. |
||
| 490 | * |
||
| 491 | * @param mixed $criteria Query criteria |
||
| 492 | * @param object $document Document to load the data into. If not specified, a new document is created. |
||
| 493 | * @param array $hints Hints for document creation |
||
| 494 | * @param int $lockMode |
||
| 495 | * @param array $sort Sort array for Cursor::sort() |
||
| 496 | * @throws LockException |
||
| 497 | * @return object|null The loaded and managed document instance or null if no document was found |
||
| 498 | * @todo Check identity map? loadById method? Try to guess whether $criteria is the id? |
||
| 499 | */ |
||
| 500 | 344 | public function load($criteria, $document = null, array $hints = [], $lockMode = 0, ?array $sort = null) |
|
| 501 | { |
||
| 502 | // TODO: remove this |
||
| 503 | 344 | if ($criteria === null || is_scalar($criteria) || $criteria instanceof ObjectId) { |
|
| 504 | $criteria = ['_id' => $criteria]; |
||
| 505 | } |
||
| 506 | |||
| 507 | 344 | $criteria = $this->prepareQueryOrNewObj($criteria); |
|
| 508 | 344 | $criteria = $this->addDiscriminatorToPreparedQuery($criteria); |
|
| 509 | 344 | $criteria = $this->addFilterToPreparedQuery($criteria); |
|
| 510 | |||
| 511 | 344 | $options = []; |
|
| 512 | 344 | if ($sort !== null) { |
|
| 513 | 92 | $options['sort'] = $this->prepareSort($sort); |
|
| 514 | } |
||
| 515 | 344 | $result = $this->collection->findOne($criteria, $options); |
|
| 516 | 344 | $result = $result !== null ? (array) $result : null; |
|
| 517 | |||
| 518 | 344 | if ($this->class->isLockable) { |
|
| 519 | 1 | $lockMapping = $this->class->fieldMappings[$this->class->lockField]; |
|
| 520 | 1 | if (isset($result[$lockMapping['name']]) && $result[$lockMapping['name']] === LockMode::PESSIMISTIC_WRITE) { |
|
| 521 | 1 | throw LockException::lockFailed($result); |
|
| 522 | } |
||
| 523 | } |
||
| 524 | |||
| 525 | 343 | return $this->createDocument($result, $document, $hints); |
|
| 526 | } |
||
| 527 | |||
| 528 | /** |
||
| 529 | * Finds documents by a set of criteria. |
||
| 530 | * |
||
| 531 | * @param array $criteria Query criteria |
||
| 532 | * @param array $sort Sort array for Cursor::sort() |
||
| 533 | * @param int|null $limit Limit for Cursor::limit() |
||
| 534 | * @param int|null $skip Skip for Cursor::skip() |
||
| 535 | * @return Iterator |
||
| 536 | */ |
||
| 537 | 22 | public function loadAll(array $criteria = [], ?array $sort = null, $limit = null, $skip = null) |
|
| 538 | { |
||
| 539 | 22 | $criteria = $this->prepareQueryOrNewObj($criteria); |
|
| 540 | 22 | $criteria = $this->addDiscriminatorToPreparedQuery($criteria); |
|
| 541 | 22 | $criteria = $this->addFilterToPreparedQuery($criteria); |
|
| 542 | |||
| 543 | 22 | $options = []; |
|
| 544 | 22 | if ($sort !== null) { |
|
| 545 | 11 | $options['sort'] = $this->prepareSort($sort); |
|
| 546 | } |
||
| 547 | |||
| 548 | 22 | if ($limit !== null) { |
|
| 549 | 10 | $options['limit'] = $limit; |
|
| 550 | } |
||
| 551 | |||
| 552 | 22 | if ($skip !== null) { |
|
| 553 | 1 | $options['skip'] = $skip; |
|
| 554 | } |
||
| 555 | |||
| 556 | 22 | $baseCursor = $this->collection->find($criteria, $options); |
|
| 557 | 22 | $cursor = $this->wrapCursor($baseCursor); |
|
| 558 | |||
| 559 | 22 | return $cursor; |
|
| 560 | } |
||
| 561 | |||
| 562 | /** |
||
| 563 | * @param object $document |
||
| 564 | * |
||
| 565 | * @return array |
||
| 566 | * @throws MongoDBException |
||
| 567 | */ |
||
| 568 | 270 | private function getShardKeyQuery($document) |
|
| 569 | { |
||
| 570 | 270 | if (! $this->class->isSharded()) { |
|
| 571 | 266 | return []; |
|
| 572 | } |
||
| 573 | |||
| 574 | 4 | $shardKey = $this->class->getShardKey(); |
|
| 575 | 4 | $keys = array_keys($shardKey['keys']); |
|
| 576 | 4 | $data = $this->uow->getDocumentActualData($document); |
|
| 577 | |||
| 578 | 4 | $shardKeyQueryPart = []; |
|
| 579 | 4 | foreach ($keys as $key) { |
|
| 580 | 4 | $mapping = $this->class->getFieldMappingByDbFieldName($key); |
|
| 581 | 4 | $this->guardMissingShardKey($document, $key, $data); |
|
| 582 | |||
| 583 | 4 | if (isset($mapping['association']) && $mapping['association'] === ClassMetadata::REFERENCE_ONE) { |
|
| 584 | 1 | $reference = $this->prepareReference( |
|
| 585 | 1 | $key, |
|
| 586 | 1 | $data[$mapping['fieldName']], |
|
| 587 | 1 | $mapping, |
|
| 588 | 1 | false |
|
| 589 | ); |
||
| 590 | 1 | foreach ($reference as $keyValue) { |
|
| 591 | 1 | $shardKeyQueryPart[$keyValue[0]] = $keyValue[1]; |
|
| 592 | } |
||
| 593 | } else { |
||
| 594 | 3 | $value = Type::getType($mapping['type'])->convertToDatabaseValue($data[$mapping['fieldName']]); |
|
| 595 | 4 | $shardKeyQueryPart[$key] = $value; |
|
| 596 | } |
||
| 597 | } |
||
| 598 | |||
| 599 | 4 | return $shardKeyQueryPart; |
|
| 600 | } |
||
| 601 | |||
| 602 | /** |
||
| 603 | * Wraps the supplied base cursor in the corresponding ODM class. |
||
| 604 | * |
||
| 605 | */ |
||
| 606 | 22 | private function wrapCursor(Cursor $baseCursor): Iterator |
|
| 607 | { |
||
| 608 | 22 | return new CachingIterator(new HydratingIterator($baseCursor, $this->dm->getUnitOfWork(), $this->class)); |
|
| 609 | } |
||
| 610 | |||
| 611 | /** |
||
| 612 | * Checks whether the given managed document exists in the database. |
||
| 613 | * |
||
| 614 | * @param object $document |
||
| 615 | * @return bool TRUE if the document exists in the database, FALSE otherwise. |
||
| 616 | */ |
||
| 617 | 3 | public function exists($document) |
|
| 618 | { |
||
| 619 | 3 | $id = $this->class->getIdentifierObject($document); |
|
| 620 | 3 | return (bool) $this->collection->findOne(['_id' => $id], ['_id']); |
|
| 621 | } |
||
| 622 | |||
| 623 | /** |
||
| 624 | * Locks document by storing the lock mode on the mapped lock field. |
||
| 625 | * |
||
| 626 | * @param object $document |
||
| 627 | * @param int $lockMode |
||
| 628 | */ |
||
| 629 | 5 | public function lock($document, $lockMode) |
|
| 630 | { |
||
| 631 | 5 | $id = $this->uow->getDocumentIdentifier($document); |
|
| 632 | 5 | $criteria = ['_id' => $this->class->getDatabaseIdentifierValue($id)]; |
|
| 633 | 5 | $lockMapping = $this->class->fieldMappings[$this->class->lockField]; |
|
| 634 | 5 | $this->collection->updateOne($criteria, ['$set' => [$lockMapping['name'] => $lockMode]]); |
|
| 635 | 5 | $this->class->reflFields[$this->class->lockField]->setValue($document, $lockMode); |
|
| 636 | 5 | } |
|
| 637 | |||
| 638 | /** |
||
| 639 | * Releases any lock that exists on this document. |
||
| 640 | * |
||
| 641 | * @param object $document |
||
| 642 | */ |
||
| 643 | 1 | public function unlock($document) |
|
| 644 | { |
||
| 645 | 1 | $id = $this->uow->getDocumentIdentifier($document); |
|
| 646 | 1 | $criteria = ['_id' => $this->class->getDatabaseIdentifierValue($id)]; |
|
| 647 | 1 | $lockMapping = $this->class->fieldMappings[$this->class->lockField]; |
|
| 648 | 1 | $this->collection->updateOne($criteria, ['$unset' => [$lockMapping['name'] => true]]); |
|
| 649 | 1 | $this->class->reflFields[$this->class->lockField]->setValue($document, null); |
|
| 650 | 1 | } |
|
| 651 | |||
| 652 | /** |
||
| 653 | * Creates or fills a single document object from an query result. |
||
| 654 | * |
||
| 655 | * @param array|null $result The query result. |
||
| 656 | * @param object $document The document object to fill, if any. |
||
| 657 | * @param array $hints Hints for document creation. |
||
| 658 | * @return object|null The filled and managed document object or NULL, if the query result is empty. |
||
| 659 | */ |
||
| 660 | 343 | private function createDocument($result, $document = null, array $hints = []) |
|
| 661 | { |
||
| 662 | 343 | if ($result === null) { |
|
| 663 | 113 | return null; |
|
| 664 | } |
||
| 665 | |||
| 666 | 299 | if ($document !== null) { |
|
| 667 | 24 | $hints[Query::HINT_REFRESH] = true; |
|
| 668 | 24 | $id = $this->class->getPHPIdentifierValue($result['_id']); |
|
| 669 | 24 | $this->uow->registerManaged($document, $id, $result); |
|
| 670 | } |
||
| 671 | |||
| 672 | 299 | return $this->uow->getOrCreateDocument($this->class->name, $result, $hints, $document); |
|
| 673 | } |
||
| 674 | |||
| 675 | /** |
||
| 676 | * Loads a PersistentCollection data. Used in the initialize() method. |
||
| 677 | * |
||
| 678 | */ |
||
| 679 | 164 | public function loadCollection(PersistentCollectionInterface $collection) |
|
| 680 | { |
||
| 681 | 164 | $mapping = $collection->getMapping(); |
|
| 682 | 164 | switch ($mapping['association']) { |
|
| 683 | case ClassMetadata::EMBED_MANY: |
||
| 684 | 109 | $this->loadEmbedManyCollection($collection); |
|
| 685 | 109 | break; |
|
| 686 | |||
| 687 | case ClassMetadata::REFERENCE_MANY: |
||
| 688 | 77 | if (isset($mapping['repositoryMethod']) && $mapping['repositoryMethod']) { |
|
| 689 | 5 | $this->loadReferenceManyWithRepositoryMethod($collection); |
|
| 690 | } else { |
||
| 691 | 73 | if ($mapping['isOwningSide']) { |
|
| 692 | 61 | $this->loadReferenceManyCollectionOwningSide($collection); |
|
| 693 | } else { |
||
| 694 | 17 | $this->loadReferenceManyCollectionInverseSide($collection); |
|
| 695 | } |
||
| 696 | } |
||
| 697 | 77 | break; |
|
| 698 | } |
||
| 699 | 164 | } |
|
| 700 | |||
| 701 | 109 | private function loadEmbedManyCollection(PersistentCollectionInterface $collection) |
|
| 702 | { |
||
| 703 | 109 | $embeddedDocuments = $collection->getMongoData(); |
|
| 704 | 109 | $mapping = $collection->getMapping(); |
|
| 705 | 109 | $owner = $collection->getOwner(); |
|
| 706 | 109 | if (! $embeddedDocuments) { |
|
| 707 | 58 | return; |
|
| 708 | } |
||
| 709 | |||
| 710 | 82 | foreach ($embeddedDocuments as $key => $embeddedDocument) { |
|
| 711 | 82 | $className = $this->uow->getClassNameForAssociation($mapping, $embeddedDocument); |
|
| 712 | 82 | $embeddedMetadata = $this->dm->getClassMetadata($className); |
|
| 713 | 82 | $embeddedDocumentObject = $embeddedMetadata->newInstance(); |
|
| 714 | |||
| 715 | 82 | $this->uow->setParentAssociation($embeddedDocumentObject, $mapping, $owner, $mapping['name'] . '.' . $key); |
|
| 716 | |||
| 717 | 82 | $data = $this->hydratorFactory->hydrate($embeddedDocumentObject, $embeddedDocument, $collection->getHints()); |
|
| 718 | 82 | $id = $data[$embeddedMetadata->identifier] ?? null; |
|
| 719 | |||
| 720 | 82 | if (empty($collection->getHints()[Query::HINT_READ_ONLY])) { |
|
| 721 | 81 | $this->uow->registerManaged($embeddedDocumentObject, $id, $data); |
|
| 722 | } |
||
| 723 | 82 | if (CollectionHelper::isHash($mapping['strategy'])) { |
|
| 724 | 10 | $collection->set($key, $embeddedDocumentObject); |
|
| 725 | } else { |
||
| 726 | 82 | $collection->add($embeddedDocumentObject); |
|
| 727 | } |
||
| 728 | } |
||
| 729 | 82 | } |
|
| 730 | |||
| 731 | 61 | private function loadReferenceManyCollectionOwningSide(PersistentCollectionInterface $collection) |
|
| 732 | { |
||
| 733 | 61 | $hints = $collection->getHints(); |
|
| 734 | 61 | $mapping = $collection->getMapping(); |
|
| 735 | 61 | $groupedIds = []; |
|
| 736 | |||
| 737 | 61 | $sorted = isset($mapping['sort']) && $mapping['sort']; |
|
| 738 | |||
| 739 | 61 | foreach ($collection->getMongoData() as $key => $reference) { |
|
| 740 | 55 | $className = $this->uow->getClassNameForAssociation($mapping, $reference); |
|
| 741 | 55 | $identifier = ClassMetadata::getReferenceId($reference, $mapping['storeAs']); |
|
| 742 | 55 | $id = $this->dm->getClassMetadata($className)->getPHPIdentifierValue($identifier); |
|
| 743 | |||
| 744 | // create a reference to the class and id |
||
| 745 | 55 | $reference = $this->dm->getReference($className, $id); |
|
| 746 | |||
| 747 | // no custom sort so add the references right now in the order they are embedded |
||
| 748 | 55 | if (! $sorted) { |
|
| 749 | 54 | if (CollectionHelper::isHash($mapping['strategy'])) { |
|
| 750 | 2 | $collection->set($key, $reference); |
|
| 751 | } else { |
||
| 752 | 52 | $collection->add($reference); |
|
| 753 | } |
||
| 754 | } |
||
| 755 | |||
| 756 | // only query for the referenced object if it is not already initialized or the collection is sorted |
||
| 757 | 55 | if (! (($reference instanceof Proxy && ! $reference->__isInitialized__)) && ! $sorted) { |
|
| 758 | 22 | continue; |
|
| 759 | } |
||
| 760 | |||
| 761 | 40 | $groupedIds[$className][] = $identifier; |
|
| 762 | } |
||
| 763 | 61 | foreach ($groupedIds as $className => $ids) { |
|
| 764 | 40 | $class = $this->dm->getClassMetadata($className); |
|
| 765 | 40 | $mongoCollection = $this->dm->getDocumentCollection($className); |
|
| 766 | 40 | $criteria = $this->cm->merge( |
|
| 767 | 40 | ['_id' => ['$in' => array_values($ids)]], |
|
| 768 | 40 | $this->dm->getFilterCollection()->getFilterCriteria($class), |
|
| 769 | 40 | $mapping['criteria'] ?? [] |
|
| 770 | ); |
||
| 771 | 40 | $criteria = $this->uow->getDocumentPersister($className)->prepareQueryOrNewObj($criteria); |
|
| 772 | |||
| 773 | 40 | $options = []; |
|
| 774 | 40 | if (isset($mapping['sort'])) { |
|
| 775 | 40 | $options['sort'] = $this->prepareSort($mapping['sort']); |
|
| 776 | } |
||
| 777 | 40 | if (isset($mapping['limit'])) { |
|
| 778 | $options['limit'] = $mapping['limit']; |
||
| 779 | } |
||
| 780 | 40 | if (isset($mapping['skip'])) { |
|
| 781 | $options['skip'] = $mapping['skip']; |
||
| 782 | } |
||
| 783 | 40 | if (! empty($hints[Query::HINT_READ_PREFERENCE])) { |
|
| 784 | $options['readPreference'] = $hints[Query::HINT_READ_PREFERENCE]; |
||
| 785 | } |
||
| 786 | |||
| 787 | 40 | $cursor = $mongoCollection->find($criteria, $options); |
|
| 788 | 40 | $documents = $cursor->toArray(); |
|
| 789 | 40 | foreach ($documents as $documentData) { |
|
| 790 | 39 | $document = $this->uow->getById($documentData['_id'], $class); |
|
| 791 | 39 | if ($document instanceof Proxy && ! $document->__isInitialized()) { |
|
| 792 | 39 | $data = $this->hydratorFactory->hydrate($document, $documentData); |
|
| 793 | 39 | $this->uow->setOriginalDocumentData($document, $data); |
|
| 794 | 39 | $document->__isInitialized__ = true; |
|
| 795 | } |
||
| 796 | 39 | if (! $sorted) { |
|
| 797 | 38 | continue; |
|
| 798 | } |
||
| 799 | |||
| 800 | 40 | $collection->add($document); |
|
| 801 | } |
||
| 802 | } |
||
| 803 | 61 | } |
|
| 804 | |||
| 805 | 17 | private function loadReferenceManyCollectionInverseSide(PersistentCollectionInterface $collection) |
|
| 806 | { |
||
| 807 | 17 | $query = $this->createReferenceManyInverseSideQuery($collection); |
|
| 808 | 17 | $documents = $query->execute()->toArray(); |
|
| 809 | 17 | foreach ($documents as $key => $document) { |
|
| 810 | 16 | $collection->add($document); |
|
| 811 | } |
||
| 812 | 17 | } |
|
| 813 | |||
| 814 | /** |
||
| 815 | * |
||
| 816 | * @return Query |
||
| 817 | */ |
||
| 818 | 17 | public function createReferenceManyInverseSideQuery(PersistentCollectionInterface $collection) |
|
| 819 | { |
||
| 820 | 17 | $hints = $collection->getHints(); |
|
| 821 | 17 | $mapping = $collection->getMapping(); |
|
| 822 | 17 | $owner = $collection->getOwner(); |
|
| 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) |
|
| 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 | 5 | $collection->add($obj); |
|
| 868 | } |
||
| 869 | } |
||
| 870 | 5 | } |
|
| 871 | |||
| 872 | /** |
||
| 873 | * @return Iterator |
||
| 874 | */ |
||
| 875 | 5 | public function createReferenceManyWithRepositoryMethodCursor(PersistentCollectionInterface $collection) |
|
| 896 | |||
| 897 | /** |
||
| 898 | * Prepare a projection array by converting keys, which are PHP property |
||
| 899 | * names, to MongoDB field names. |
||
| 900 | * |
||
| 901 | * @param array $fields |
||
| 902 | * @return array |
||
| 903 | */ |
||
| 904 | 14 | public function prepareProjection(array $fields) |
|
| 914 | |||
| 915 | /** |
||
| 916 | * @param string|int $sort |
||
| 917 | * @return string|int |
||
| 918 | */ |
||
| 919 | 25 | private function getSortDirection($sort) |
|
| 931 | |||
| 932 | /** |
||
| 933 | * Prepare a sort specification array by converting keys to MongoDB field |
||
| 934 | * names and changing direction strings to int. |
||
| 935 | * |
||
| 936 | * @param array $fields |
||
| 937 | * @return array |
||
| 938 | */ |
||
| 939 | 140 | public function prepareSort(array $fields) |
|
| 949 | |||
| 950 | /** |
||
| 951 | * Prepare a mongodb field name and convert the PHP property names to MongoDB field names. |
||
| 952 | * |
||
| 953 | * @param string $fieldName |
||
| 954 | * @return string |
||
| 955 | */ |
||
| 956 | 433 | public function prepareFieldName($fieldName) |
|
| 962 | |||
| 963 | /** |
||
| 964 | * Adds discriminator criteria to an already-prepared query. |
||
| 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 | * @param array $preparedQuery |
||
| 971 | * @return array |
||
| 972 | */ |
||
| 973 | 497 | public function addDiscriminatorToPreparedQuery(array $preparedQuery) |
|
| 989 | |||
| 990 | /** |
||
| 991 | * Adds filter criteria to an already-prepared query. |
||
| 992 | * |
||
| 993 | * This method should be used once for query criteria and not be used for |
||
| 994 | * nested expressions. It should be called after |
||
| 995 | * {@link DocumentPerister::addDiscriminatorToPreparedQuery()}. |
||
| 996 | * |
||
| 997 | * @param array $preparedQuery |
||
| 998 | * @return array |
||
| 999 | */ |
||
| 1000 | 498 | public function addFilterToPreparedQuery(array $preparedQuery) |
|
| 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 | * @param array $query |
||
| 1022 | * @param bool $isNewObj |
||
| 1023 | * @return array |
||
| 1024 | */ |
||
| 1025 | 530 | public function prepareQueryOrNewObj(array $query, $isNewObj = false) |
|
| 1053 | |||
| 1054 | /** |
||
| 1055 | * Prepares a query value and converts the PHP value to the database value |
||
| 1056 | * if it is an identifier. |
||
| 1057 | * |
||
| 1058 | * It also handles converting $fieldName to the database name if they are different. |
||
| 1059 | * |
||
| 1060 | * @param string $fieldName |
||
| 1061 | * @param mixed $value |
||
| 1062 | * @param ClassMetadata $class Defaults to $this->class |
||
| 1063 | * @param bool $prepareValue Whether or not to prepare the value |
||
| 1064 | * @param bool $inNewObj Whether or not newObj is being prepared |
||
| 1065 | * @return array An array of tuples containing prepared field names and values |
||
| 1066 | */ |
||
| 1067 | 881 | private function prepareQueryElement($fieldName, $value = null, $class = null, $prepareValue = true, $inNewObj = false) |
|
| 1068 | { |
||
| 1069 | 881 | $class = $class ?? $this->class; |
|
| 1070 | |||
| 1071 | // @todo Consider inlining calls to ClassMetadata methods |
||
| 1072 | |||
| 1073 | // Process all non-identifier fields by translating field names |
||
| 1074 | 881 | if ($class->hasField($fieldName) && ! $class->isIdentifier($fieldName)) { |
|
| 1075 | 247 | $mapping = $class->fieldMappings[$fieldName]; |
|
| 1076 | 247 | $fieldName = $mapping['name']; |
|
| 1077 | |||
| 1078 | 247 | if (! $prepareValue) { |
|
| 1079 | 52 | return [[$fieldName, $value]]; |
|
| 1080 | } |
||
| 1081 | |||
| 1262 | |||
| 1263 | /** |
||
| 1264 | * Prepares a query expression. |
||
| 1265 | * |
||
| 1266 | * @param array|object $expression |
||
| 1267 | * @param ClassMetadata $class |
||
| 1268 | * @return array|object |
||
| 1269 | */ |
||
| 1270 | 79 | private function prepareQueryExpression($expression, $class) |
|
| 1297 | |||
| 1298 | /** |
||
| 1299 | * Checks whether the value has DBRef fields. |
||
| 1300 | * |
||
| 1301 | * This method doesn't check if the the value is a complete DBRef object, |
||
| 1302 | * although it should return true for a DBRef. Rather, we're checking that |
||
| 1303 | * the value has one or more fields for a DBref. In practice, this could be |
||
| 1304 | * $elemMatch criteria for matching a DBRef. |
||
| 1305 | * |
||
| 1306 | * @param mixed $value |
||
| 1307 | * @return bool |
||
| 1308 | */ |
||
| 1309 | 80 | private function hasDBRefFields($value) |
|
| 1327 | |||
| 1328 | /** |
||
| 1329 | * Checks whether the value has query operators. |
||
| 1330 | * |
||
| 1331 | * @param mixed $value |
||
| 1332 | * @return bool |
||
| 1333 | */ |
||
| 1334 | 84 | private function hasQueryOperators($value) |
|
| 1352 | |||
| 1353 | /** |
||
| 1354 | * Gets the array of discriminator values for the given ClassMetadata |
||
| 1355 | * |
||
| 1356 | * @return array |
||
| 1357 | */ |
||
| 1358 | 27 | private function getClassDiscriminatorValues(ClassMetadata $metadata) |
|
| 1377 | |||
| 1378 | 550 | private function handleCollections($document, $options) |
|
| 1401 | |||
| 1402 | /** |
||
| 1403 | * If the document is new, ignore shard key field value, otherwise throw an exception. |
||
| 1404 | * Also, shard key field should be present in actual document data. |
||
| 1405 | * |
||
| 1406 | * @param object $document |
||
| 1407 | * @param string $shardKeyField |
||
| 1408 | * @param array $actualDocumentData |
||
| 1409 | * |
||
| 1410 | * @throws MongoDBException |
||
| 1411 | */ |
||
| 1412 | 4 | private function guardMissingShardKey($document, $shardKeyField, $actualDocumentData) |
|
| 1428 | |||
| 1429 | /** |
||
| 1430 | * Get shard key aware query for single document. |
||
| 1431 | * |
||
| 1432 | * @param object $document |
||
| 1433 | * |
||
| 1434 | * @return array |
||
| 1435 | */ |
||
| 1436 | 266 | private function getQueryForDocument($document) |
|
| 1446 | |||
| 1447 | /** |
||
| 1448 | * @param array $options |
||
| 1449 | * |
||
| 1450 | * @return array |
||
| 1451 | */ |
||
| 1452 | 551 | private function getWriteOptions(array $options = []) |
|
| 1462 | |||
| 1463 | /** |
||
| 1464 | * @param string $fieldName |
||
| 1465 | * @param mixed $value |
||
| 1466 | * @param array $mapping |
||
| 1467 | * @param bool $inNewObj |
||
| 1468 | * @return array |
||
| 1469 | */ |
||
| 1470 | 15 | private function prepareReference($fieldName, $value, array $mapping, $inNewObj) |
|
| 1510 | } |
||
| 1511 |
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.