IntegerField::type()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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