Source of file BetweenRule.php
Size: 1,592 Bytes - Last Modified: 2017-11-08T13:54:24+00:00
/home/travis/build/bluzphp/framework/src/Validator/Rule/BetweenRule.php
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 | <?php /** * Bluz Framework Component * * @copyright Bluz PHP Team * @link https://github.com/bluzphp/framework */ declare(strict_types=1); namespace Bluz\Validator\Rule; use Bluz\Validator\Exception\ComponentException; /** * Check for value in range between minimum and maximum values * * @package Bluz\Validator\Rule */ class BetweenRule extends AbstractCompareRule { /** * @var mixed minimum value */ protected $minValue; /** * @var mixed maximum value */ protected $maxValue; /** * Setup validation rule * * @param mixed $min * @param mixed $max * * @throws \Bluz\Validator\Exception\ComponentException */ public function __construct($min, $max) { $this->minValue = $min; $this->maxValue = $max; if (is_null($min) || is_null($max)) { throw new ComponentException('Minimum and maximum is required'); } if ($min > $max) { throw new ComponentException(sprintf('%s cannot be less than %s for validation', $min, $max)); } } /** * Check input data * * @param NumericRule $input * * @return bool */ public function validate($input): bool { return $this->less($this->minValue, $input) && $this->less($input, $this->maxValue); } /** * Get error template * * @return string */ public function getDescription() : string { return __('must be between "%1" and "%2"', $this->minValue, $this->maxValue); } } |