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

ObjectField::validateCastValue()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 14
nc 9
nop 1
dl 0
loc 19
rs 8.8571
c 0
b 0
f 0
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