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

GeojsonField::type()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
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