Completed
Push — master ( fbe049...cd378b )
by Ori
07:11
created

BooleanField::validateCastValue()   C

Complexity

Conditions 7
Paths 20

Size

Total Lines 26
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 20
nc 20
nop 1
dl 0
loc 26
rs 6.7272
c 0
b 0
f 0
1
<?php
2
3
namespace frictionlessdata\tableschema\Fields;
4
5
class BooleanField extends BaseField
6
{
7
    protected function validateCastValue($val)
8
    {
9
        if (isset($this->descriptor()->trueValues)) {
10
            $trueValues = $this->descriptor()->trueValues;
11
        } else {
12
            $trueValues = ['true', 'True', 'TRUE', '1'];
13
        }
14
        if (isset($this->descriptor()->falseValues)) {
15
            $falseValues = $this->descriptor()->falseValues;
16
        } else {
17
            $falseValues = ['false', 'False', 'FALSE', '0'];
18
        }
19
        if (is_bool($val)) {
20
            return $val;
21
        } elseif (is_string($val)) {
22
            if (in_array($val, $trueValues)) {
23
                return true;
24
            } elseif (in_array($val, $falseValues)) {
25
                return false;
26
            } else {
27
                throw $this->getValidationException('invalid bool value', $val);
28
            }
29
        } else {
30
            throw $this->getValidationException('value must be a bool or string', $val);
31
        }
32
    }
33
34
    public static function type()
35
    {
36
        return 'boolean';
37
    }
38
}
39