/**
  * {@inheritdoc}
  */
 public function denormalize($data, $class, $format = null, array $context = [])
 {
     $result = $this->createObject($class);
     $fields = $this->fieldHelper->getFields($class, true);
     foreach ($fields as $field) {
         $fieldName = $field['name'];
         if (array_key_exists($fieldName, $data)) {
             $value = $data[$fieldName];
             if ($data[$fieldName] !== null && ($this->fieldHelper->isRelation($field) || $this->fieldHelper->isDateTimeField($field))) {
                 if ($this->fieldHelper->isMultipleRelation($field)) {
                     $entityClass = sprintf('ArrayCollection<%s>', $field['related_entity_name']);
                 } elseif ($this->fieldHelper->isSingleRelation($field)) {
                     $entityClass = $field['related_entity_name'];
                 } else {
                     $entityClass = 'DateTime';
                     $context = array_merge($context, ['type' => $field['type']]);
                 }
                 $context = array_merge($context, ['fieldName' => $fieldName]);
                 $value = $this->serializer->denormalize($value, $entityClass, $format, $context);
             }
             $this->fieldHelper->setObjectValue($result, $fieldName, $value);
         }
     }
     return $result;
 }
 /**
  * Returns collection of denormalized data
  *
  * @param mixed $data
  * @param string $class
  * @param mixed $format
  * @param array $context
  * @return ArrayCollection
  */
 public function denormalize($data, $class, $format = null, array $context = array())
 {
     if (!is_array($data)) {
         return new ArrayCollection();
     }
     $itemType = $this->getItemType($class);
     if (!$itemType) {
         return new ArrayCollection($data);
     }
     $result = new ArrayCollection();
     foreach ($data as $item) {
         $result->add($this->serializer->denormalize($item, $itemType, $format, $context));
     }
     return $result;
 }
Exemple #3
0
 /**
  * @param array  $data
  * @param string $name
  * @param string $type
  * @param mixed  $format
  * @param array  $context
  *
  * @return null|object
  */
 protected function denormalizeObject(array $data, $name, $type, $format = null, $context = array())
 {
     $result = null;
     if (!empty($data[$name])) {
         $result = $this->serializer->denormalize($data[$name], $type, $format, $context);
     }
     return $result;
 }
 /**
  * @param mixed $data
  * @param string $class
  * @param mixed $format
  * @param array $context
  * @return mixed
  */
 public function denormalize($data, $class, $format = null, array $context = array())
 {
     $fieldRules = $this->getProcessedFieldRules();
     if (!is_array($data)) {
         if ($this->primaryField) {
             $data = array($this->primaryField['normalizeName'] => $data);
         } else {
             return $this->createNewObject();
         }
     }
     $object = $this->createNewObject();
     foreach ($fieldRules as $field) {
         if (!$field['denormalize'] || !array_key_exists($field['normalizeName'], $data)) {
             continue;
         }
         $value = $data[$field['normalizeName']];
         if (isset($field['type']) && $value !== null) {
             $value = $this->serializer->denormalize($data[$field['normalizeName']], $field['type'], $format, array_merge($context, $field['context']));
         }
         $this->getPropertyAccessor()->setValue($object, $field['denormalizeName'], $value);
     }
     return $object;
 }