动态删除JMS序列化事件订户中的实体



我有一个学说实体,我使用JMS序列化器将其呈现在我的API中。

我想添加这样的布尔字段:

/**
 * @var bool
 *
 * @ORMColumn(name = "is_serialized", type = "boolean")
 */
protected $isSerialized = true;

我还使用EventuBscriber在序列化之前向我的实体添加一些数据。

我想基于$isSerialized值动态包含或非每个实体(我无法修改学说查询)。

class SerializationEventSubscriber extends EventSubscriberInterface
{
    /**
     * @param ObjectEvent $event
     */
    public function onPostSerialize(ObjectEvent $event)
    {
        if (!$this->isGroup('api', $event)) {
            return;
        }
        $entity  = $event->getObject();
        $visitor = $event->getVisitor();
        if (!$object->isSerialized()) {
            // Skip the current object and remove it from serialization
        }
    }
}

我找不到有关此的任何信息,在JMS注释文档中都没有。

这是我的 EventListener,但是我没有删除对象,而只是跳过了无效的字段。

use DoctrineORMMappingClassMetadata;
use DoctrineORMEntityManagerInterface;
use JMSSerializerJsonSerializationVisitor;
use JMSSerializerEventDispatcherObjectEvent;
use JMSSerializerMetadataClassMetadata as JMSClassMetadata;
use JMSSerializerMetadataStaticPropertyMetadata;
class EntitySerializerListener
{
    /**
     * @var EntityManagerInterface
     */
    protected $em;
    public function __construct(EntityManagerInterface $entityManager)
    {
        $this->em = $entityManager;
    }
    public function onPostSerialize(ObjectEvent $event)
    {
        /** @var JsonSerializationVisitor $visitor */
        $object = $event->getObject();
        $visitor = $event->getVisitor();
        $context = $event->getContext();
        $type = $event->getType();
        /** @var JMSClassMetadata $metadata */
        $metadata = $context->getMetadataFactory()->getMetadataForClass($type['name']);
        $data = $visitor->endVisitingObject($metadata, $object, $type);
        $visitor->startVisitingObject($metadata, $object, $type);
        // Here I remove unnecessary fields
        $this->prune($type['name'], $data);
 
        // Reset fresh serialized data
        foreach ($data as $field => $value) {
            $visitor->visitProperty(new StaticPropertyMetadata($type['name'], $field, $value), $value);
        }
    }
    /**
     * Prune the empty field which was set to NULL by MaxDepth annotation but left in the data graph by JWT serializer.
     *
     * @param string $fqcn
     * @param array $data
     */
    protected function prune(string $fqcn, array & $data)
    {
        /** @var ClassMetadata $metadata */
        $metadata = $this->em->getMetadataFactory()->getMetadataFor($fqcn);
        // Handle association
        $associations = $metadata->getAssociationMappings();
        foreach ($associations as $field => $association) {
            if (!array_key_exists($field, $data)) {
                continue;
            }
            
            // Here remove entity or any other field which you want
            if (empty($data[$field])) {
                unset($data[$field]);
            } else {
                $this->prune($association['targetEntity'], $data[$field]);
            }
        }
    }
}

相关内容

  • 没有找到相关文章

最新更新