Completed
Push — master ( b10a48...29cde4 )
by Ori
05:27
created

BaseField   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 117
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 2
A descriptor() 0 4 1
A name() 0 4 1
A inferDescriptor() 0 8 2
A infer() 0 11 2
A inferProperties() 0 6 1
A validateValue() 0 8 1
A castValue() 0 4 1
A getInferIdentifier() 0 4 1
A type() 0 4 1
A getValidationException() 0 10 1
1
<?php
2
namespace frictionlessdata\tableschema\Fields;
3
4
use frictionlessdata\tableschema\Exceptions\FieldValidationException;
5
use frictionlessdata\tableschema\SchemaValidationError;
6
7
abstract class BaseField
8
{
9
    public function __construct($descriptor=null)
10
    {
11
        $this->descriptor = empty($descriptor) ? (object)[] : $descriptor;
12
    }
13
14
    public function descriptor()
15
    {
16
        return $this->descriptor;
17
    }
18
19
    public function name()
20
    {
21
        return $this->descriptor()->name;
22
    }
23
24
    /**
25
     * try to create a field object based on the descriptor
26
     * by default uses the type attribute
27
     * return the created field object or false if the descriptor does not match this field
28
     * @param object $descriptor
29
     * @return bool|BaseField
30
     */
31
    public static function inferDescriptor($descriptor)
32
    {
33
        if ($descriptor->type == static::type()) {
34
            return new static($descriptor);
35
        } else {
36
            return false;
37
        }
38
    }
39
40
    /**
41
     * try to create a new field object based on the given value
42
     * @param mixed $val
43
     * @param null|object $descriptor
44
     * @param bool @lenient
45
     * @return bool|BaseField
46
     */
47
    public static function infer($val, $descriptor=null, $lenient=false)
48
    {
49
        $field = new static($descriptor);
50
        try {
51
            $field->validateValue($val);
52
        } catch (FieldValidationException $e) {
53
            return false;
54
        }
55
        $field->inferProperties($val, $lenient);
56
        return $field;
57
    }
58
59
    public function inferProperties($val, $lenient)
60
    {
61
        // should be implemented by extending classes
62
        // allows adding / modfiying descriptor properties based on the given value
63
        $this->descriptor->type = $this->type();
64
    }
65
66
    /**
67
     * @param mixed $val
68
     * @return mixed
69
     * @throws \frictionlessdata\tableschema\Exceptions\FieldValidationException;
70
     */
71
    public function validateValue($val)
72
    {
73
        // extending classes should raise FieldValidationException on any errors here
74
        // can use getValidationException function to get a simple exception with single validation error message
75
        // you can also throw an exception with multiple validation errors manually
76
        // must make sure all validation is done in this function and ensure castValue doesn't raise any errors
77
        return $val;
78
    }
79
80
    /**
81
     * @param mixed $val
82
     * @return mixed
83
     * @throws \frictionlessdata\tableschema\Exceptions\FieldValidationException;
84
     */
85
    public function castValue($val)
86
    {
87
        return $this->validateValue($val);
88
    }
89
90
    /**
91
     * get a unique identifier for this field
92
     * used in the inferring process
93
     * this is usually the type, but can be modified to support more advanced inferring process
94
     * @param bool @lenient
95
     * @return string
96
     */
97
    public function getInferIdentifier($lenient=false)
98
    {
99
        return $this->type();
100
    }
101
102
    /**
103
     * should be implemented by extending classes to return the table schema type of this field
104
     * @return string
105
     */
106
    static public function type()
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
107
    {
108
        throw new \Exception("must be implemented by extending classes");
109
    }
110
111
    protected $descriptor;
112
113
    protected function getValidationException($errorMsg, $val=null)
114
    {
115
        return new FieldValidationException([
116
            new SchemaValidationError(SchemaValidationError::FIELD_VALIDATION, [
117
                "field" => $this->name(),
118
                "value" => $val,
119
                "error" => $errorMsg
120
            ])
121
        ]);
122
    }
123
}