<commit_before><?php

/**
 * Validator
 * @author <NAME> <<EMAIL>>
 */

namespace Neptune\Validate;

use Neptune\Validate\Result;
use Neptune\Validate\Rule\AbstractRule;
use Neptune\Validate\Rule\Required;

class Validator {

	protected $rules = array();
    protected $required = array();

    public function check($name, AbstractRule $rule)
    {
        if (!isset($this->rules[$name])) {
            $this->rules[$name] = array();
        }

        $this->rules[$name][] = $rule;

        if ($rule instanceof Required) {
            $this->required[$name] = true;
        }
        return $this;
    }

    protected function doValidate(Result $result, array $input, $name, $rules, $early_exit = false)
    {
        //first check if the value actually exists.
        if (!isset($input[$name])) {
            $result->addError($name, $name . ' is not in input.');
            return $result;
        }
        //now run the rules for this name
        foreach ($rules as $rule) {
            if (!$rule->validate($result, $name, $input[$name], $input) && $early_exit) {
                return $result;
            }
        }
        return $result;
    }

    /**
     * Validate an array of values.
     */
    public function validate(array $input, $early_exit = false) {
        $result = new Result($input);
        //add custom messages that have been supplied
        foreach($this->rules as $name => $rules) {
            $this->doValidate($result, $input, $name, $rules, $early_exit);
        }
        return $result;
    }

    /**
     * Validate an array of values from a form. Unlike validate(), if
     * a submitted value is empty, the rules for that value will not
     * be applied unless the value has the 'required' rule. This
     * allows validation on optional fields, but only if they are
     * present.
     */
    public function validateForm(array $input, $early_exit = false)
    {
        $result = new Result($input);
        foreach ($this->rules as $name => $rules) {
            //if a value is submitted but empty, and not required, skip it
            if(!in_array($name, $this->required)) {
                if(!$this->required($input[$name])) {
                    continue;
                }
            }
            $this->doValidate($result, $input, $name, $rules, $early_exit);
        }
        return $result;
    }

	protected function required($value) {
        if (is_string($value)) {
            $value = trim($value);
        }
        if (empty($value) && !is_numeric($value)) {
            return false;
        }
        return true;
	}

}<commit_msg>Cleaning up Validator a bit.<commit_after><?php

/**
 * Validator
 * @author <NAME> <<EMAIL>>
 */

namespace Neptune\Validate;

use Neptune\Validate\Rule\AbstractRule;
use Neptune\Validate\Rule\Required;

class Validator
{
    protected $rules = array();
    protected $required = array();

    public function check($name, AbstractRule $rule)
    {
        if (!isset($this->rules[$name])) {
            $this->rules[$name] = array();
        }

        $this->rules[$name][] = $rule;

        if ($rule instanceof Required) {
            $this->required[$name] = true;
        }

        return $this;
    }

    protected function doValidate(Result $result, array $input, $name, $rules, $early_exit = false)
    {
        //first check if the value actually exists.
        if (!isset($input[$name])) {
            $result->addError($name, $name . ' is not in input.');

            return $result;
        }
        //now run the rules for this name
        foreach ($rules as $rule) {
            if (!$rule->validate($result, $name, $input[$name], $input) && $early_exit) {
                return $result;
            }
        }

        return $result;
    }

    /**
     * Validate an array of values.
     */
    public function validate(array $input, $early_exit = false)
    {
        $result = new Result($input);
        //add custom messages that have been supplied
        foreach ($this->rules as $name => $rules) {
            $this->doValidate($result, $input, $name, $rules, $early_exit);
        }

        return $result;
    }

    /**
     * Validate an array of values from a form. Unlike validate(), if
     * a submitted value is empty, the rules for that value will not
     * be applied unless the value has the 'required' rule. This
     * allows validation on optional fields, but only if they are
     * present.
     */
    public function validateForm(array $input, $early_exit = false)
    {
        $result = new Result($input);
        foreach ($this->rules as $name => $rules) {
            //if a value is submitted but empty, and not required, skip it
            if (!in_array($name, $this->required)) {
                if (!$this->required($input[$name])) {
                    continue;
                }
            }
            $this->doValidate($result, $input, $name, $rules, $early_exit);
        }

        return $result;
    }

    protected function required($value)
    {
        if (is_string($value)) {
            $value = trim($value);
        }
        if (empty($value) && !is_numeric($value)) {
            return false;
        }

        return true;
    }

}
{'fraction_non_alphanumeric': 0.135243553008596, 'fraction_numerical': 0.0, 'mean_word_length': 1.9432265317594155, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 37, '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': '228663', 'n_tokens_mistral': 1484, 'n_tokens_neox': 1465, 'n_words': 435}