I am using the JMS/Serialzier library.
I have setup an Event-Subscriber, which listens for Events::PRE_SERIALIZE
and I then convert all the object instances of an the class Price
having the property currency
and amount
into different currencies.
public function onPreSerialize(PreSerializeEvent $event)
{
$object = $event->getObject();
$class = get_class($object);
switch ($class) {
case Price::class:
return $this->currencyService->convertPrice($object);
}
}
Yet now, in my application I have the edge case that one price belonging to one container object EdgeCase
does not need to be converted at all:
use JMS\Serializer\Annotation\Type;
class EdgeCase {
/**
* @Type("Kopernikus\Price")
* @var Price
*/
private $price; // this one instance should not be handled by the event subscriber
}
but has to retain its original state. Yet I don't seem to be able to differentiate the origin of where my object is comming from.
I want to be able to configure which Price
objects should be converted and when.