JMS Serializer Discriminater by by Root Name

  Kiến thức lập trình

I would like to discriminate by rootNodeName

So that I can handle XML like these:

<A>
   <Id>1234</Id>
</A>
<B>
   <Id>5678</Id>
</B>

and handle them by the same deserialize line.

$message = $serializer->deserialize($xml, Message::class, 'xml');
#[JMSDiscriminator(field: '__xmlDiscriminator', map: [
    "A" => A::class,
    "B" => B::class
])]
#[JMSXmlDiscriminator(attribute: false, cdata: false)]
abstract class Message

So that I have at the end two Classes:

#[JMSXmlRoot("A")]
class A extends Message {}
#[JMSXmlRoot("B")]
class B extends Message {}

is that somehow possible?
The XML Structure is extern and cant be changed. The only thing I can tell which XML it is, is by the name of the RootNode

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT