GeojsonField::validateCastValue()   C
last analyzed

Complexity

Conditions 7
Paths 10

Size

Total Lines 27
Code Lines 17

Duplication

Lines 10
Ratio 37.04 %

Importance

Changes 0
Metric Value
cc 7
eloc 17
nc 10
nop 1
dl 10
loc 27
rs 6.7272
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 View Code Duplication
        if (is_string($val)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
10
            try {
11
                $val = json_decode($val);
12
            } catch (\Exception $e) {
13
                throw $this->getValidationException($e->getMessage(), $val);
14
            }
15
            if (!$val) {
16
                throw $this->getValidationException('failed to decode json', $val);
17
            }
18
        }
19
        $val = json_decode(json_encode($val));
20
        if (!is_object($val)) {
21
            throw $this->getValidationException('must be an object', $val);
22
        }
23
        if ($this->format() == 'default') {
24
            try {
25
                // this validates the geojson
26
                \GeoJson\GeoJson::jsonUnserialize($val);
27
            } catch (\Exception $e) {
28
                throw $this->getValidationException($e->getMessage(), $val);
29
            }
30
        }
31
32
        return $val;
33
    }
34
35
    public static function type()
36
    {
37
        return 'geojson';
38
    }
39
}
40