function it_denormalizes_association_with_products(Association $association, Serializer $serializer, ProductInterface $product1, ProductInterface $product2) { // Mock product denormalization $serializer->denormalize('foo', self::PRODUCT_CLASS, self::FORMAT_CSV)->willReturn($product1); $serializer->denormalize('bar', self::PRODUCT_CLASS, self::FORMAT_CSV)->willReturn($product2); $association->addProduct($product1)->shouldBeCalled(); $association->addProduct($product2)->shouldBeCalled(); $this->setSerializer($serializer); $this->denormalize('foo,bar', self::ENTITY_CLASS, self::FORMAT_CSV, ['part' => 'products', 'entity' => $association]); }
/** * @param $code * @return array */ public function deserializeForm($code) { $mappings_raw = json_decode($code, TRUE); $decoded_fillpdf_form = $this->serializer->denormalize($mappings_raw['form'], 'Drupal\fillpdf\Entity\FillPdfForm'); // Denormalization is a pain; we have to iterate over the fields to actually // recompose the $fields array. $field_json = $mappings_raw['fields']; $decoded_fields = []; foreach ($field_json as $normalized_field) { $field = $this->serializer->denormalize($normalized_field, 'Drupal\fillpdf\Entity\FillPdfFormField'); $decoded_fields[$field->pdf_key->value] = $field; } $return = ['form' => $decoded_fillpdf_form, 'fields' => $decoded_fields]; return $return; }
/** * {@inheritdoc} */ public function denormalize($data, $type, $format = null) { // Use alias namespace ? if (strrpos($type, ':') !== false) { list($namespaceAlias, $simpleClassName) = explode(':', $type); $type = $this->entityManager->getConfiguration()->getEntityNamespace($namespaceAlias) . '\\' . $simpleClassName; } return parent::denormalize($data, $type, $format); }
/** * @param array $data * @param \ReflectionParameter $param * @param string $format * @param array $context * * @return object */ protected function denormalizeParam($data, $param, $format, $context) { $name = $param->getName(); $index = $param->getPosition(); if (array_key_exists($name, $data)) { $value = $data[$name]; } elseif (array_key_exists($index, $data)) { $value = $data[$index]; } elseif ($param->isDefaultValueAvailable()) { $value = $param->getDefaultValue(); } else { $message = sprintf('Missing parameter #%s: %s', $index, $name); throw new \RuntimeException($message); } if ($param->getClass()) { $class = $param->getClass()->getName(); } elseif ($this->serializer->supportsDenormalization($value, MixedDenormalizer::TYPE, $format)) { $class = MixedDenormalizer::TYPE; } if (isset($class)) { $value = $this->serializer->denormalize($value, $class, $format, $context); } return $value; }
public function testDenormalizeWithSupportOnData() { $denormalizer1 = $this->getMock('Symfony\\Component\\Serializer\\Normalizer\\DenormalizerInterface'); $denormalizer1->method('supportsDenormalization')->willReturnCallback(function ($data, $type, $format) { return isset($data['test1']); }); $denormalizer1->method('denormalize')->willReturn('test1'); $denormalizer2 = $this->getMock('Symfony\\Component\\Serializer\\Normalizer\\DenormalizerInterface'); $denormalizer2->method('supportsDenormalization')->willReturn(true); $denormalizer2->method('denormalize')->willReturn('test2'); $serializer = new Serializer(array($denormalizer1, $denormalizer2)); $this->assertEquals('test1', $serializer->denormalize(array('test1' => true), 'test')); $this->assertEquals('test2', $serializer->denormalize(array(), 'test')); }
/** * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException */ public function testDenormalizeOnNormalizer() { $serializer = new Serializer(array(new TestNormalizer()), array()); $data = array('title' => 'foo', 'numbers' => array(5, 3)); $this->assertTrue($serializer->denormalize(json_encode($data), 'stdClass', 'json')); }
/** * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException * @expectedExceptionMessage The type of the key "a" must be "int" ("string" given). */ public function testRejectInvalidKey() { $extractor = new PropertyInfoExtractor(array(), array(new PhpDocExtractor(), new ReflectionExtractor())); $normalizer = new ObjectNormalizer(null, null, null, $extractor); $serializer = new Serializer(array(new ArrayDenormalizer(), new DateTimeNormalizer(), $normalizer)); $serializer->denormalize(array('inners' => array('a' => array('foo' => 1))), ObjectOuter::class); }
/** * @expectedException UnexpectedValueException * @expectedExceptionMessage The type of the "date" attribute for class "Symfony\Component\Serializer\Tests\Normalizer\ObjectOuter" must be one of "DateTimeInterface" ("string" given). */ public function testRejectInvalidType() { $normalizer = new ObjectNormalizer(null, null, null, new ReflectionExtractor()); $serializer = new Serializer(array($normalizer)); $serializer->denormalize(array('date' => 'foo'), ObjectOuter::class); }
/** * {@inheritdoc} */ public function denormalize($data, $type = null, $format = null, array $context = array()) { $type = isset($data[TypifiedNormalizer::META_CLASS]) ? $data[TypifiedNormalizer::META_CLASS] : $type; return parent::denormalize($data, $type, $format, $context); }
/** * {@inheritdoc} */ public function denormalize($data, $type, $format = null, array $context = []) { return parent::denormalize(is_array($data) ? $this->order($data) : $data, $type, $format, $context); }
public function deserialize(array $serializedObject) { return $this->serializer->denormalize($serializedObject['payload'], $serializedObject['class']); }
/** * {@inheritdoc} */ public function denormalize($data, $type, $format = null, array $context = array()) { $this->denormalizerCache = array(); // disable internal cache; return parent::denormalize($data, $type, $format, $context); }
public function testDenormalization() { $denormalizeBusService = $this->serializer->denormalize(['description' => 'description', 'id' => 1, 'information' => ['information'], 'lines' => ['navy'], 'name' => '1A', 'operation' => ['status' => 'running']], BusService::class); $this->assertEquals($this->busService, $denormalizeBusService); }