1
|
|
|
<?php |
2
|
|
|
namespace JsonTable; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* @package JSON table |
6
|
|
|
*/ |
7
|
|
|
abstract class Base |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @static |
11
|
|
|
* |
12
|
|
|
* @var string Schema JSON |
13
|
|
|
*/ |
14
|
|
|
protected static $schemaJson; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @static |
18
|
|
|
* |
19
|
|
|
* @var string The path and name of the file to analyse. |
20
|
|
|
*/ |
21
|
|
|
protected static $fileName; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @static |
25
|
|
|
* |
26
|
|
|
* @var array The columns found in the header. |
27
|
|
|
* This is used to validate that each row has the correct number of columns |
28
|
|
|
* and to get the column name from it's position. |
29
|
|
|
*/ |
30
|
|
|
protected static $headerColumns; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @static |
34
|
|
|
* |
35
|
|
|
* @var object The SplFileObject of the CSV file. |
36
|
|
|
*/ |
37
|
|
|
protected static $file; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var object The PDO object. |
41
|
|
|
*/ |
42
|
|
|
public static $pdoConnection; |
43
|
|
|
|
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Set the schema. |
47
|
|
|
* |
48
|
|
|
* @param string $schemaJson The schema conforming to the JSON table schema specification. |
49
|
|
|
* @see http://dataprotocols.org/json-table-schema |
50
|
|
|
* |
51
|
|
|
* @return void |
52
|
|
|
* |
53
|
|
|
* @throws \Exception if the schema is not a valid JSON string. |
54
|
|
|
* @throws \Exception if the schema is an invalid data type. |
55
|
|
|
*/ |
56
|
90 |
|
public function setSchema($schemaJson) |
57
|
|
|
{ |
58
|
90 |
|
if (is_string($schemaJson)) { |
59
|
87 |
|
if (is_null($schemaJson = json_decode($schemaJson))) { |
60
|
1 |
|
throw new \Exception('The schema is not a valid JSON string.'); |
61
|
|
|
} |
62
|
89 |
|
} elseif (!is_object($schemaJson)) { |
63
|
3 |
|
throw new \Exception('Invalid schema data type.'); |
64
|
|
|
} |
65
|
|
|
|
66
|
86 |
|
foreach ($schemaJson->fields as &$field) { |
67
|
86 |
|
$field->name = strtolower($field->name); |
68
|
86 |
|
} |
69
|
86 |
|
unset($field); |
70
|
|
|
|
71
|
86 |
|
self::$schemaJson = $schemaJson; |
72
|
86 |
|
} |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Set the file. |
77
|
|
|
* This checks that the file exists. |
78
|
|
|
* |
79
|
|
|
* @param string $fileName The path and name of the file to analyse. |
80
|
|
|
* @see http://dataprotocols.org/json-table-schema |
81
|
|
|
* |
82
|
|
|
* @return boolean Whether the file was successfully set. |
83
|
|
|
*/ |
84
|
91 |
|
public function setFile($fileName) |
85
|
|
|
{ |
86
|
91 |
|
if (file_exists($fileName)) { |
87
|
84 |
|
self::$fileName = (string) $fileName; |
88
|
84 |
|
return true; |
89
|
|
|
} |
90
|
|
|
|
91
|
7 |
|
return false; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Set the database connection. |
97
|
|
|
* |
98
|
|
|
* @static |
99
|
|
|
* |
100
|
|
|
* @param object $pdoConnection The PDO object. |
101
|
|
|
* |
102
|
|
|
* @return boolean Whether the connection was valid. |
103
|
|
|
*/ |
104
|
89 |
|
public function setPdoConnection($pdoConnection) |
105
|
|
|
{ |
106
|
89 |
|
if ($pdoConnection instanceof \PDO) { |
107
|
83 |
|
self::$pdoConnection = $pdoConnection; |
108
|
83 |
|
return true; |
109
|
|
|
} |
110
|
|
|
|
111
|
6 |
|
return false; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Open a handle to the file to be analysed. |
117
|
|
|
* |
118
|
|
|
* @static |
119
|
|
|
* |
120
|
|
|
* @return void |
121
|
|
|
* |
122
|
|
|
* @throws \Exception if a CSV file has not been set. |
123
|
|
|
*/ |
124
|
84 |
|
public static function openFile() |
125
|
|
|
{ |
126
|
84 |
|
if (empty(self::$fileName)) { |
127
|
1 |
|
throw new \Exception('CSV file not set.'); |
128
|
|
|
} |
129
|
|
|
|
130
|
83 |
|
self::$file = new \SplFileObject(self::$fileName); |
131
|
83 |
|
self::$file->setFlags(\SplFileObject::READ_CSV | \SplFileObject::SKIP_EMPTY); |
132
|
83 |
|
} |
133
|
|
|
|
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Set the CSV header columns from those in the file. |
137
|
|
|
* These are stored in lowercase as all column to schema checking is considered as case insensitive. |
138
|
|
|
* |
139
|
|
|
* @static |
140
|
|
|
* |
141
|
|
|
* @return true on success. |
142
|
|
|
*/ |
143
|
76 |
|
public static function setCsvHeaderColumns() |
144
|
|
|
{ |
145
|
76 |
|
self::$file->rewind(); |
146
|
76 |
|
self::$headerColumns = array_map('strtolower', self::$file->current()); |
147
|
76 |
|
return true; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Reset all the static properties of the class. |
153
|
|
|
* This is run by the testing suite at the end of each test. |
154
|
|
|
*/ |
155
|
81 |
|
public static function reset() |
156
|
|
|
{ |
157
|
81 |
|
self::$schemaJson = null; |
158
|
81 |
|
self::$fileName = null; |
159
|
81 |
|
self::$headerColumns = null; |
|
|
|
|
160
|
81 |
|
self::$file = null; |
161
|
81 |
|
self::$pdoConnection = null; |
162
|
81 |
|
} |
163
|
|
|
|
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Rewind the CSV file pointer to the first line of data. |
167
|
|
|
* |
168
|
|
|
* @static |
169
|
|
|
* |
170
|
|
|
* @return void |
171
|
|
|
*/ |
172
|
81 |
|
protected static function rewindFilePointerToFirstData() |
173
|
|
|
{ |
174
|
81 |
|
self::$file->seek(1); |
175
|
81 |
|
} |
176
|
|
|
|
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Get the data from the current CSV file row and move the pointer on to the next row. |
180
|
|
|
* |
181
|
|
|
* @static |
182
|
|
|
* |
183
|
|
|
* @return array|boolean The CSV data or false if the end of the file has been reached. |
184
|
|
|
*/ |
185
|
74 |
|
protected static function loopThroughFileRows() |
186
|
|
|
{ |
187
|
74 |
|
if (self::$file->eof()) { |
188
|
|
|
return false; |
189
|
|
|
} |
190
|
|
|
|
191
|
74 |
|
$csvRow = self::$file->current(); |
192
|
74 |
|
self::$file->next(); |
193
|
|
|
|
194
|
74 |
|
return $csvRow; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Get the key of the field with the specified name from the schema. |
200
|
|
|
* This can be used to validate that a column exists in the schema. |
201
|
|
|
* |
202
|
|
|
* @param string $fieldName The field name. |
203
|
|
|
* |
204
|
|
|
* @return int The key ID or false if the field is not found. |
205
|
|
|
*/ |
206
|
74 |
|
protected function getSchemaKeyFromName($fieldName) |
207
|
|
|
{ |
208
|
74 |
|
foreach (self::$schemaJson->fields as $key => $field) { |
209
|
74 |
|
if ($field->name === $fieldName) { |
210
|
74 |
|
return $key; |
211
|
|
|
} |
212
|
72 |
|
} |
213
|
|
|
|
214
|
|
|
return false; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Get the position of the field with the specified name from the CSV file. |
220
|
|
|
* This can be used to validate that a column exists in the CSV file. |
221
|
|
|
* |
222
|
|
|
* @param string $fieldName The field name. |
223
|
|
|
* |
224
|
|
|
* @return int The position or false if the field is not found. |
225
|
|
|
*/ |
226
|
73 |
|
protected function getCsvPositionFromName($fieldName) |
227
|
|
|
{ |
228
|
73 |
|
return array_search($fieldName, self::$headerColumns); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Get the schema object for a column, given the columns position in the CSV file. |
234
|
|
|
* |
235
|
|
|
* @param int $csvColumnPosition The position of the column in the CSV file. |
236
|
|
|
* |
237
|
|
|
* @return object The schema column. |
238
|
|
|
*/ |
239
|
73 |
|
protected function getSchemaColumnFromCsvColumnPosition($csvColumnPosition) |
240
|
|
|
{ |
241
|
73 |
|
$csvColumnName = self::$headerColumns[$csvColumnPosition]; |
242
|
73 |
|
$schemaKey = $this->getSchemaKeyFromName($csvColumnName); |
243
|
|
|
|
244
|
73 |
|
return self::$schemaJson->fields[$schemaKey]; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Get the type of the specified column. |
250
|
|
|
* |
251
|
|
|
* @param object $schemaColumn The schema column object to examine. |
252
|
|
|
* |
253
|
|
|
* @return string The type. |
254
|
|
|
*/ |
255
|
73 |
|
protected function getColumnType($schemaColumn) |
256
|
|
|
{ |
257
|
73 |
|
return (property_exists($schemaColumn, 'type')) ? $schemaColumn->type : 'string'; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Get the format of the specified column. |
263
|
|
|
* |
264
|
|
|
* @param object $schemaColumn The schema column object to examine. |
265
|
|
|
* |
266
|
|
|
* @return string The format or null if no format is specified. |
267
|
|
|
*/ |
268
|
73 |
|
protected function getColumnFormat($schemaColumn) |
269
|
|
|
{ |
270
|
73 |
|
return (property_exists($schemaColumn, 'format')) ? $schemaColumn->format : 'default'; |
271
|
|
|
} |
272
|
|
|
} |
273
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..