PageRenderTime 52ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Doctrine/ODM/MongoDB/Hydrator.php

https://github.com/weaverryan/mongodb-odm
PHP | 192 lines | 118 code | 15 blank | 59 comment | 19 complexity | af5f6bef4182fb68b2328823d364addb MD5 | raw file
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the LGPL. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\ODM\MongoDB;
  20. use Doctrine\ODM\MongoDB\Query,
  21. Doctrine\ODM\MongoDB\Mapping\ClassMetadata,
  22. Doctrine\ODM\MongoDB\Mapping\Types\Type,
  23. Doctrine\ODM\MongoDB\PersistentCollection,
  24. Doctrine\Common\Collections\ArrayCollection,
  25. Doctrine\Common\Collections\Collection,
  26. Doctrine\ODM\MongoDB\Event\LifecycleEventArgs,
  27. Doctrine\ODM\MongoDB\Event\PreLoadEventArgs;
  28. /**
  29. * The Hydrator class is responsible for converting a document from MongoDB
  30. * which is an array to classes and collections based on the mapping of the document
  31. *
  32. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  33. * @link www.doctrine-project.com
  34. * @since 1.0
  35. * @author Jonathan H. Wage <jonwage@gmail.com>
  36. */
  37. class Hydrator
  38. {
  39. /**
  40. * The DocumentManager associated with this Hydrator
  41. *
  42. * @var Doctrine\ODM\MongoDB\DocumentManager
  43. */
  44. private $dm;
  45. /**
  46. * The EventManager associated with this Hydrator
  47. *
  48. * @var Doctrine\Common\EventManager
  49. */
  50. private $evm;
  51. /**
  52. * Mongo command prefix
  53. * @var string
  54. */
  55. private $cmd;
  56. /**
  57. * Create a new Hydrator instance
  58. *
  59. * @param Doctrine\ODM\MongoDB\DocumentManager $dm
  60. */
  61. public function __construct(DocumentManager $dm)
  62. {
  63. $this->dm = $dm;
  64. $this->evm = $this->dm->getEventManager();
  65. $this->cmd = $dm->getConfiguration()->getMongoCmd();
  66. }
  67. /**
  68. * Hydrate array of MongoDB document data into the given document object.
  69. *
  70. * @param object $document The document object to hydrate the data into.
  71. * @param array $data The array of document data.
  72. * @return array $values The array of hydrated values.
  73. */
  74. public function hydrate($document, &$data)
  75. {
  76. $metadata = $this->dm->getClassMetadata(get_class($document));
  77. if (isset($metadata->lifecycleCallbacks[ODMEvents::preLoad])) {
  78. $args = array(&$data);
  79. $metadata->invokeLifecycleCallbacks(ODMEvents::preLoad, $document, $args);
  80. }
  81. if ($this->evm->hasListeners(ODMEvents::preLoad)) {
  82. $this->evm->dispatchEvent(ODMEvents::preLoad, new PreLoadEventArgs($document, $this->dm, $data));
  83. }
  84. if (isset($metadata->alsoLoadMethods)) {
  85. foreach ($metadata->alsoLoadMethods as $fieldName => $method) {
  86. if (isset($data[$fieldName])) {
  87. $document->$method($data[$fieldName]);
  88. }
  89. }
  90. }
  91. foreach ($metadata->fieldMappings as $mapping) {
  92. if (isset($mapping['alsoLoadFields'])) {
  93. $rawValue = null;
  94. $names = isset($mapping['alsoLoadFields']) ? $mapping['alsoLoadFields'] : array();
  95. array_unshift($names, $mapping['name']);
  96. foreach ($names as $name) {
  97. if (isset($data[$name])) {
  98. $rawValue = $data[$name];
  99. break;
  100. }
  101. }
  102. } else {
  103. $rawValue = isset($data[$mapping['name']]) ? $data[$mapping['name']] : null;
  104. }
  105. if ($rawValue === null) {
  106. continue;
  107. }
  108. $value = null;
  109. // Hydrate embedded
  110. if (isset($mapping['embedded'])) {
  111. if ($mapping['type'] === 'one') {
  112. $embeddedDocument = $rawValue;
  113. $className = $this->dm->getClassNameFromDiscriminatorValue($mapping, $embeddedDocument);
  114. $embeddedMetadata = $this->dm->getClassMetadata($className);
  115. $value = $embeddedMetadata->newInstance();
  116. $this->hydrate($value, $embeddedDocument);
  117. $data[$mapping['name']] = $embeddedDocument;
  118. $this->dm->getUnitOfWork()->registerManaged($value, null, $embeddedDocument);
  119. } elseif ($mapping['type'] === 'many') {
  120. $embeddedDocuments = $rawValue;
  121. $coll = new PersistentCollection(new ArrayCollection());
  122. foreach ($embeddedDocuments as $key => $embeddedDocument) {
  123. $className = $this->dm->getClassNameFromDiscriminatorValue($mapping, $embeddedDocument);
  124. $embeddedMetadata = $this->dm->getClassMetadata($className);
  125. $embeddedDocumentObject = $embeddedMetadata->newInstance();
  126. $this->hydrate($embeddedDocumentObject, $embeddedDocument);
  127. $data[$mapping['name']][$key] = $embeddedDocument;
  128. $this->dm->getUnitOfWork()->registerManaged($embeddedDocumentObject, null, $embeddedDocument);
  129. $coll->add($embeddedDocumentObject);
  130. }
  131. $coll->setOwner($document, $mapping);
  132. $coll->takeSnapshot();
  133. $value = $coll;
  134. }
  135. // Hydrate reference
  136. } elseif (isset($mapping['reference'])) {
  137. $reference = $rawValue;
  138. if ($mapping['type'] === 'one' && isset($reference[$this->cmd . 'id'])) {
  139. $className = $this->dm->getClassNameFromDiscriminatorValue($mapping, $reference);
  140. $targetMetadata = $this->dm->getClassMetadata($className);
  141. $id = $targetMetadata->getPHPIdentifierValue($reference[$this->cmd . 'id']);
  142. $value = $this->dm->getReference($className, $id);
  143. } elseif ($mapping['type'] === 'many' && (is_array($reference) || $reference instanceof Collection)) {
  144. $references = $reference;
  145. $value = new PersistentCollection(new ArrayCollection(), $this->dm);
  146. $value->setInitialized(false);
  147. $value->setOwner($document, $mapping);
  148. // Delay any hydration of reference objects until the collection is
  149. // accessed and initialized for the first ime
  150. $value->setReferences($references);
  151. }
  152. $data[$mapping['name']] = $value;
  153. // Hydrate regular field
  154. } else {
  155. $value = Type::getType($mapping['type'])->convertToPHPValue($rawValue);
  156. $data[$mapping['name']] = $value;
  157. }
  158. // Set hydrated field value to document
  159. if ($value !== null) {
  160. $metadata->setFieldValue($document, $mapping['fieldName'], $value);
  161. }
  162. }
  163. // Set the document identifier
  164. if (isset($data['_id'])) {
  165. $metadata->setIdentifierValue($document, $data['_id']);
  166. $data[$metadata->identifier] = $data['_id'];
  167. unset($data['_id']);
  168. }
  169. if (isset($metadata->lifecycleCallbacks[ODMEvents::postLoad])) {
  170. $metadata->invokeLifecycleCallbacks(ODMEvents::postLoad, $document);
  171. }
  172. if ($this->evm->hasListeners(ODMEvents::postLoad)) {
  173. $this->evm->dispatchEvent(ODMEvents::postLoad, new LifecycleEventArgs($document, $this->dm));
  174. }
  175. return $document;
  176. }
  177. }