/**
  * @param array $serializedObject
  *
  * @return mixed
  *
  * @throws SerializationException
  */
 public function deserialize(array $serializedObject)
 {
     Assert::keyExists($serializedObject, 'type', "Key 'type' should be set.");
     Assert::keyExists($serializedObject, 'payload', "Key 'payload' should be set.");
     // TODO implement TypeDenormalizer
     return $this->denormalizer->denormalize($serializedObject['payload'], $serializedObject['type'], 'json');
 }
 /**
  * @test
  */
 public function it_deserializes_objects_by_passing_then_to_the_denormalizer()
 {
     $data = array('type' => 'aFlyingDuck', 'payload' => array('duck_name' => 'bob'));
     $result = new \stdClass();
     $this->denormalizerMock->expects(self::atLeastOnce())->method('denormalize')->with(array('duck_name' => 'bob'), 'aFlyingDuck', 'json')->willReturn($result);
     self::assertSame($result, $this->serializer->deserialize($data));
 }
 /**
  * {@inheritdoc}
  */
 public function denormalize($data, $class, $format = null, array $context = array())
 {
     $productValues = [];
     foreach ($data as $attFieldName => $dataValue) {
         $attributeInfos = $this->fieldNameBuilder->extractAttributeFieldNameInfos($attFieldName);
         if (null !== $attributeInfos) {
             $attribute = $attributeInfos['attribute'];
             unset($attributeInfos['attribute']);
             $valueKey = $attribute->getCode();
             if ($attribute->isLocalizable()) {
                 $valueKey .= '-' . $attributeInfos['locale_code'];
             }
             if ($attribute->isScopable()) {
                 $valueKey .= '-' . $attributeInfos['scope_code'];
             }
             if (isset($productValues[$valueKey])) {
                 $value = $productValues[$valueKey];
             } else {
                 $value = new $this->valueClass();
                 $value->setAttribute($attribute);
                 $value->setLocale($attributeInfos['locale_code']);
                 $value->setScope($attributeInfos['scope_code']);
             }
             $productValues[$valueKey] = $this->valueDenormalizer->denormalize($dataValue, $this->valueClass, 'csv', ['entity' => $value] + $attributeInfos);
         }
     }
     return new ArrayCollection($productValues);
 }
 /**
  * {@inheritdoc}
  */
 public function denormalize($data, $class, $format = null, array $context = [])
 {
     $data = $this->valuesDenormalizer->denormalize($data, $class, $format, $context);
     if (null !== $data) {
         $data = $this->localizer->localize($data, $context);
     }
     return $data;
 }
 /**
  * {@inheritdoc}
  */
 public function denormalize($data, $class, $format = null, array $context = [])
 {
     $metric = $this->metricDenormalizer->denormalize($data, $class, $format, $context);
     if (null !== $metric) {
         $metric->setData($this->localizer->localize($metric->getData(), $context));
     }
     return $metric;
 }
 /**
  * @param FormEvent $event
  */
 public function preSetData(FormEvent $event)
 {
     $data = $event->getData();
     if (null === $data || !$data instanceof ProductTemplateInterface) {
         return;
     }
     $values = $this->denormalizer->denormalize($data->getValuesData(), 'ProductValue[]', 'json', ['locale' => $this->localeResolver->getCurrentLocale(), 'disable_grouping_separator' => true]);
     $data->setValues($values);
 }
 /**
  * @param FormEvent $event
  */
 public function preSetData(FormEvent $event)
 {
     $data = $event->getData();
     if (null === $data || !$data instanceof ProductTemplateInterface) {
         return;
     }
     $values = $this->denormalizer->denormalize($data->getValuesData(), 'ProductValue[]', 'json');
     $data->setValues($values);
 }
 /**
  * {@inheritdoc}
  */
 public function denormalize($data, $class, $format = null, array $context = [])
 {
     $prices = $this->priceDenormalizer->denormalize($data, $class, $format, $context);
     if (null !== $prices) {
         foreach ($prices as $price) {
             $price->setData($this->localizer->localize($price->getData(), $context));
         }
     }
     return $prices;
 }
 public function denormalize(DenormalizerInterface $denormalizer, $data, $format = null, array $context = array())
 {
     if (isset($data['domains'])) {
         foreach ($data['domains'] as $value) {
             $domain = $denormalizer->denormalize($value, __NAMESPACE__ . '\\' . 'Domain');
             $this->addDomain($domain);
         }
         unset($data['domains']);
     }
     return parent::denormalize($denormalizer, $data, $format);
 }
 public function denormalize(DenormalizerInterface $denormalizer, $data, $format = null, array $context = array())
 {
     if (isset($data['mailboxes'])) {
         foreach ($data['mailboxes'] as $value) {
             $mailbox = $denormalizer->denormalize($value, __NAMESPACE__ . '\\' . 'Mailbox');
             $this->addMailbox($mailbox);
         }
         unset($data['mailboxes']);
     }
     return parent::denormalize($denormalizer, $data, $format);
 }
 /**
  * {@inheritdoc}
  */
 public function denormalize($data, $class, $format = null, array $context = [])
 {
     $values = new ArrayCollection();
     foreach ($data as $attributeCode => $valuesData) {
         $attribute = $this->attributeRepository->findOneByIdentifier($attributeCode);
         foreach ($valuesData as $valueData) {
             $value = $this->denormalizer->denormalize($valueData, $this->valueClass, 'json', ['attribute' => $attribute] + $context);
             $values->add($value);
         }
     }
     return $values;
 }
 /**
  * Build product values from template values raw data
  *
  * @param ProductTemplateInterface $template
  * @param AttributeInterface[]     $attributes
  *
  * @return ProductValueInterface[]
  */
 protected function buildProductValuesFromTemplateValuesData(ProductTemplateInterface $template, array $attributes)
 {
     $values = $this->denormalizer->denormalize($template->getValuesData(), 'ProductValue[]', 'json');
     $product = new $this->productClass();
     foreach ($values as $value) {
         $product->addValue($value);
     }
     foreach ($attributes as $attribute) {
         $this->productBuilder->addAttributeToProduct($product, $attribute);
     }
     return $product->getValues();
 }
 /**
  * Build product values from template values raw data
  *
  * @param ProductTemplateInterface $template
  * @param AttributeInterface[]     $attributes
  * @param string                   $locale
  *
  * @return ProductValueInterface[]
  */
 protected function buildProductValuesFromTemplateValuesData(ProductTemplateInterface $template, array $attributes, $locale)
 {
     $options = ['locale' => $locale, 'disable_grouping_separator' => true];
     $values = $this->denormalizer->denormalize($template->getValuesData(), 'ProductValue[]', 'json', $options);
     $product = new $this->productClass();
     foreach ($values as $value) {
         $product->addValue($value);
     }
     foreach ($attributes as $attribute) {
         $this->productBuilder->addAttributeToProduct($product, $attribute);
     }
     $this->productBuilder->addMissingProductValues($product);
     return $product->getValues();
 }
 /**
  * @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;
 }
Exemplo n.º 16
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);
 }
Exemplo n.º 17
0
 /**
  * {@inheritdoc}
  */
 public function denormalize($data, $class, $format = null, array $context = [])
 {
     if (isset($data['#type'])) {
         $type = $data['#type'];
         unset($data['#type']);
         $data = $this->denormalize($data, $type, $format, $context);
         $data = $this->objectNormalizer->denormalize($data, $type, $format, $context);
         return $data;
     }
     if (is_array($data) || $data instanceof \Traversable) {
         foreach ($data as $key => $value) {
             $data[$key] = $this->serializer->denormalize($value, $class, $format, $context);
         }
     }
     return $data;
 }
 /**
  * {@inheritdoc}
  */
 public function denormalize($data, $referenceDataClass, $format = null, array $context = [])
 {
     if (empty($data)) {
         return new ArrayCollection();
     }
     if (false === is_array($data)) {
         throw new InvalidParameterException(sprintf('Data expected to be an array.'));
     }
     $referenceDataColl = new ArrayCollection();
     foreach ($data as $singleData) {
         $referenceData = $this->referenceDataDenormalizer->denormalize($singleData, $referenceDataClass, $format, $context);
         if (null !== $referenceData) {
             $referenceDataColl->add($referenceData);
         }
     }
     return $referenceDataColl;
 }
 /**
  * Normalizes and returns the media values of a product template
  *
  * @param ProductTemplateInterface|null $template
  *
  * @return \Pim\Bundle\CatalogBundle\Model\ProductValueInterface[]
  */
 protected function getProductTemplateMediaValues(ProductTemplateInterface $template = null)
 {
     if (null === $template) {
         return [];
     }
     $values = $this->denormalizer->denormalize($template->getValuesData(), 'ProductValue[]', 'json');
     return $values->filter(function ($value) {
         return in_array($value->getAttribute()->getAttributeType(), [AttributeTypes::IMAGE, AttributeTypes::FILE]);
     })->toArray();
 }
Exemplo n.º 20
0
 /**
  * Normalize the variant group values
  *
  * @param GroupInterface $group
  * @param string         $format
  * @param array          $context
  *
  * @return array
  */
 protected function normalizeVariantGroupValues(GroupInterface $group, $format, array $context)
 {
     if (!$group->getType()->isVariant() || null === $group->getProductTemplate()) {
         return [];
     }
     $context["entity"] = "variant-group";
     // As variant group > product template > values data are not type hinted we cannot normalize them directly
     // so we first denormalize them into product values using the common format then normalize them
     // this allow to transform localization based values for example
     return $this->valuesNormalizer->normalize($this->valuesDenormalizer->denormalize($group->getProductTemplate()->getValuesData(), $format, $context), $format, $context);
 }
 /**
  * Filter empty values that are not used in a template then denormalize the product values objects from CSV fields
  *
  * @param array                    $rawProductValues
  * @param ProductTemplateInterface $template
  *
  * @return ProductValueInterface[]
  */
 protected function denormalizeValuesFromItemData(array $rawProductValues, ProductTemplateInterface $template = null)
 {
     $templateCodes = null !== $template ? array_keys($template->getValuesData()) : [];
     foreach ($rawProductValues as $index => $data) {
         $attributeInfos = $this->fieldNameBuilder->extractAttributeFieldNameInfos($index);
         $attribute = $attributeInfos['attribute'];
         if ("" === trim($data) && !in_array($attribute->getCode(), $templateCodes)) {
             unset($rawProductValues[$index]);
         }
     }
     return $this->denormalizer->denormalize($rawProductValues, 'ProductValue[]', 'csv');
 }
 public function testDenormalizationWithType()
 {
     $dummy = $this->getDummy();
     $this->normalizer->addType('dummy', TypehintNormalizerClassDummy::class);
     $this->normalizer->addType('subdummy', TypehintNormalizerSubclassDummy::class);
     $expected = $dummy;
     $normalized = $this->normalizer->normalize($dummy);
     $this->assertTrue($this->normalizer->supportsDenormalization($normalized, RecursiveNormalizerClassDummy::class));
     $actual = $this->normalizer->denormalize($normalized, TypehintNormalizerClassDummy::class);
     $this->assertEquals($expected, $actual);
 }
Exemplo n.º 23
0
 /**
  * Update the variant group fields
  *
  * @param GroupInterface $group
  * @param array          $groupData
  *
  * @return GroupInterface
  */
 protected function updateGroup(GroupInterface $group, array $groupData)
 {
     $group = $this->denormalizer->denormalize($groupData, $this->class, $this->format, ['entity' => $group]);
     return $group;
 }
Exemplo n.º 24
0
 /**
  * {@inheritdoc}
  */
 public function handleGetRequest(Request $request, $identifier)
 {
     $resource = $this->getResourceById($identifier);
     $data = $this->serializer->serialize($resource, self::RESPONSE_FORMAT, ['group' => $this->getResourceType()]);
     return new Response($data);
 }
Exemplo n.º 25
0
 /**
  * {@inheritdoc}
  */
 public function supportsDenormalization($data, $type, $format = null)
 {
     return substr($type, -2) === '[]' && $this->serializer->supportsDenormalization($data, substr($type, 0, -2), $format);
 }