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

ObjectField   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 10
c 0
b 0
f 0
wmc 6
lcom 0
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
B validateCastValue() 0 19 5
A type() 0 4 1
1
<?php
2
3
namespace frictionlessdata\tableschema\Fields;
4
5
/**
6
 * Class ObjectField
7
 * casts to php object.
8
 */
9
class ObjectField extends BaseField
10
{
11
    protected function validateCastValue($val)
12
    {
13
        try {
14
            if (is_string($val)) {
15
                $object = json_decode($val);
16
            } elseif (is_object($val)) {
17
                $object = json_decode(json_encode($val));
18
            } else {
19
                $object = null;
20
            }
21
        } catch (\Exception $e) {
22
            throw $this->getValidationException($e->getMessage(), $val);
23
        }
24
        if (is_object($object)) {
25
            return $object;
26
        } else {
27
            throw $this->getValidationException(null, $val);
28
        }
29
    }
30
31
    public static function type()
32
    {
33
        return 'object';
34
    }
35
}
36