1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace frictionlessdata\tableschema\Fields; |
4
|
|
|
|
5
|
|
|
use frictionlessdata\tableschema\Exceptions\FieldValidationException; |
6
|
|
|
use frictionlessdata\tableschema\SchemaValidationError; |
7
|
|
|
use frictionlessdata\tableschema\Utils; |
8
|
|
|
|
9
|
|
|
class FieldsFactory |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* list of all the available field classes. |
13
|
|
|
* |
14
|
|
|
* this list is used when inferring field type from a value |
15
|
|
|
* infer works by trying to case the value to the field, in the fieldClasses order |
16
|
|
|
* first field that doesn't raise exception on infer wins |
17
|
|
|
*/ |
18
|
|
|
public static $fieldClasses = [ |
19
|
|
|
'\\frictionlessdata\\tableschema\\Fields\\IntegerField', |
20
|
|
|
'\\frictionlessdata\\tableschema\\Fields\\NumberField', |
21
|
|
|
'\\frictionlessdata\\tableschema\\Fields\\StringField', |
22
|
|
|
|
23
|
|
|
// these fields will not be inferred - StringField will catch all values before it reaches these |
24
|
|
|
'\\frictionlessdata\\tableschema\\Fields\\YearMonthField', |
25
|
|
|
'\\frictionlessdata\\tableschema\\Fields\\YearField', |
26
|
|
|
'\\frictionlessdata\\tableschema\\Fields\\TimeField', |
27
|
|
|
'\\frictionlessdata\\tableschema\\Fields\\ObjectField', |
28
|
|
|
'\\frictionlessdata\\tableschema\\Fields\\GeopointField', |
29
|
|
|
'\\frictionlessdata\\tableschema\\Fields\\GeojsonField', |
30
|
|
|
'\\frictionlessdata\\tableschema\\Fields\\DurationField', |
31
|
|
|
'\\frictionlessdata\\tableschema\\Fields\\DatetimeField', |
32
|
|
|
'\\frictionlessdata\\tableschema\\Fields\\DateField', |
33
|
|
|
'\\frictionlessdata\\tableschema\\Fields\\BooleanField', |
34
|
|
|
'\\frictionlessdata\\tableschema\\Fields\\ArrayField', |
35
|
|
|
'\\frictionlessdata\\tableschema\\Fields\\AnyField', |
36
|
|
|
]; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* get a new field object in the correct type according to the descriptor. |
40
|
|
|
* |
41
|
|
|
* @param object|array $descriptor |
42
|
|
|
* |
43
|
|
|
* @return BaseField |
44
|
|
|
* |
45
|
|
|
* @throws \Exception |
46
|
|
|
*/ |
47
|
|
|
public static function field($descriptor, $name = null) |
48
|
|
|
{ |
49
|
|
|
if (is_a($descriptor, 'frictionlessdata\\tableschema\\Fields\\BaseField')) { |
50
|
|
|
return $descriptor; |
|
|
|
|
51
|
|
|
} else { |
52
|
|
|
if (Utils::isJsonString($descriptor)) { |
53
|
|
|
$descriptor = json_decode($descriptor); |
54
|
|
|
} elseif (is_array($descriptor)) { |
55
|
|
|
$descriptor = json_decode(json_encode($descriptor)); |
56
|
|
|
} |
57
|
|
|
if (!isset($descriptor->name) && !is_null($name)) { |
58
|
|
|
$descriptor->name = $name; |
59
|
|
|
} |
60
|
|
|
foreach (static::$fieldClasses as $fieldClass) { |
61
|
|
|
/** @var BaseField $fieldClass */ |
62
|
|
|
if ($field = $fieldClass::inferDescriptor($descriptor)) { |
63
|
|
|
return $field; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
throw new FieldValidationException([ |
67
|
|
|
new SchemaValidationError( |
68
|
|
|
SchemaValidationError::SCHEMA_VIOLATION, |
69
|
|
|
'Could not find a valid field for descriptor: '.json_encode($descriptor)), |
70
|
|
|
]); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param $val |
76
|
|
|
* @param null $descriptor |
77
|
|
|
* |
78
|
|
|
* @return mixed |
79
|
|
|
* |
80
|
|
|
* @throws FieldValidationException |
81
|
|
|
*/ |
82
|
|
|
public static function infer($val, $descriptor = null, $lenient = false) |
83
|
|
|
{ |
84
|
|
|
foreach (static::$fieldClasses as $fieldClass) { |
85
|
|
|
/** @var BaseField $fieldClass */ |
86
|
|
|
if ($field = $fieldClass::infer($val, $descriptor, $lenient)) { |
87
|
|
|
return $field; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
throw new FieldValidationException([ |
91
|
|
|
new SchemaValidationError( |
92
|
|
|
SchemaValidationError::SCHEMA_VIOLATION, |
93
|
|
|
'Could not find a valid field for value: '.json_encode($val)), |
94
|
|
|
]); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|