<commit_before><?php

namespace Hateoas\Serializer\EventSubscriber;

use Hateoas\Factory\EmbeddedsFactory;
use Hateoas\Factory\LinksFactory;
use Hateoas\Serializer\EmbedSerializer;
use Hateoas\Serializer\JMSSerializerMetadataAwareInterface;
use Hateoas\Serializer\XmlSerializerInterface;
use JMS\Serializer\EventDispatcher\Events;
use JMS\Serializer\EventDispatcher\EventSubscriberInterface;
use JMS\Serializer\EventDispatcher\ObjectEvent;
use Metadata\MetadataFactoryInterface;

/**
 * @author <NAME> <<EMAIL>>
 */
class XmlEventSubscriber implements EventSubscriberInterface, JMSSerializerMetadataAwareInterface
{
    /**
     * {@inheritdoc}
     */
    public static function getSubscribedEvents()
    {
        return array(
            array(
                'event'  => Events::POST_SERIALIZE,
                'format' => 'xml',
                'method' => 'onPostSerialize',
            ),
        );
    }

    /**
     * @var XmlSerializerInterface
     */
    private $xmlSerializer;

    /**
     * @var LinksFactory
     */
    private $linksFactory;

    /**
     * @var EmbeddedsFactory
     */
    private $embeddedsFactory;

    /**
     * @var MetadataFactoryInterface
     */
    private $serializerMetadataFactory;

    /**
     * @param XmlSerializerInterface $xmlSerializer
     * @param LinksFactory           $linksFactory
     * @param EmbeddedsFactory       $embeddedsFactory
     */
    public function __construct(XmlSerializerInterface $xmlSerializer, LinksFactory $linksFactory, EmbeddedsFactory $embeddedsFactory)
    {
        $this->xmlSerializer    = $xmlSerializer;
        $this->linksFactory     = $linksFactory;
        $this->embeddedsFactory = $embeddedsFactory;
    }

    public function setMetadataFactory(MetadataFactoryInterface $metadataFactory)
    {
        $this->serializerMetadataFactory = $metadataFactory;
    }

    public function onPostSerialize(ObjectEvent $event)
    {
        $object    = $event->getObject();
        $context   = $event->getContext();
        $embeddeds = $this->embeddedsFactory->create($event->getObject(), $event->getContext());
        $links     = $this->linksFactory->create($event->getObject(), $event->getContext());

        if (count($links) > 0) {
            $this->xmlSerializer->serializeLinks($links, $event->getVisitor(), $context);
        }

        if (count($embeddeds) > 0) {
            // This fixes the $context->getDepth()
            $context->startVisiting($object);
            $context->pushClassMetadata($this->serializerMetadataFactory->getMetadataForClass($event->getType()['name']));

            $this->xmlSerializer->serializeEmbeddeds($embeddeds, $event->getVisitor(), new EmbedSerializer($context));

            $context->stopVisiting($object);
            $context->popClassMetadata();
        }
    }
}
<commit_msg>fix depth check<commit_after><?php

namespace Hateoas\Serializer\EventSubscriber;

use Hateoas\Factory\EmbeddedsFactory;
use Hateoas\Factory\LinksFactory;
use Hateoas\Serializer\EmbedSerializer;
use Hateoas\Serializer\JMSSerializerMetadataAwareInterface;
use Hateoas\Serializer\XmlSerializerInterface;
use JMS\Serializer\EventDispatcher\Events;
use JMS\Serializer\EventDispatcher\EventSubscriberInterface;
use JMS\Serializer\EventDispatcher\ObjectEvent;
use Metadata\MetadataFactoryInterface;

/**
 * @author <NAME> <<EMAIL>>
 */
class XmlEventSubscriber implements EventSubscriberInterface, JMSSerializerMetadataAwareInterface
{
    /**
     * {@inheritdoc}
     */
    public static function getSubscribedEvents()
    {
        return array(
            array(
                'event'  => Events::POST_SERIALIZE,
                'format' => 'xml',
                'method' => 'onPostSerialize',
            ),
        );
    }

    /**
     * @var XmlSerializerInterface
     */
    private $xmlSerializer;

    /**
     * @var LinksFactory
     */
    private $linksFactory;

    /**
     * @var EmbeddedsFactory
     */
    private $embeddedsFactory;

    /**
     * @var MetadataFactoryInterface
     */
    private $serializerMetadataFactory;

    /**
     * @param XmlSerializerInterface $xmlSerializer
     * @param LinksFactory           $linksFactory
     * @param EmbeddedsFactory       $embeddedsFactory
     */
    public function __construct(XmlSerializerInterface $xmlSerializer, LinksFactory $linksFactory, EmbeddedsFactory $embeddedsFactory)
    {
        $this->xmlSerializer    = $xmlSerializer;
        $this->linksFactory     = $linksFactory;
        $this->embeddedsFactory = $embeddedsFactory;
    }

    public function setMetadataFactory(MetadataFactoryInterface $metadataFactory)
    {
        $this->serializerMetadataFactory = $metadataFactory;
    }

    public function onPostSerialize(ObjectEvent $event)
    {
        $object    = $event->getObject();
        $context   = $event->getContext();
        
        // This fixes the $context->getDepth()
        $context->startVisiting($object);
        $context->pushClassMetadata($this->serializerMetadataFactory->getMetadataForClass($event->getType()['name']));
            
        $embeddeds = $this->embeddedsFactory->create($event->getObject(), $event->getContext());
        $links     = $this->linksFactory->create($event->getObject(), $event->getContext());

        if (count($links) > 0) {
            $this->xmlSerializer->serializeLinks($links, $event->getVisitor(), $context);
        }

        if (count($embeddeds) > 0) {
            $this->xmlSerializer->serializeEmbeddeds($embeddeds, $event->getVisitor(), new EmbedSerializer($context));
        }
        
        $context->stopVisiting($object);
        $context->popClassMetadata();
    }
}

{'fraction_non_alphanumeric': 0.12154306852210675, 'fraction_numerical': 0.0007045974986788797, 'mean_word_length': 2.9266943291839556, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 75, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 16}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4738998', 'n_tokens_mistral': 1589, 'n_tokens_neox': 1518, 'n_words': 248}