Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
100.00% |
1 / 1 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
28 / 28 |
Between | |
100.00% |
1 / 1 |
|
100.00% |
3 / 3 |
9 | |
100.00% |
28 / 28 |
__construct | |
100.00% |
1 / 1 |
1 | |
100.00% |
3 / 3 |
|||
validate | |
100.00% |
1 / 1 |
7 | |
100.00% |
20 / 20 |
|||
getInverseError | |
100.00% |
1 / 1 |
1 | |
100.00% |
5 / 5 |
<?php | |
namespace Verja\Validator; | |
use Verja\Error; | |
use Verja\Validator; | |
class Between extends Validator | |
{ | |
/** @var float|int */ | |
protected $min; | |
/** @var float|int */ | |
protected $max; | |
/** | |
* Between constructor. | |
* | |
* @param float|int $min | |
* @param float|int $max | |
*/ | |
public function __construct($min = null, $max = null) | |
{ | |
$this->min = $min; | |
$this->max = $max; | |
} | |
/** | |
* Validate $value | |
* | |
* @param mixed $value | |
* @param array $context | |
* @return bool | |
*/ | |
public function validate($value, array $context = []): bool | |
{ | |
if (!is_int($value) && !is_double($value)) { | |
$this->error = new Error('NO_NUMBER', $value, 'value should be a number'); | |
return false; | |
} | |
if (!is_null($this->min) && $this->min > $value) { | |
$this->error = new Error( | |
'TOO_SMALL', | |
$value, | |
sprintf('value should be at least %s', $this->min), | |
[ | |
'min' => $this->min, | |
'max' => $this->max, | |
] | |
); | |
return false; | |
} | |
if (!is_null($this->max) && $this->max < $value) { | |
$this->error = new Error( | |
'TOO_BIG', | |
$value, | |
sprintf('value should be maximal %s', $this->max), | |
[ | |
'min' => $this->min, | |
'max' => $this->max, | |
] | |
); | |
return false; | |
} | |
return true; | |
} | |
public function getInverseError($value) | |
{ | |
return new Error( | |
'BETWEEN', | |
$value, | |
sprintf('value should not be between %d and %d', $this->min, $this->max), | |
['min' => $this->min, 'max' => $this->max] | |
); | |
} | |
} |