3

how can I deserialize this XML

<Outer>
   <simpleProperty1>A</simpleProperty1>
   <simpleProperty2>B</simpleProperty2>
   <Inner>
      <simpleProperty3>C</simpleProperty3>
      <simpleProperty4>D</simpleProperty4>
   </Inner>
   <Inner>
      <simpleProperty3>E</simpleProperty3>
      <simpleProperty4>F</simpleProperty4>
   </Inner>
</Outer>

into some PHP classes:

class Outer 
{
   /** @var string */
   private $simpleProperty1;
   /** @var string */
   private $simpleProperty2;
   /** @var Inner[] */
   private $inners;

   [insert getters and setters here]
}

class Inner 
{
   /** @var string */
   private $simpleProperty3;
   /** @var string */
   private $simpleProperty4;

   [insert getters and setters here]
}

using the Symfony Serializer?

The outer object and its simple properties are filled, but instead of the inner object I get an associative array containing two more associative arrays that contain the simpleProperty3 and simpleProperty4.

4
  • You could have a look at this and you probably will need to define a custom denormalizer function. You could also look at this question Commented Apr 3, 2017 at 13:00
  • Can you generate an xml like above with Symfony Serializer? Then you can also Deserialize it again. Currently the docu about that, are only with flat/simple objects. You can only go and inject somthing in the getter/setter for INNER and bind the values manualy. But better dig into the llinks that @segFault shows Commented Apr 3, 2017 at 13:00
  • @JustOnUnderMillions I can serialize, but I still cannot deserialize Commented Apr 3, 2017 at 13:14
  • Im not really into sympony, but when you can fully serialize it, it must be possible to deserialize it. Seems it is always the same class base. But after having a look into the links from @segFault it seems that complex structures need complex handling/work. symfony.com/doc/current/components/serializer.html Commented Apr 3, 2017 at 13:16

1 Answer 1

3

I was able to solve it with a custom PropertyExtractor that points the serializer to the correct type:

$encoders = [new XmlEncoder('response', LIBXML_NOERROR)];
$normalizers = [
    new ArrayDenormalizer(),
    new ObjectNormalizer(null, null, null, 
      new class implements PropertyTypeExtractorInterface
        {
          private $reflectionExtractor;

          public function __construct()
          {
              $this->reflectionExtractor = new ReflectionExtractor();
          }

          public function getTypes($class, $property, array $context = array())
          {
              if (is_a($class, Outer::class, true) && 'Inner' === $property) {
                return [
                  new Type(Type::BUILTIN_TYPE_OBJECT, true, Inner::class . "[]")
                ];
              }
              return $this->reflectionExtractor->getTypes($class, $property, $context);
          }
        })
    ];
$this->serializer = new Serializer($normalizers, $encoders);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.