dpw43061
2017-04-03 12:48
浏览 102
已采纳

如何使用Symfony Serializer反序列化一系列复杂对象?

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.

图片转代码服务由CSDN问答提供 功能建议

如何反序列化此XML

 &lt; Outer&gt;  
&lt; simpleProperty1&gt; A&lt; / simpleProperty1&gt; 
&lt; simpleProperty2&gt; B&lt; / simpleProperty2&gt; 
&lt; Inner&gt; 
&lt; simpleProperty3&gt; C&lt; / simpleProperty3&gt; 
&lt; simpleProperty4&gt; D&lt; / simpleProperty4&gt;  
&lt; / Inner&gt; 
&lt; Inner&gt; 
&lt; simpleProperty3&gt; E&lt; / simpleProperty3&gt; 
&lt; simpleProperty4&gt; F&lt; / simpleProperty4&gt; 
&lt; / Inner&gt; 
&lt; / Outer&gt; 
    
 
 

进入某些PHP类:

  class Outer 
 {
 / ** @var string * / \  n private $ simpleProperty1; 
 / ** @var string * / 
 private $ simpleProperty2; 
 / ** @var Inner [] * / 
 private $ inners; 
 
 [在这里插入getter和setter]  
 
 
 
class Inner 
 {
 / ** @var string * / 
 private $ simpleProperty3; 
 / ** @var string * / 
 private $ simpleProperty4; 
 
 [insert getters 和设置器] 
} 
   
 
 

使用 Symfony Serializer

外部对象及其简单属性被填充,但是我得到一个关联数组,而不是内部对象,它包含两个包含 simpleProperty3 simpleProperty4 的关联数组。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douyou2234 2017-04-03 13:40
    已采纳

    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);
    
    点赞 打赏 评论

相关推荐 更多相似问题