예제 #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;
 }