<commit_before><?php

namespace Mollie\API\Model\Base;

use Mollie\API\Mollie;

/**
 * Model base
 */
abstract class ModelBase
{
    /** @var object */
    protected $response;

    /** @var Mollie */
    protected $api;

    /** @var string Resource type */
    public $resource;

    /**
     * Constructor
     *
     * @param Mollie $api
     * @param object|array $data
     */
    public function __construct(Mollie $api, $data)
    {
        // Fill model
        $this->fill($data);

        // API reference
        $this->api = $api;
    }

    /**
     * Fill model with data
     *
     * @param array|object|\Traversable $data
     * @throws InvalidArgumentException
     */
    protected function fill($data)
    {
        // Check data type
        if (!is_object($data) && !is_array($data) && !$data instanceof \Traversable) {
            throw new \InvalidArgumentException("Model data should be an array, object or traversable object.");
        }

        // Fill model
        foreach ($data as $k => $v) {
            if (property_exists($this, $k)) {
                $this->$k = $this->parseData($k, $v);
            }
        }

        // Set raw response data and normalize to object
        $this->response = is_object($data) ? $data : json_decode(json_encode($data, JSON_FORCE_OBJECT), false);
    }

    /**
     * Parse data and convert it's value when needed e.g. parse dates into their respective objects
     *
     * @param string $name Variable name
     * @param mixed $value
     * @return mixed
     */
    protected function parseData($name, $value)
    {
        if (!empty($value)) {
            // ISO 8601 Date
            if (preg_match('/.+(Datetime|Date)$/', $name)) {
                try {
                    return new \DateTime($value);
                } catch (\Exception $ex) {
                    throw new \InvalidArgumentException("Property {$name} is not a valid date/time string: {$value}.");
                }
            }

            // ISO 8601 Duration
            if (preg_match('/.+(Period)$/', $name)) {
                // Remove trailing T returned by API in test mode
                $value = rtrim($value, 'T');

                try {
                    return new \DateInterval($value);
                } catch (\Exception $ex) {
                    throw new \InvalidArgumentException("Property {$name} is not a valid ISO 8601 duration string: {$value}.");
                }
            }
        }

        // Metadata
        if ($name == "metadata" && !is_object($value) && !is_array($value)) {
            throw new \InvalidArgumentException("Property {$name} is not an object or array.");
        }

        // Amount
        if (preg_match('/amount.*$/', $name) && isset($value)) {
            return $value + 0.0;
        }

        return $value;
    }

    /**
     * Get JSON response object
     * @return object
     */
    public function getResponse()
    {
        return $this->response;
    }
}<commit_msg>Remove rtrim workaround for trailing T in test response<commit_after><?php

namespace Mollie\API\Model\Base;

use Mollie\API\Mollie;

/**
 * Model base
 */
abstract class ModelBase
{
    /** @var object */
    protected $response;

    /** @var Mollie */
    protected $api;

    /** @var string Resource type */
    public $resource;

    /**
     * Constructor
     *
     * @param Mollie $api
     * @param object|array $data
     */
    public function __construct(Mollie $api, $data)
    {
        // Fill model
        $this->fill($data);

        // API reference
        $this->api = $api;
    }

    /**
     * Fill model with data
     *
     * @param array|object|\Traversable $data
     * @throws InvalidArgumentException
     */
    protected function fill($data)
    {
        // Check data type
        if (!is_object($data) && !is_array($data) && !$data instanceof \Traversable) {
            throw new \InvalidArgumentException("Model data should be an array, object or traversable object.");
        }

        // Fill model
        foreach ($data as $k => $v) {
            if (property_exists($this, $k)) {
                $this->$k = $this->parseData($k, $v);
            }
        }

        // Set raw response data and normalize to object
        $this->response = is_object($data) ? $data : json_decode(json_encode($data, JSON_FORCE_OBJECT), false);
    }

    /**
     * Parse data and convert it's value when needed e.g. parse dates into their respective objects
     *
     * @param string $name Variable name
     * @param mixed $value
     * @return mixed
     */
    protected function parseData($name, $value)
    {
        if (!empty($value)) {
            // ISO 8601 Date
            if (preg_match('/.+(Datetime|Date)$/', $name)) {
                try {
                    return new \DateTime($value);
                } catch (\Exception $ex) {
                    throw new \InvalidArgumentException("Property {$name} is not a valid date/time string: {$value}.");
                }
            }

            // ISO 8601 Duration
            if (preg_match('/.+(Period)$/', $name)) {
                try {
                    return new \DateInterval($value);
                } catch (\Exception $ex) {
                    throw new \InvalidArgumentException("Property {$name} is not a valid ISO 8601 duration string: {$value}.");
                }
            }
        }

        // Metadata
        if ($name == "metadata" && !is_object($value) && !is_array($value)) {
            throw new \InvalidArgumentException("Property {$name} is not an object or array.");
        }

        // Amount
        if (preg_match('/amount.*$/', $name) && isset($value)) {
            return $value + 0.0;
        }

        return $value;
    }

    /**
     * Get JSON response object
     * @return object
     */
    public function getResponse()
    {
        return $this->response;
    }
}
{'fraction_non_alphanumeric': 0.1333220682663062, 'fraction_numerical': 0.004731328151402501, 'mean_word_length': 1.6662162162162162, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, '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': '2461883', 'n_tokens_mistral': 1695, 'n_tokens_neox': 1605, 'n_words': 462}