3 回答
TA贡献2039条经验 获得超7个赞
下面是一个最小的工作例如反序列化XML到单个POS实例与ArrayCollection的POS_Source实例。我扔掉了对反序列化此特定XML而言不是必不可少的所有规范化器等。
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Serializer\Encoder\XmlEncoder;
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
class POS
{
// ... just as in the question ...
}
/**
* Minimal implementation of POS_Source for purposes of this deserialization example.
*/
class POS_Source
{
private $RequestorID;
public function setPOS(POS $POS)
{
}
public function getRequestorID()
{
return $this->RequestorID;
}
public function setRequestorID($RequestorID)
{
$this->RequestorID = $RequestorID;
}
}
$data = '<POS>
<!-- ... the same XML as in the question ... -->
</POS>';
$normalizers = [
new ArrayDenormalizer(),
new ObjectNormalizer(null, null, null, new ReflectionExtractor())
];
$encoders = [new XmlEncoder()];
$serializer = new Serializer($normalizers, $encoders);
$pos = $serializer->deserialize($data,POS::class,'xml');
dump($pos);
印刷:
POS {#14
-Source: Doctrine\Common\Collections\ArrayCollection {#11
-elements: array:4 [
0 => POS_Source {#17
-RequestorID: array:3 [
"@Type" => 11
"@ID" => "T921"
"CompanyName" => array:3 [
"@Code" => "CP"
"@CodeContext" => "123T"
"#" => ""
]
]
}
1 => POS_Source {#27
-RequestorID: array:3 [
"@Type" => 1
"@ID" => 34778
"#" => ""
]
}
2 => POS_Source {#22
-RequestorID: array:3 [
"@Type" => 9
"@ID" => "ZF"
"#" => ""
]
}
3 => POS_Source {#25
-RequestorID: array:3 [
"@Type" => 17
"@ID" => "mabaan"
"#" => ""
]
}
]
}
}
TA贡献1765条经验 获得超5个赞
这是部分答案,而不是解决方案。
因此,看起来反序列化不支持嵌入式php对象,并且您已经创建了一个自定义反序列化方法。
我仍在使用解决方案,但简短的答案是您必须遍历标准化数组,然后尝试匹配属性名称。我正在尝试寻找一种方法来查询对象,以仅查询序列化组文档块批注中包含的那些属性。
TA贡献2021条经验 获得超8个赞
在反序列化包含其他对象的对象时,必须提供ObjectNormalizer一个类型提取器来确定嵌套对象的类型。
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
// ...
$normalizers = [
new DateTimeNormalizer(),
new ArrayDenormalizer(),
new PropertyNormalizer(),
new ObjectNormalizer($classMetadataFactory, $metadataAwareNameConverter, null, new ReflectionExtractor()), // added type extractor as fourth argument
];
- 3 回答
- 0 关注
- 167 浏览
添加回答
举报