NumberField   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
D validateCastValue() 0 29 10
A type() 0 4 1
1
<?php
2
3
namespace frictionlessdata\tableschema\Fields;
4
5
class NumberField extends BaseField
6
{
7
    /**
8
     * @param mixed $val
9
     *
10
     * @return float
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
        }
19
        $isPercent = false;
20
        if (is_string($val)) {
21
            if (substr($val, -1) == '%') {
22
                $val = rtrim($val, '%');
23
                $isPercent = true;
24
            }
25
            if (isset($this->descriptor()->groupChar)) {
26
                $val = str_replace($this->descriptor()->groupChar, '', $val);
27
            }
28
            if (isset($this->descriptor()->decimalChar) && $this->descriptor()->decimalChar != '.') {
29
                $val = str_replace($this->descriptor()->decimalChar, '.', $val);
30
            }
31
        }
32
        if (!is_numeric($val)) {
33
            throw $this->getValidationException('value must be numeric', $val);
34
        } else {
35
            $val = (float) $val;
36
            if ($isPercent) {
37
                $val = $val / 100;
38
            }
39
40
            return $val;
41
        }
42
    }
43
44
    public static function type()
45
    {
46
        return 'number';
47
    }
48
}
49