<commit_before><?php
/**
 * Created by PhpStorm.
 * User: shizhice
 * Date: 2017/8/26
 * Time: 下午1:41
 */

namespace Shizhice\Support;


class Validator
{
    private static $instance;
    protected $data = [];
    protected $rules = [];
    protected $regex = [];
    protected $errorMessages = [];
    protected $customMessages = [];
    protected static $customRuleValidate = [];

    /**
     * Validator constructor.
     * @param array $rules
     * @param array $message
     */
    public function __construct(array $rules = [], $message = [])
    {
        $this->customMessages = $message;
        $this->setRule($rules);
    }

    /**
     * 设置验证规则
     * @param $rules
     */
    private function setRule($rules)
    {
        $this->rules = array_merge_recursive(
            $this->rules, $this->explodeExplicitRule($rules)->rules
        );
    }

    /**
     * 分割验证规则
     * @param $rules
     * @return object
     */
    private function explodeExplicitRule($rules)
    {
        foreach ($rules as $key => &$rule) {
            if (is_array($rule)) {

            }elseif($rule instanceof \Closure){
                $rule = [$rule];
            }else{
                $rule = explode("|", $rule);
            }
        }
        return (object) [
            'rules' => $rules
        ];
    }

    /**
     * 实例化验证
     * @param array $rules
     * @param array $message
     * @return Validator
     */
    public static function make($rules = [], $message = [])
    {
        if (is_null(self::$instance)) {
            self::$instance = new self($rules, $message);
        }
        return self::$instance;
    }

    public function pass(array $data)
    {
        $this->data = $data;
        foreach ($this->rules as $attribute => $rules) {

            $value = isset($this->data[$attribute]) ? $this->data[$attribute] : '';
            // 存在验证规则required 或者 value 存在的时候进行验证
            if (!empty($value) || 0 === $value || in_array('require', $rules)) {
                foreach ($rules as $rule) {
                    if (! $this->checkItem($value, $rule)) {
                        $currentRule = explode(":", $rule)[0];
                        $msgKey = sprintf("%s.%s", $attribute, $currentRule);
                        $this->errorMessages[$attribute][$currentRule] = isset($this->customMessages[$msgKey])
                            ? $this->customMessages[$msgKey]
                            : sprintf("%s 验证失败", $msgKey);
                    }
                }
            }
        }

        return empty($this->errorMessages);
    }

    protected function checkItem($value, $rule)
    {
        if ($rule instanceof \Closure) {
            return $rule($value);
        }else {
            @list($rule, $args) = explode(":", $rule);
            $args and $args = explode(",", $args);

            if (method_exists($this, $rule.'Validate')) {
                return $this->{$rule.'Validate'}($value,  ...$args);
            }else{
                return $this->is($value, $rule);
            }
        }
    }

    protected function is($value, $rule)
    {
        switch ($rule) {
            case 'require':
                // 必须
                $result = !empty($value) || '0' == $value;
                break;
            case 'date':
                // 是否是一个有效日期
                $result = false !== strtotime($value);
                break;
            case 'alpha':
                // 只允许字母
                $result = $this->regex($value, '/^[A-Za-z]+$/');
                break;
            case 'alphaNum':
                // 只允许字母和数字
                $result = $this->regex($value, '/^[A-Za-z0-9]+$/');
                break;<commit_msg>Update Validator.php<commit_after><?php
/**
 * Created by PhpStorm.
 * User: shizhice
 * Date: 2017/8/26
 * Time: 下午1:41
 */

namespace Shizhice\Support;


class Validator
{
    private static $instance;
    protected $data = [];
    protected $rules = [];
    protected $regex = [];
    protected $errorMessages = [];
    protected $customMessages = [];
    protected static $customRuleValidate = [];

    /**
     * Validator constructor.
     * @param array $rules
     * @param array $message
     */
    public function __construct(array $rules = [], $message = [])
    {
        $this->customMessages = $message;
        $this->setRule($rules);
    }

    /**
     * 设置验证规则
     * @param $rules
     */
    private function setRule($rules)
    {
        $this->rules = array_merge_recursive(
            $this->rules, $this->explodeExplicitRule($rules)->rules
        );
    }

    /**
     * 分割验证规则
     * @param $rules
     * @return object
     */
    private function explodeExplicitRule($rules)
    {
        foreach ($rules as $key => &$rule) {
            if (is_array($rule)) {

            }elseif($rule instanceof \Closure){
                $rule = [$rule];
            }else{
                $rule = explode("|", $rule);
            }
        }
        return (object) [
            'rules' => $rules
        ];
    }

    /**
     * 实例化验证
     * @param array $rules
     * @param array $message
     * @return Validator
     */
    public static function make($rules = [], $message = [])
    {
        if (is_null(self::$instance)) {
            self::$instance = new self($rules, $message);
        }
        return self::$instance;
    }

    public function pass(array $data)
    {
        $this->data = $data;
        foreach ($this->rules as $attribute => $rules) {

            $value = isset($this->data[$attribute]) ? $this->data[$attribute] : '';
            // 存在验证规则required 或者 value 存在的时候进行验证
            if (!empty($value) || 0 === $value || in_array('require', $rules)) {
                foreach ($rules as $rule) {
                    if (! $this->checkItem($value, $rule)) {
                        $currentRule = explode(":", $rule)[0];
                        $msgKey = sprintf("%s.%s", $attribute, $currentRule);
                        $this->errorMessages[$attribute][$currentRule] = isset($this->customMessages[$msgKey])
                            ? $this->customMessages[$msgKey]
                            : sprintf("%s 验证失败", $msgKey);
                    }
                }
            }
        }

        return empty($this->errorMessages);
    }

    protected function checkItem($value, $rule)
    {
        if ($rule instanceof \Closure) {
            return $rule($value);
        }else {
            @list($rule, $args) = explode(":", $rule);
            $args and $args = explode(",", $args);

            if (method_exists($this, $rule.'Validate')) {
                return $this->{$rule.'Validate'}($value,  ...(array) $args);
            }else{
                return $this->is($value, $rule);
            }
        }
    }

    protected function is($value, $rule)
    {
        switch ($rule) {
            case 'require':
                // 必须
                $result = !empty($value) || '0' == $value;
                break;
            case 'date':
                // 是否是一个有效日期
                $result = false !== strtotime($value);
                break;
            case 'alpha':
                // 只允许字母
                $result = $this->regex($value, '/^[A-Za-z]+$/');
                break;
            case 'alphaNum':
                // 只允许字母和数字
                $result = $this->regex($value, '/^[A-Za-z0-9]+$/');
                break;
{'fraction_non_alphanumeric': 0.1583502366463827, 'fraction_numerical': 0.004056795131845842, 'mean_word_length': 1.49527665317139, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 47, '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': '5585861', 'n_tokens_mistral': 2222, 'n_tokens_neox': 2206, 'n_words': 435}