Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
6 / 6 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
Numeric | |
100.00% |
6 / 6 |
|
100.00% |
2 / 2 |
5 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
filter | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
4 |
1 | <?php |
2 | |
3 | namespace Verja\Filter; |
4 | |
5 | use Verja\Filter; |
6 | use Verja\Gate; |
7 | use Verja\Validator; |
8 | |
9 | class Numeric extends Filter |
10 | { |
11 | /** @var string */ |
12 | protected $decimalPoint; |
13 | |
14 | /** |
15 | * Numeric constructor. |
16 | * |
17 | * @param string $decimalPoint |
18 | */ |
19 | public function __construct(string $decimalPoint = '.') |
20 | { |
21 | $this->decimalPoint = $decimalPoint; |
22 | } |
23 | |
24 | /** |
25 | * Filter $value |
26 | * |
27 | * @param mixed $value |
28 | * @param array $context |
29 | * @return mixed |
30 | */ |
31 | public function filter($value, array $context = []) |
32 | { |
33 | Gate::assert(new Validator\Numeric($this->decimalPoint), $value); |
34 | |
35 | if ($this->decimalPoint !== '.' && is_string($value)) { |
36 | $value = str_replace('.', '', $value); |
37 | $value = str_replace($this->decimalPoint, '.', $value); |
38 | } |
39 | |
40 | return (double)$value === round((double)$value) ? (int)((double)$value) : (double)$value; |
41 | } |
42 | } |