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() |
|
|
|
|
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
|
|
|
} |