denormalize() public method

Denormalizes data into the given type.
public denormalize ( mixed $data, string $type, string $format = null ) : mixed
$data mixed
$type string
$format string
return mixed
 /**
  * {@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;
 }
 /**
  * {@inheritdoc}
  */
 public function denormalize($data, $class, $format = null, array $context = [])
 {
     if (empty($data)) {
         return null;
     }
     $options = new ArrayCollection();
     foreach ($data as $optionCode) {
         $option = $this->serializer->denormalize($optionCode, AttributeTypes::OPTION_SIMPLE_SELECT, $format, $context);
         $options->add($option);
     }
     return $options;
 }
 /**
  * {@inheritdoc}
  */
 public function denormalize($data, $class, $format = null, array $context = [])
 {
     if (empty($data)) {
         return null;
     }
     $options = new ArrayCollection();
     foreach ($data as $optionCode) {
         $option = $this->serializer->denormalize($optionCode, 'pim_catalog_simpleselect', $format, $context);
         $options->add($option);
     }
     return $options;
 }
 /**
  * @param mixed $data
  * @param string $class
  * @param mixed $format
  * @param array $context
  * @return AbstractTypedAddress
  */
 public function denormalize($data, $class, $format = null, array $context = array())
 {
     /** @var AbstractTypedAddress $result */
     $result = $this->addressNormalizer->denormalize($data, $class, $format, $context);
     if (!empty($data['types']) && is_array($data['types'])) {
         $types = $this->serializer->denormalize($data['types'], static::TYPES_TYPE, $format, $context);
         if ($types) {
             foreach ($types as $type) {
                 $result->addType($type);
             }
         }
     }
     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;
 }
Ejemplo n.º 6
0
 /**
  * @param array  $values
  * @param string $class
  * @param string $format
  * @param array  $context
  *
  * @return array|object
  */
 public function denormalize($values, $class, $format = null, array $context = array())
 {
     if (!is_array($values)) {
         return $values;
     }
     $filtered = array_filter($values, function ($value) use($class, $format) {
         return $this->serializer->supportsDenormalization($value, $class, $format);
     });
     if (count($filtered) !== count($values)) {
         throw new \InvalidArgumentException('Not all values within the array can be denormalized.');
     }
     return array_map(function ($value) use($class, $format, $context) {
         return $this->serializer->denormalize($value, $class, $format, $context);
     }, $filtered);
 }
Ejemplo n.º 7
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;
 }
Ejemplo n.º 8
0
 /**
  * {@inheritdoc}
  */
 public function handleUpdateRequest(Request $request, $identifier)
 {
     $parameters = $request->request->all();
     $resource = $this->getResourceById($identifier);
     $className = ClassUtils::getRealClass(get_class($resource));
     $resource = $this->serializer->denormalize($parameters, $className, self::RESPONSE_FORMAT, ['resource' => $resource]);
     $data = $this->serializer->serialize($resource, self::RESPONSE_FORMAT, ['group' => $this->getResourceType()]);
     $this->manager->updateResource($resource);
     return new Response($data);
 }
 /**
  * {@inheritdoc}
  */
 public function denormalize($data, $class, $format = null, array $context = [])
 {
     if (!isset($context['attribute'])) {
         throw new InvalidArgumentException('Attribute must be passed in the context');
     }
     $attribute = $context['attribute'];
     if (!$attribute instanceof AttributeInterface) {
         throw new InvalidArgumentException(sprintf('Attribute must be an instance of %s, %s given', 'Pim\\Bundle\\CatalogBundle\\Model\\AttributeInterface', is_object($attribute) ? get_class($attribute) : gettype($attribute)));
     }
     $data = $data + ['locale' => null, 'scope' => null, 'value' => null];
     $value = new $this->entityClass();
     $value->setAttribute($attribute);
     $value->setLocale($data['locale']);
     $value->setScope($data['scope']);
     $valueData = $this->serializer->denormalize($data['value'], $attribute->getAttributeType(), $format, $context);
     if (null !== $valueData) {
         $value->setData($valueData);
     }
     return $value;
 }
 /**
  * @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;
 }