<commit_before><?php

namespace CQRS\Serializer\Helper;

use Ramsey\Uuid\Uuid;
use ReflectionParameter;

final class ParamDeserializationHelper
{
    /**
     * @param array $data
     * @param ReflectionParameter $parameter
     * @return mixed
     */
    public function deserializeParam(array $data, ReflectionParameter $parameter)
    {
        $value = $this->getParamValue($data, $parameter);

        $class = $parameter->getClass();
        if (!$class || ($value === null && $parameter->allowsNull())) {
            return $value;
        }

        return $this->deserializeValue($value, $class->getName());
    }

    /**
     * @param array $data
     * @param ReflectionParameter $parameter
     * @return mixed
     */
    private function getParamValue(array $data, ReflectionParameter $parameter)
    {
        $key = $parameter->getName();
        $value = $this->getValue($data, $key);

        // BC for events serialized with generically named aggregate ID field
        if ($value === null
            && $key !== 'aggregateId'
            && $parameter->getPosition() === 0
            && !$parameter->allowsNull()
        ) {
            $value = $this->getValue($data, 'aggregateId');
        }

        return $value;
    }

    /**
     * @param array $data
     * @param string $key
     * @return mixed
     */
    private function getValue(array $data, $key)
    {
        if (isset($data[$key])) {
            return $data[$key];
        }

        if (isset($data[$this->camelCaseToUnderscore($key)])) {
            return $data[$this->camelCaseToUnderscore($key)];
        }

        return null;
    }

    private function camelCaseToUnderscore($key)
    {
        $string = preg_replace('/(?<=\\w)(?=[A-Z])/', "_$1", $key);
        return strtolower($string);
    }

    /**
     * @param mixed $value
     * @param string $type
     * @return mixed
     */
    private function deserializeValue($value, $type)
    {
        if (method_exists($type, 'jsonDeserialize')) {
            return $type::jsonDeserialize($value, $this);
        }

        if (is_string($value)) {
            if (method_exists($type, 'fromUuid')) {
                return $type::fromUuid(Uuid::fromString($value));
            } elseif (method_exists($type, 'fromString')) {
                return $type::fromString($value);
            }
        } elseif (is_int($value)) {
            if (method_exists($type, 'fromInteger')) {
                return $type::fromInteger($value);
            }

            if (method_exists($type, 'fromInt')) {
                return $type::fromInt($value);
            }
        } elseif (null === $value && method_exists($type, 'unknown')) {
            return $type::unknown();
        }

        return new $type($value);
    }
}
<commit_msg>add desrialize uuid param type fromUuid()<commit_after><?php

namespace CQRS\Serializer\Helper;

use Ramsey\Uuid\Uuid;
use ReflectionParameter;

final class ParamDeserializationHelper
{
    /**
     * @param array $data
     * @param ReflectionParameter $parameter
     * @return mixed
     */
    public function deserializeParam(array $data, ReflectionParameter $parameter)
    {
        $value = $this->getParamValue($data, $parameter);

        $class = $parameter->getClass();
        if (!$class || ($value === null && $parameter->allowsNull())) {
            return $value;
        }

        return $this->deserializeValue($value, $class->getName());
    }

    /**
     * @param array $data
     * @param ReflectionParameter $parameter
     * @return mixed
     */
    private function getParamValue(array $data, ReflectionParameter $parameter)
    {
        $key = $parameter->getName();
        $value = $this->getValue($data, $key);

        // BC for events serialized with generically named aggregate ID field
        if ($value === null
            && $key !== 'aggregateId'
            && $parameter->getPosition() === 0
            && !$parameter->allowsNull()
        ) {
            $value = $this->getValue($data, 'aggregateId');
        }

        return $value;
    }

    /**
     * @param array $data
     * @param string $key
     * @return mixed
     */
    private function getValue(array $data, $key)
    {
        if (isset($data[$key])) {
            return $data[$key];
        }

        if (isset($data[$this->camelCaseToUnderscore($key)])) {
            return $data[$this->camelCaseToUnderscore($key)];
        }

        return null;
    }

    private function camelCaseToUnderscore($key)
    {
        $string = preg_replace('/(?<=\\w)(?=[A-Z])/', "_$1", $key);
        return strtolower($string);
    }

    /**
     * @param mixed $value
     * @param string $type
     * @return mixed
     */
    private function deserializeValue($value, $type)
    {
        if (method_exists($type, 'jsonDeserialize')) {
            return $type::jsonDeserialize($value, $this);
        }

        if (is_string($value)) {
            if (method_exists($type, 'fromUuid')) {
                return $type::fromUuid(Uuid::fromString($value));
            }

            if (method_exists($type, 'fromString')) {
                return $type::fromString($value);
            }
        } elseif (is_int($value)) {
            if (method_exists($type, 'fromInteger')) {
                return $type::fromInteger($value);
            }

            if (method_exists($type, 'fromInt')) {
                return $type::fromInt($value);
            }
        } elseif (null === $value && method_exists($type, 'unknown')) {
            return $type::unknown();
        }

        return new $type($value);
    }
}

{'fraction_non_alphanumeric': 0.1528345477163675, 'fraction_numerical': 0.0007108583614714769, 'mean_word_length': 2.0703764320785596, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 27, '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': '7553372', 'n_tokens_mistral': 1622, 'n_tokens_neox': 1588, 'n_words': 335}