<commit_before><?php

namespace Vespolina\Workflow\JMSSerializer;

use JMS\Serializer\AbstractVisitor;
use JMS\Serializer\Context;
use JMS\Serializer\GraphNavigator;
use JMS\Serializer\Handler\SubscribingHandlerInterface;
use Vespolina\Workflow\Token;

class TokenHandler implements SubscribingHandlerInterface
{
    /**
     * {@inheritDoc}
     */
    public static function getSubscribingMethods()
    {
        return array(array(
            'direction' => GraphNavigator::DIRECTION_SERIALIZATION,
            'format'    => 'json',
            'type'      => 'Vespolina\Workflow\Token',
            'method'    => 'serializeToken',
        ), array(
            'direction' => GraphNavigator::DIRECTION_DESERIALIZATION,
            'format'    => 'json',
            'type'      => 'Vespolina\Workflow\Token',
            'method'    => 'deserializeToken',
        ));
    }

    /**
     * @param  AbstractVisitor  $visitor
     * @param  Token            $token
     * @param  string           $type
     * @param  Context          $context
     * @return array
     */
    public function serializeToken(AbstractVisitor $visitor, Token $token, $type, Context $context)
    {
        $data = [];

        foreach ($token->getData() as $key => $datum) {
            $datumType = $this->getType($datum);
            $type = [
                'name' => $datumType,
                'params' => [],
            ];
            $argsData = '';
            if ($type['name'] === 'array') {
                $argsData = [];
                foreach ($datum as $arrayKey => $arrayData) {
                    $arrayType = [
                        'name' => $this->getType($arrayData),
                        'params' => [],
                    ];
                    $argsData[] = ['key' => $arrayKey, 'args' => $context->accept($arrayData, $arrayType), 'type' => $arrayType];
                }
            } else {
                 $argsData = $context->accept($datum, $type);
            }
            $args = ['key' => $key, 'args' => $argsData, 'type' => $type];

            $data[] = $args;
        }

        $data = array(
            'data'      => $data,
            'location'  => $token->getLocation(),
        );

        $visitor->setRoot($data);

        return $data;
    }

    /**
     * @param  AbstractVisitor $visitor
     * @param  array           $data
     * @param  string          $type
     * @param  Context         $context
     * @return Envelope
     */
    public function deserializeToken(AbstractVisitor $visitor, array $data, $type, Context $context)
    {
        $token = new Token();
        $token->setLocation($data['location']);
        foreach ($data['data'] as $datum) {
            $type = $datum['type'];

            if ($type['name'] === 'array') {
                $argsData = [];
                foreach ($datum['args'] as $arrayData) {
                    $arrayKey = $arrayData['key'];
                    $argsData[$arrayKey] = $context->accept($arrayData['args'], $arrayData['type']);
                }
            } else {
                $argsData = $context->accept($datum, $type);
            }<commit_msg>bug fix for non-array token data<commit_after><?php

namespace Vespolina\Workflow\JMSSerializer;

use JMS\Serializer\AbstractVisitor;
use JMS\Serializer\Context;
use JMS\Serializer\GraphNavigator;
use JMS\Serializer\Handler\SubscribingHandlerInterface;
use Vespolina\Workflow\Token;

class TokenHandler implements SubscribingHandlerInterface
{
    /**
     * {@inheritDoc}
     */
    public static function getSubscribingMethods()
    {
        return array(array(
            'direction' => GraphNavigator::DIRECTION_SERIALIZATION,
            'format'    => 'json',
            'type'      => 'Vespolina\Workflow\Token',
            'method'    => 'serializeToken',
        ), array(
            'direction' => GraphNavigator::DIRECTION_DESERIALIZATION,
            'format'    => 'json',
            'type'      => 'Vespolina\Workflow\Token',
            'method'    => 'deserializeToken',
        ));
    }

    /**
     * @param  AbstractVisitor  $visitor
     * @param  Token            $token
     * @param  string           $type
     * @param  Context          $context
     * @return array
     */
    public function serializeToken(AbstractVisitor $visitor, Token $token, $type, Context $context)
    {
        $data = [];

        foreach ($token->getData() as $key => $datum) {
            $datumType = $this->getType($datum);
            $type = [
                'name' => $datumType,
                'params' => [],
            ];
            $argsData = '';
            if ($type['name'] === 'array') {
                $argsData = [];
                foreach ($datum as $arrayKey => $arrayData) {
                    $arrayType = [
                        'name' => $this->getType($arrayData),
                        'params' => [],
                    ];
                    $argsData[] = ['key' => $arrayKey, 'args' => $context->accept($arrayData, $arrayType), 'type' => $arrayType];
                }
            } else {
                 $argsData = $context->accept($datum, $type);
            }
            $args = ['key' => $key, 'args' => $argsData, 'type' => $type];

            $data[] = $args;
        }

        $data = array(
            'data'      => $data,
            'location'  => $token->getLocation(),
        );

        $visitor->setRoot($data);

        return $data;
    }

    /**
     * @param  AbstractVisitor $visitor
     * @param  array           $data
     * @param  string          $type
     * @param  Context         $context
     * @return Envelope
     */
    public function deserializeToken(AbstractVisitor $visitor, array $data, $type, Context $context)
    {
        $token = new Token();
        $token->setLocation($data['location']);
        foreach ($data['data'] as $datum) {
            $type = $datum['type'];

            if ($type['name'] === 'array') {
                $argsData = [];
                foreach ($datum['args'] as $arrayData) {
                    $arrayKey = $arrayData['key'];
                    $argsData[$arrayKey] = $context->accept($arrayData['args'], $arrayData['type']);
                }
            } else {
                $argsData = $context->accept($datum['args'], $type);
            }
{'fraction_non_alphanumeric': 0.14988179669030732, 'fraction_numerical': 0.0, 'mean_word_length': 1.632102861883036, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 67, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '3103539', 'n_tokens_mistral': 1792, 'n_tokens_neox': 1742, 'n_words': 345}