Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
100.00% |
1 / 1 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
10 / 10 |
Numeric | |
100.00% |
1 / 1 |
|
100.00% |
3 / 3 |
8 | |
100.00% |
10 / 10 |
__construct | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
validate | |
100.00% |
1 / 1 |
6 | |
100.00% |
7 / 7 |
|||
getInverseError | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
<?php | |
namespace Verja\Validator; | |
use Verja\Error; | |
use Verja\Validator; | |
class Numeric extends Validator | |
{ | |
/** @var string */ | |
protected $decimalPoint; | |
/** | |
* Numeric constructor. | |
* | |
* @param string $decimalPoint | |
*/ | |
public function __construct(string $decimalPoint = '.') | |
{ | |
$this->decimalPoint = $decimalPoint; | |
} | |
/** | |
* Validate $value | |
* | |
* @param mixed $value | |
* @param array $context | |
* @return bool | |
*/ | |
public function validate($value, array $context = []): bool | |
{ | |
if ($this->decimalPoint !== '.' && is_string($value)) { | |
$value = str_replace('.', '', $value); | |
$value = str_replace($this->decimalPoint, '.', $value); | |
} | |
if (!is_int($value) && !is_double($value) && !is_numeric($value)) { | |
$this->error = new Error('NOT_NUMERIC', $value, 'value should be numeric'); | |
return false; | |
} | |
return true; | |
} | |
public function getInverseError($value) | |
{ | |
return new Error('IS_NUMERIC', $value, 'value should not be numeric'); | |
} | |
} |