<commit_before><?php

class Form
{
    public $values = array();
    public $errors = array();
    public $fields = array();

    public function __construct(array $values, $fields = null)
    {
        $this->init();
        if ($fields) $this->fields = $fields;
        $this->values = array_intersect_key($values, $this->fields);

        if ($values) {
            foreach ($this->fields as $name => $callback) {
                try {
                    call_user_func($callback, $this->get($values, $name));
                } catch (InvalidArgumentException $e) {
                    $this->errors[$name] = $e->getMessage();
                }
            }
        }
    }

    protected function init()
    {
        // initialize $fields when subclassing
    }

    public static function check($expression, $message)
    {
        if (false == $expression) {
            throw new InvalidArgumentException($message);
        }
    }

    public function __get($name)
    {
        return $this->get($this->values, $name);
    }

    public function error($name, $message = null)
    {
        $value = $this->get($this->errors, $name);
        return $message && $value ? $message : $value;
    }

    private function get(array $array, $key, $default = null)
    {
        return isset($array[$key]) ? $array[$key] : $default;
    }<commit_msg>Refactor to use DomainException<commit_after><?php

class Form
{
    public $values = array();
    public $errors = array();
    public $fields = array();

    public function __construct(array $values, $fields = null)
    {
        $this->init();
        if ($fields) $this->fields = $fields;
        $this->values = array_intersect_key($values, $this->fields);

        if ($values) {
            foreach ($this->fields as $name => $callback) {
                try {
                    call_user_func($callback, $this->get($values, $name));
                } catch (DomainException $e) {
                    $this->errors[$name] = $e->getMessage();
                }
            }
        }
    }

    protected function init()
    {
        // initialize $fields when subclassing
    }

    public static function check($expression, $message)
    {
        if (false == $expression) {
            throw new DomainException($message);
        }
    }

    public function __get($name)
    {
        return $this->get($this->values, $name);
    }

    public function error($name, $message = null)
    {
        $value = $this->get($this->errors, $name);
        return $message && $value ? $message : $value;
    }

    private function get(array $array, $key, $default = null)
    {
        return isset($array[$key]) ? $array[$key] : $default;
    }
{'fraction_non_alphanumeric': 0.14771048744460857, 'fraction_numerical': 0.0, 'mean_word_length': 1.9574235807860263, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 29, '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': '3117941', 'n_tokens_mistral': 774, 'n_tokens_neox': 748, 'n_words': 183}