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

GeojsonField   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 10
c 0
b 0
f 0
wmc 7
lcom 0
cbo 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
B validateCastValue() 0 23 6
A type() 0 4 1
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