이 매우 간단한 XML을 역 직렬화하려고합니다.
<?xml version="1.0" encoding="utf-8"?>
<root>
<namespace>foo</namespace>
<resource>bar</resource>
<description>baz</description>
<arguments>
<argument>
<name>foo</name>
<type>string</type>
<description>foo</description>
</argument>
<argument>
<name>bar</name>
<type>string</type>
<description>bar</description>
</argument>
</arguments>
</root>
나는에 인수 요소를 역 직렬화 할 수없는 무엇을하려고 아무리 array
또는 ArrayCollection
배열이 항상 비어 끝으로 수집 모든 종류의 나.
/**
* @Type("array<Model\Argument>")
*/
private $arguments;
/**
* @Type("ArrayCollection<Model\Argument>")
*/
private $arguments;
둘 다 빈 배열로 끝납니다. 그래서 인수에 대해 별도의 개체를 만들려고했습니다.
따라서 원래 속성은 다음과 같이 끝납니다.
/**
* @Type("Model\Arguments")
*/
private $arguments;
그리고 수업
class Arguments {
/**
* @Type("array<App\Blueprint\Model\Argument>")
*/
private $arguments;
/**
* @return mixed
*/
public function getArguments() {
return $this->arguments;
}
/**
* @param mixed $arguments
*/
public function setArguments($arguments) {
$this->arguments = $arguments;
}
}
배열은 여전히 비어 있습니다. 그러나 유형을로 변경 @Type("App\Blueprint\Model\Argument")
하면 첫 번째 객체 만 정확한 객체를 얻을 수 있습니다.
역 직렬화 처리기를 등록하려고 할 때 SimpleXmlElement
다음과 같이 오기 때문에 수정할 수 없으며 It is not yet possible to assign complex types to properties
경고 가 표시됩니다.
편집하다
지금은 어리석은 방식으로 해결했습니다.
$registry->registerHandler(
'deserialization',
'Model\Arguments',
'xml',
function(XmlDeserializationVisitor $visitor, $data, array $type, DeserializationContext $context) {
$arguments = [];
foreach($data->children() as $child) {
$arguments[] = SerializerBuilder::create()
->build()
->deserialize($child->asXML(), 'Model\Argument', 'xml');
}
return $arguments;
}
);
누군가가 더 나은 해결책을 가지고 있다면 여기에 남겨 두십시오 .
당신은 아주 가까웠습니다. 두 개의 개별 클래스를 정의해야합니다.
/**
* @Type("Model\Arguments")
*/
public $arguments;
/**
* @Type("array<Model\Argument>")
* @XmlList(inline = true, entry = "argument")
*/
public $argument;
클래스 의 @XmlList
속성을 확인합니다 Argument
. 게터와 세터도 제거 할 수 있습니다!
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다