<commit_before><?php
/**
 * @author    <NAME> <<EMAIL>>
 *
 * @copyright Interactive Solutions
 */

declare(strict_types = 1);

namespace InteractiveSolutions\Bernard\Serializer;

use Bernard\Envelope;
use Bernard\Serializer;
use InvalidArgumentException;
use ReflectionClass;

class ExplicitSerializer implements Serializer
{
    /**
     * @param  Envelope $envelope
     *
     * @return string
     */
    public function serialize(Envelope $envelope)
    {
        $message = $envelope->getMessage();
        if (!$message instanceof AbstractExplicitMessage) {
            throw new InvalidArgumentException();
        }

        $reflection = new ReflectionClass($message);

        $args = [];

        foreach ($reflection->getProperties() as $property) {
            $property->setAccessible(true);

            $args[$property->getName()] = $property->getValue($message);
        }

        return json_encode([
            'args'      => $args,
            'class'     => $envelope->getClass(),
            'timestamp' => $envelope->getTimestamp(),
        ]);
    }

    /**
     * @return Envelope
     */
    public function deserialize($serialized)
    {
        $data = json_decode($serialized);

        $reflection = new ReflectionClass($data->class);

        $instance = $reflection->newInstanceWithoutConstructor();

        foreach ($data->args as $key => $value) {
            $property = $reflection->getProperty($key);
            $property->setAccessible(true);
            $property->setValue($instance, $value);
        }

        $reflection = new ReflectionClass(AbstractExplicitMessage::class);

        $property = $reflection->getProperty('timestamp');
        $property->setAccessible(true);
        $property->setValue($instance, $data->timestamp);

        return new Envelope($instance);
    }
}
<commit_msg>Decode to array instead of object.<commit_after><?php
/**
 * @author    <NAME> <<EMAIL>>
 *
 * @copyright Interactive Solutions
 */

declare(strict_types = 1);

namespace InteractiveSolutions\Bernard\Serializer;

use Bernard\Envelope;
use Bernard\Serializer;
use InvalidArgumentException;
use ReflectionClass;

class ExplicitSerializer implements Serializer
{
    /**
     * @param  Envelope $envelope
     *
     * @return string
     */
    public function serialize(Envelope $envelope)
    {
        $message = $envelope->getMessage();
        if (!$message instanceof AbstractExplicitMessage) {
            throw new InvalidArgumentException();
        }

        $reflection = new ReflectionClass($message);

        $args = [];

        foreach ($reflection->getProperties() as $property) {
            $property->setAccessible(true);

            $args[$property->getName()] = $property->getValue($message);
        }

        return json_encode([
            'args'      => $args,
            'class'     => $envelope->getClass(),
            'timestamp' => $envelope->getTimestamp(),
        ]);
    }

    /**
     * @return Envelope
     */
    public function deserialize($serialized)
    {
        $data = json_decode($serialized, true);

        $reflection = new ReflectionClass($data['class']);

        $instance = $reflection->newInstanceWithoutConstructor();

        foreach ($data['args'] as $key => $value) {
            $property = $reflection->getProperty($key);
            $property->setAccessible(true);
            $property->setValue($instance, $value);
        }

        $reflection = new ReflectionClass(AbstractExplicitMessage::class);

        $property = $reflection->getProperty('timestamp');
        $property->setAccessible(true);
        $property->setValue($instance, $data['timestamp']);

        return new Envelope($instance);
    }
}

{'fraction_non_alphanumeric': 0.13644910018802042, 'fraction_numerical': 0.0005372011818426001, 'mean_word_length': 2.5842155919153034, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 48, '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': '1672712', 'n_tokens_mistral': 991, 'n_tokens_neox': 972, 'n_words': 192}