[李景山php]每天laravel-20161007|Validator.php-7

来源:互联网 发布:2016年nba西部决赛数据 编辑:程序博客网 时间:2024/06/05 14:42
    /**     * Validate that an attribute is an array.     *     * @param  string  $attribute     * @param  mixed   $value     * @return bool     */    protected function validateArray($attribute, $value)    {// function name is validate the array        if (! $this->hasAttribute($attribute)) {            return true;        }// if it doesn't has this attribute        return is_null($value) || is_array($value);// is_null and is_array    }// validate that an attribute is an array    /**     * Validate that an attribute is a boolean.     *     * @param  string  $attribute     * @param  mixed   $value     * @return bool     */    protected function validateBoolean($attribute, $value)    {        if (! $this->hasAttribute($attribute)) {            return true;        }// has attribute        $acceptable = [true, false, 0, 1, '0', '1'];// can be acceptable options        return is_null($value) || in_array($value, $acceptable, true);// is_null and it is array    }    /**     * Validate that an attribute is an integer.     *     * @param  string  $attribute     * @param  mixed   $value     * @return bool     */    protected function validateInteger($attribute, $value)    {        if (! $this->hasAttribute($attribute)) {            return true;        }//ditto        return is_null($value) || filter_var($value, FILTER_VALIDATE_INT) !== false;    }// has a key like this    /**     * Validate that an attribute is numeric.     *     * @param  string  $attribute     * @param  mixed   $value     * @return bool     */    protected function validateNumeric($attribute, $value)    {        if (! $this->hasAttribute($attribute)) {            return true;        }//just a number not only a int or float        return is_null($value) || is_numeric($value);    }// validate Numeric    /**     * Validate that an attribute is a string.     *     * @param  string  $attribute     * @param  mixed   $value     * @return bool     */    protected function validateString($attribute, $value)    {        if (! $this->hasAttribute($attribute)) {            return true;        }// determine this attribute        return is_null($value) || is_string($value);    }// back value    /**     * Validate the attribute is a valid JSON string.     *     * @param  string  $attribute     * @param  mixed   $value     * @return bool     */    protected function validateJson($attribute, $value)    {        if (! is_scalar($value) && ! method_exists($value, '__toString')) {            return false;        }// scalar means it is a stander value ,just like integer float string boolean        // other like array object resource        // if this value is not a scalar and it is can't be change to string        json_decode($value);// use this user function to change it.        return json_last_error() === JSON_ERROR_NONE;// then return this result    }    /**     * Validate that an attribute has a given number of digits.     *     * @param  string  $attribute     * @param  mixed   $value     * @param  array   $parameters     * @return bool     */    protected function validateDigits($attribute, $value, $parameters)    {        $this->requireParameterCount(1, $parameters, 'digits');// i hate this user method        return $this->validateNumeric($attribute, $value)            && strlen((string) $value) == $parameters[0];// just validate the attribute and this value    }//Validate that an attribute has a given number of digits.    /**     * Validate that an attribute is between a given number of digits.     *     * @param  string  $attribute     * @param  mixed   $value     * @param  array   $parameters     * @return bool     */    protected function validateDigitsBetween($attribute, $value, $parameters)    {        $this->requireParameterCount(2, $parameters, 'digits_between');        // this function has three parameter Count        $length = strlen((string) $value);// get string length        return $this->validateNumeric($attribute, $value)          && $length >= $parameters[0] && $length <= $parameters[1];// a log    }// Validate that an attribute is between a given number of digits    /**     * Validate the size of an attribute.     *     * @param  string  $attribute     * @param  mixed   $value     * @param  array   $parameters     * @return bool     */    protected function validateSize($attribute, $value, $parameters)    {        $this->requireParameterCount(1, $parameters, 'size');        return $this->getSize($attribute, $value) == $parameters[0];    }//validate the size of an attribute.    /**     * Validate the size of an attribute is between a set of values.     *     * @param  string  $attribute     * @param  mixed   $value     * @param  array   $parameters     * @return bool     */    protected function validateBetween($attribute, $value, $parameters)    {        $this->requireParameterCount(2, $parameters, 'between');        $size = $this->getSize($attribute, $value);//get size        return $size >= $parameters[0] && $size <= $parameters[1];// just return this judge result    }// validate Between    /**     * Validate the size of an attribute is greater than a minimum value.     *     * @param  string  $attribute     * @param  mixed   $value     * @param  array   $parameters     * @return bool     */    protected function validateMin($attribute, $value, $parameters)    {        $this->requireParameterCount(1, $parameters, 'min');// get this Parameter Count        return $this->getSize($attribute, $value) >= $parameters[0];//get Size    }// validate the size of an attribute is greater than a minimum value.    /**     * Validate the size of an attribute is less than a maximum value.     *     * @param  string  $attribute     * @param  mixed   $value     * @param  array   $parameters     * @return bool     */    protected function validateMax($attribute, $value, $parameters)    {        $this->requireParameterCount(1, $parameters, 'max');        if ($value instanceof UploadedFile && ! $value->isValid()) {            return false;        }        return $this->getSize($attribute, $value) <= $parameters[0];    }//validate the size of an attribute is less than a maximum value.    /**     * Get the size of an attribute.     *     * @param  string  $attribute     * @param  mixed   $value     * @return mixed     */    protected function getSize($attribute, $value)    {        $hasNumeric = $this->hasRule($attribute, $this->numericRules);// has Numeric        // This method will determine if the attribute is a number, string, or file and        // return the proper size accordingly. If it is a number, then number itself        // is the size. If it is a file, we take kilobytes, and for a string the        // entire length of the string will be considered the attribute size.        if (is_numeric($value) && $hasNumeric) {            return $value;// numeric just return it is self        } elseif (is_array($value)) {            return count($value);// get count        } elseif ($value instanceof File) {            return $value->getSize() / 1024;// get Size        }        return mb_strlen($value);// if it is mb_strlen    }
0 0
原创粉丝点击