|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace frictionlessdata\tableschema; |
|
4
|
|
|
|
|
5
|
|
|
use frictionlessdata\tableschema\Fields\BaseField; |
|
6
|
|
|
use frictionlessdata\tableschema\Fields\FieldsFactory; |
|
7
|
|
|
|
|
8
|
|
|
class EditableSchema extends Schema |
|
9
|
|
|
{ |
|
10
|
|
|
public function __construct($descriptor = null) |
|
11
|
|
|
{ |
|
12
|
|
|
$this->descriptor = empty($descriptor) ? (object) ['fields' => []] : $descriptor; |
|
13
|
|
|
} |
|
14
|
|
|
|
|
15
|
|
|
public function fields($newFields = null) |
|
16
|
|
|
{ |
|
17
|
|
|
if (!is_null($newFields)) { |
|
18
|
|
|
$this->descriptor()->fields = []; |
|
19
|
|
|
$this->fieldsCache = []; |
|
20
|
|
|
foreach ($newFields as $name => $field) { |
|
21
|
|
|
if (!is_a($field, "frictionlessdata\\tableschema\\Fields\\BaseField")) { |
|
22
|
|
|
if (!isset($field->name)) $field->name = $name; |
|
23
|
|
|
$field = FieldsFactory::field($field); |
|
24
|
|
|
} |
|
25
|
|
|
$this->fieldsCache[$name] = $field; |
|
26
|
|
|
$this->descriptor()->fields[] = $field->descriptor(); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
return $this->revalidate(); |
|
|
|
|
|
|
30
|
|
|
} else { |
|
31
|
|
|
return is_null($this->fieldsCache) ? [] : $this->fieldsCache; |
|
32
|
|
|
} |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function field($name, $field = null) |
|
36
|
|
|
{ |
|
37
|
|
|
$fields = $this->fields(); |
|
38
|
|
|
if (!is_null($field)) { |
|
39
|
|
|
$fields[$name] = $field; |
|
40
|
|
|
|
|
41
|
|
|
return $this->fields($fields); |
|
42
|
|
|
} elseif (array_key_exists($name, $fields)) { |
|
43
|
|
|
return $fields[$name]; |
|
44
|
|
|
} else { |
|
45
|
|
|
throw new \Exception("unknown field name: {$name}"); |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function removeField($name) |
|
50
|
|
|
{ |
|
51
|
|
|
$fields = $this->fields(); |
|
52
|
|
|
unset($fields[$name]); |
|
53
|
|
|
|
|
54
|
|
|
return $this->fields($fields); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
View Code Duplication |
public function primaryKey($primaryKey = null) |
|
|
|
|
|
|
58
|
|
|
{ |
|
59
|
|
|
if (is_null($primaryKey)) { |
|
60
|
|
|
return parent::primaryKey(); |
|
61
|
|
|
} else { |
|
62
|
|
|
$this->descriptor()->primaryKey = $primaryKey; |
|
63
|
|
|
|
|
64
|
|
|
return $this->revalidate(); |
|
|
|
|
|
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
View Code Duplication |
public function foreignKeys($foreignKeys = null) |
|
|
|
|
|
|
69
|
|
|
{ |
|
70
|
|
|
if (is_null($foreignKeys)) { |
|
71
|
|
|
return parent::foreignKeys(); |
|
72
|
|
|
} else { |
|
73
|
|
|
$this->descriptor()->foreignKeys = $foreignKeys; |
|
74
|
|
|
|
|
75
|
|
|
return $this->revalidate(); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
View Code Duplication |
public function missingValues($missingValues = null) |
|
|
|
|
|
|
80
|
|
|
{ |
|
81
|
|
|
if (is_null($missingValues)) { |
|
82
|
|
|
return parent::missingValues(); |
|
83
|
|
|
} else { |
|
84
|
|
|
$this->descriptor()->missingValues = $missingValues; |
|
85
|
|
|
|
|
86
|
|
|
return $this->revalidate(); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function revalidate() |
|
91
|
|
|
{ |
|
92
|
|
|
$validationErrors = SchemaValidator::validate($this->descriptor()); |
|
93
|
|
|
if (count($validationErrors) > 0) { |
|
94
|
|
|
throw new Exceptions\SchemaValidationFailedException($validationErrors); |
|
95
|
|
|
} else { |
|
96
|
|
|
return $this; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.