<commit_before><?php

namespace iMega\Formatter;

class Formatter
{
    protected $fields = [];

    public function __construct(array $meta)
    {
        foreach ($meta as $item) {
            if (empty($item) || !is_array($item)) {
                throw new FormatterException('Wrong type');
            }
            list($name, $default, $type, $error) = $item;
            if (0 !== $error) {
                throw new FormatterException('Wrong field name');
            }
            if (get_parent_class($type) == GenericType::class) {
                $this->fields[$name] = [$default, $type];
            } else {
                throw new FormatterException('Wrong instance');
            }
        }
    }

    public function getData($name, $value)
    {
        $type = $this->getType($name);
        try {
            return $type::getData($value);
        } catch (FormatterException $e) {
            return $type::getData($this->getDefaultValue($name));
        }
    }

    public function getValue($name, $value)
    {
        $type = $this->getType($name);
        try {
            return $type::getValue($value);
        } catch (FormatterException $e) {
            return $this->getDefaultValue($name);
        }
    }

    public function getDataCollection(array $values)
    {
        $ret = [];
        foreach ($this->getFileds() as $name => $v) {
            if (array_key_exists($name, $values)) {
                $ret[$name] = $this->getData($name, $values[$name]);
            } else {
                $type = $this->getType($name);
                $ret[$name] = $type::getData($this->getDefaultValue($name));
            }
        }

        return $ret;
    }

    public function getValueCollection(array $values)
    {
        $ret = [];
        foreach ($this->getFileds() as $name => $v) {
            if (array_key_exists($name, $values)) {
                $ret[$name] = $this->getValue($name, $values[$name]);
            } else {
                $ret[$name] = $this->getDefaultValue($name);
            }
        }

        return $ret;
    }

    /**
     * @return array
     */
    public function getFileds()
    {
        return $this->fields;
    }

    /**
     * @param string $name<commit_msg>Change return getFileds<commit_after><?php

namespace iMega\Formatter;

class Formatter
{
    protected $fields = [];

    public function __construct(array $meta)
    {
        foreach ($meta as $item) {
            if (empty($item) || !is_array($item)) {
                throw new FormatterException('Wrong type');
            }
            list($name, $default, $type, $error) = $item;
            if (0 !== $error) {
                throw new FormatterException('Wrong field name');
            }
            if (get_parent_class($type) == GenericType::class) {
                $this->fields[$name] = [$default, $type];
            } else {
                throw new FormatterException('Wrong instance');
            }
        }
    }

    public function getData($name, $value)
    {
        $type = $this->getType($name);
        try {
            return $type::getData($value);
        } catch (FormatterException $e) {
            return $type::getData($this->getDefaultValue($name));
        }
    }

    public function getValue($name, $value)
    {
        $type = $this->getType($name);
        try {
            return $type::getValue($value);
        } catch (FormatterException $e) {
            return $this->getDefaultValue($name);
        }
    }

    public function getDataCollection(array $values)
    {
        $ret = [];
        foreach ($this->getFileds() as $name) {
            if (array_key_exists($name, $values)) {
                $ret[$name] = $this->getData($name, $values[$name]);
            } else {
                $type = $this->getType($name);
                $ret[$name] = $type::getData($this->getDefaultValue($name));
            }
        }

        return $ret;
    }

    public function getValueCollection(array $values)
    {
        $ret = [];
        foreach ($this->getFileds() as $name) {
            if (array_key_exists($name, $values)) {
                $ret[$name] = $this->getValue($name, $values[$name]);
            } else {
                $ret[$name] = $this->getDefaultValue($name);
            }
        }

        return $ret;
    }

    /**
     * @return array
     */
    public function getFileds()
    {
        return array_keys($this->fields);
    }

    /**
     * @param string $name
{'fraction_non_alphanumeric': 0.1512455516014235, 'fraction_numerical': 0.00044483985765124553, 'mean_word_length': 1.6976604679064187, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 31, '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': '3377871', 'n_tokens_mistral': 1278, 'n_tokens_neox': 1270, 'n_words': 255}