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

ObjectField::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
/**
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