IntegerField::validateCastValue()   B
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 11
nc 4
nop 1
dl 0
loc 15
rs 8.8571
c 0
b 0
f 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