Exemplo n.º 1
0
 /**
  * @inheritdoc
  */
 function hydrate($document, $data, array $hints = [])
 {
     $hydratedData = [];
     foreach ($this->metadata->fieldMappings as $fieldName => $mapping) {
         $name = $mapping['name'];
         $propertyValue = isset($data[$name]) ? $data[$name] : null;
         if (!isset($mapping['association'])) {
             if ($propertyValue === null) {
                 continue;
             }
             $type = Type::getType($mapping['type']);
             $value = $type->convertToPHPValue($propertyValue);
             $this->metadata->reflFields[$fieldName]->setValue($document, $value);
             $hydratedData[$fieldName] = $value;
             continue;
         }
         if ($mapping['association'] & ClassMetadata::TO_MANY) {
             $coll = new PersistentCollection(new ArrayCollection(), $this->dm, $this->uow);
             $coll->setOwner($document, $mapping);
             $coll->setInitialized(false);
             if ($propertyValue) {
                 $coll->setData($propertyValue);
             }
             $this->metadata->reflFields[$fieldName]->setValue($document, $coll);
             $hydratedData[$fieldName] = $coll;
             continue;
         }
         if ($propertyValue === null) {
             continue;
         }
         if ($mapping['association'] === ClassMetadata::LINK) {
             if (is_string($propertyValue)) {
                 $link = $this->dm->getReference($propertyValue);
             } else {
                 $link = $this->uow->getOrCreateDocument($propertyValue);
             }
             $this->metadata->reflFields[$fieldName]->setValue($document, $link);
             $hydratedData[$fieldName] = $link;
             continue;
         }
         if ($mapping['association'] === ClassMetadata::EMBED) {
             // an embed one must have @class, we would support generic JSON properties via another mapping type
             if (!isset($propertyValue[self::ORIENT_PROPERTY_CLASS])) {
                 throw new HydratorException(sprintf("missing @class for embedded property '%s'", $name));
             }
             $oclass = $propertyValue[self::ORIENT_PROPERTY_CLASS];
             $embeddedMetadata = $this->dm->getMetadataFactory()->getMetadataForOClass($oclass);
             $doc = $embeddedMetadata->newInstance();
             $embeddedData = $this->dm->getHydratorFactory()->hydrate($doc, $propertyValue, $hints);
             $this->uow->registerManaged($doc, null, $embeddedData);
             $this->metadata->reflFields[$fieldName]->setValue($document, $doc);
             $hydratedData[$fieldName] = $doc;
             continue;
         }
     }
     return $hydratedData;
 }
Exemplo n.º 2
0
 /**
  * Get a documents actual data, flattening all the objects to arrays.
  *
  * @param object $document
  *
  * @return array
  */
 public function getDocumentActualData($document)
 {
     $class = $this->dm->getClassMetadata(get_class($document));
     $actualData = [];
     foreach ($class->fieldMappings as $fieldName => $mapping) {
         if (isset($mapping['notSaved'])) {
             continue;
         }
         $rp = $class->reflFields[$fieldName];
         $value = $rp->getValue($document);
         if (isset($mapping['association']) && $mapping['association'] & ClassMetadata::TO_MANY && $value !== null && !$value instanceof PersistentCollection) {
             // If $actualData[$name] is not a Collection then use an ArrayCollection.
             if (!$value instanceof Collection) {
                 $value = new ArrayCollection($value);
             }
             // Inject PersistentCollection
             $coll = new PersistentCollection($value, $this->dm, $this);
             $coll->setOwner($document, $mapping);
             $coll->setDirty(!$value->isEmpty());
             $rp->setValue($document, $coll);
             $value = $coll;
         }
         $actualData[$fieldName] = $value;
     }
     return $actualData;
 }