Completed
Push — master ( 29cde4...77c4c6 )
by Ori
03:02
created

IntegerField::validateValue()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 3
eloc 9
nc 3
nop 1
1
<?php
2
namespace frictionlessdata\tableschema\Fields;
3
4
class IntegerField extends BaseField
5
{
6
    /**
7
     * @param mixed $val
8
     * @return integer
9
     * @throws \frictionlessdata\tableschema\Exceptions\FieldValidationException;
10
     */
11
    public function validateCastValue($val)
12
    {
13
        $val = parent::validateCastValue($val);
14
        if (!is_numeric($val)) {
15
            throw $this->getValidationException("value must be numeric", $val);
16
        } else {
17
            $intVal = (integer)$val;
18
            if ($intVal != (float)$val) {
19
                throw $this->getValidationException("value must be an integer", $val);
20
            } else {
21
                return $intVal;
22
            }
23
        }
24
    }
25
26
    public static function type()
27
    {
28
        return "integer";
29
    }
30
31
    protected function isEmptyValue($val)
32
    {
33
        return (!is_numeric($val) && empty($val));
34
    }
35
}