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

GeojsonField::validateCastValue()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 14
nc 9
nop 1
dl 0
loc 23
rs 8.5906
c 0
b 0
f 0
1
<?php
2
3
namespace frictionlessdata\tableschema\Fields;
4
5
class GeojsonField extends BaseField
6
{
7
    protected function validateCastValue($val)
8
    {
9
        if (is_string($val)) {
10
            try {
11
                $val = json_decode($val);
12
            } catch (\Exception $e) {
13
                throw $this->getValidationException($e->getMessage(), $val);
14
            }
15
        }
16
        if (!is_object($val)) {
17
            throw $this->getValidationException('must be an object', $val);
18
        }
19
        if ($this->format() == 'default') {
20
            try {
21
                // this validates the geojson
22
                \GeoJson\GeoJson::jsonUnserialize($val);
23
            } catch (\Exception $e) {
24
                throw $this->getValidationException($e->getMessage(), $val);
25
            }
26
        }
27
28
        return $val;
29
    }
30
31
    public static function type()
32
    {
33
        return 'geojson';
34
    }
35
}
36