为了账号安全,请及时绑定邮箱和手机立即绑定

Validator.php-7

标签:
PHP

/**

 * 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

}


点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消