Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
100.00% |
1 / 1 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
7 / 7 |
Numeric | |
100.00% |
1 / 1 |
|
100.00% |
2 / 2 |
5 | |
100.00% |
7 / 7 |
__construct | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
filter | |
100.00% |
1 / 1 |
4 | |
100.00% |
5 / 5 |
<?php | |
namespace Verja\Filter; | |
use Verja\Filter; | |
use Verja\Gate; | |
use Verja\Validator; | |
class Numeric extends Filter | |
{ | |
/** @var string */ | |
protected $decimalPoint; | |
/** | |
* Numeric constructor. | |
* | |
* @param string $decimalPoint | |
*/ | |
public function __construct(string $decimalPoint = '.') | |
{ | |
$this->decimalPoint = $decimalPoint; | |
} | |
/** | |
* Filter $value | |
* | |
* @param mixed $value | |
* @param array $context | |
* @return mixed | |
*/ | |
public function filter($value, array $context = []) | |
{ | |
Gate::assert(new Validator\Numeric($this->decimalPoint), $value); | |
if ($this->decimalPoint !== '.' && is_string($value)) { | |
$value = str_replace('.', '', $value); | |
$value = str_replace($this->decimalPoint, '.', $value); | |
} | |
return (double)$value === round((double)$value) ? (int)((double)$value) : (double)$value; | |
} | |
} |