These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace frictionlessdata\tableschema; |
||
4 | |||
5 | use frictionlessdata\tableschema\Fields\FieldsFactory; |
||
6 | |||
7 | class EditableSchema extends Schema |
||
8 | { |
||
9 | public function __construct($descriptor = null) |
||
10 | { |
||
11 | $this->descriptor = empty($descriptor) ? (object) ['fields' => []] : $descriptor; |
||
12 | } |
||
13 | |||
14 | public function fields($newFields = null) |
||
15 | { |
||
16 | if (!is_null($newFields)) { |
||
17 | $this->descriptor()->fields = []; |
||
18 | $this->fieldsCache = []; |
||
19 | foreach ($newFields as $name => $field) { |
||
20 | if (!is_a($field, 'frictionlessdata\\tableschema\\Fields\\BaseField')) { |
||
21 | if (!isset($field->name)) { |
||
22 | $field->name = $name; |
||
23 | } |
||
24 | $field = FieldsFactory::field($field); |
||
25 | } |
||
26 | $this->fieldsCache[$name] = $field; |
||
27 | $this->descriptor()->fields[] = $field->descriptor(); |
||
28 | } |
||
29 | |||
30 | return $this->revalidate(); |
||
31 | } else { |
||
32 | return is_null($this->fieldsCache) ? [] : $this->fieldsCache; |
||
33 | } |
||
34 | } |
||
35 | |||
36 | public function field($name, $field = null) |
||
37 | { |
||
38 | $fields = $this->fields(); |
||
39 | if (!is_null($field)) { |
||
40 | $fields[$name] = $field; |
||
41 | |||
42 | return $this->fields($fields); |
||
43 | } elseif (array_key_exists($name, $fields)) { |
||
44 | return $fields[$name]; |
||
45 | } else { |
||
46 | throw new \Exception("unknown field name: {$name}"); |
||
47 | } |
||
48 | } |
||
49 | |||
50 | public function removeField($name) |
||
51 | { |
||
52 | $fields = $this->fields(); |
||
53 | unset($fields[$name]); |
||
54 | |||
55 | return $this->fields($fields); |
||
56 | } |
||
57 | |||
58 | View Code Duplication | public function primaryKey($primaryKey = null) |
|
0 ignored issues
–
show
|
|||
59 | { |
||
60 | if (is_null($primaryKey)) { |
||
61 | return parent::primaryKey(); |
||
62 | } else { |
||
63 | $this->descriptor()->primaryKey = $primaryKey; |
||
64 | |||
65 | return $this->revalidate(); |
||
66 | } |
||
67 | } |
||
68 | |||
69 | View Code Duplication | public function foreignKeys($foreignKeys = null) |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository.
Loading history...
|
|||
70 | { |
||
71 | if (is_null($foreignKeys)) { |
||
72 | return parent::foreignKeys(); |
||
73 | } else { |
||
74 | $this->descriptor()->foreignKeys = $foreignKeys; |
||
75 | |||
76 | return $this->revalidate(); |
||
77 | } |
||
78 | } |
||
79 | |||
80 | View Code Duplication | public function missingValues($missingValues = null) |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository.
Loading history...
|
|||
81 | { |
||
82 | if (is_null($missingValues)) { |
||
83 | return parent::missingValues(); |
||
84 | } else { |
||
85 | $this->descriptor()->missingValues = $missingValues; |
||
86 | |||
87 | return $this->revalidate(); |
||
88 | } |
||
89 | } |
||
90 | |||
91 | public function revalidate() |
||
92 | { |
||
93 | $validationErrors = SchemaValidator::validate($this->descriptor()); |
||
94 | if (count($validationErrors) > 0) { |
||
95 | throw new Exceptions\SchemaValidationFailedException($validationErrors); |
||
96 | } else { |
||
97 | return $this; |
||
98 | } |
||
99 | } |
||
100 | } |
||
101 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.