1 | <?php |
||
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 | 45 | public function setSchema($schemaJson) |
|
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 | 48 | public function setFile($fileName) |
|
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 | 45 | public function setPdoConnection($pdoConnection) |
|
105 | { |
||
106 | 45 | if ($pdoConnection instanceof \PDO) { |
|
107 | 39 | self::$pdoConnection = $pdoConnection; |
|
108 | 39 | 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 | 41 | protected static function openFile() |
|
125 | { |
||
126 | 41 | if (empty(self::$fileName)) { |
|
127 | 1 | throw new \Exception('CSV file not set.'); |
|
128 | } |
||
129 | |||
130 | 40 | self::$file = new \SplFileObject(self::$fileName); |
|
131 | 40 | self::$file->setFlags(\SplFileObject::READ_CSV | \SplFileObject::SKIP_EMPTY); |
|
132 | 40 | } |
|
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 | 40 | protected static function setCsvHeaderColumns() |
|
149 | |||
150 | |||
151 | /** |
||
152 | * Rewind the CSV file pointer to the first line of data. |
||
153 | * |
||
154 | * @static |
||
155 | * |
||
156 | * @return void |
||
157 | */ |
||
158 | 38 | protected static function rewindFilePointerToFirstData() |
|
162 | |||
163 | |||
164 | /** |
||
165 | * Get the data from the current CSV file row and move the pointer on to the next row. |
||
166 | * |
||
167 | * @static |
||
168 | * |
||
169 | * @return array|boolean The CSV data or false if the end of the file has been reached. |
||
170 | */ |
||
171 | 38 | protected static function loopThroughFileRows() |
|
182 | |||
183 | |||
184 | /** |
||
185 | * Get the key of the field with the specified name from the schema. |
||
186 | * This can be used to validate that a column exists in the schema. |
||
187 | * |
||
188 | * @param string $fieldName The field name. |
||
189 | * |
||
190 | * @return int The key ID or false if the field is not found. |
||
191 | */ |
||
192 | 38 | protected function getSchemaKeyFromName($fieldName) |
|
202 | |||
203 | |||
204 | /** |
||
205 | * Get the position of the field with the specified name from the CSV file. |
||
206 | * This can be used to validate that a column exists in the CSV file. |
||
207 | * |
||
208 | * @param string $fieldName The field name. |
||
209 | * |
||
210 | * @return int The position or false if the field is not found. |
||
211 | */ |
||
212 | 37 | protected function getCsvPositionFromName($fieldName) |
|
216 | |||
217 | |||
218 | /** |
||
219 | * Get the schema object for a column, given the columns position in the CSV file. |
||
220 | * |
||
221 | * @param int $csvColumnPosition The position of the column in the CSV file. |
||
222 | * |
||
223 | * @return object The schema column. |
||
224 | */ |
||
225 | 38 | protected function getSchemaColumnFromCsvColumnPosition($csvColumnPosition) |
|
232 | |||
233 | |||
234 | /** |
||
235 | * Get the type of the specified column. |
||
236 | * |
||
237 | * @param object $schemaColumn The schema column object to examine. |
||
238 | * |
||
239 | * @return string The type. |
||
240 | */ |
||
241 | 38 | protected function getColumnType($schemaColumn) |
|
245 | |||
246 | |||
247 | /** |
||
248 | * Get the format of the specified column. |
||
249 | * |
||
250 | * @param object $schemaColumn The schema column object to examine. |
||
251 | * |
||
252 | * @return string The format or null if no format is specified. |
||
253 | */ |
||
254 | 38 | protected function getColumnFormat($schemaColumn) |
|
258 | } |
||
259 |