Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
Numeric
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
3 / 3
8
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 validate
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
6
 getInverseError
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace Verja\Validator;
4
5use Verja\Error;
6use Verja\Validator;
7
8class Numeric extends Validator
9{
10    /** @var string */
11    protected $decimalPoint;
12
13    /**
14     * Numeric constructor.
15     *
16     * @param string $decimalPoint
17     */
18    public function __construct(string $decimalPoint = '.')
19    {
20        $this->decimalPoint = $decimalPoint;
21    }
22
23    /**
24     * Validate $value
25     *
26     * @param mixed $value
27     * @param array $context
28     * @return bool
29     */
30    public function validate($value, array $context = []): bool
31    {
32        if ($this->decimalPoint !== '.' && is_string($value)) {
33            $value = str_replace('.', '', $value);
34            $value = str_replace($this->decimalPoint, '.', $value);
35        }
36
37        if (!is_int($value) && !is_double($value) && !is_numeric($value)) {
38            $this->error = new Error('NOT_NUMERIC', $value, 'value should be numeric');
39            return false;
40        }
41
42        return true;
43    }
44
45    public function getInverseError($value)
46    {
47        return new Error('IS_NUMERIC', $value, 'value should not be numeric');
48    }
49}