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

IntegerField   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 0
cbo 2
dl 0
loc 32
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A validateCastValue() 0 14 3
A type() 0 4 1
A isEmptyValue() 0 4 2
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
}