Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
66.67% |
56 / 84 |
|
64.29% |
9 / 14 |
CRAP | |
0.00% |
0 / 1 |
| tao_helpers_data_CsvFile | |
66.67% |
56 / 84 |
|
64.29% |
9 / 14 |
107.33 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| setData | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getData | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setColumnMapping | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getColumnMapping | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| load | |
88.57% |
31 / 35 |
|
0.00% |
0 / 1 |
13.25 | |||
| setOptions | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getOptions | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getRow | |
84.62% |
11 / 13 |
|
0.00% |
0 / 1 |
5.09 | |||
| count | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getValue | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
56 | |||
| setValue | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
56 | |||
| getColumnCount | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setColumnCount | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2008-2010 (original work) Deutsche Institut für Internationale Pädagogische Forschung |
| 19 | * (under the project TAO-TRANSFER); |
| 20 | * 2009-2012 (update and modification) Public Research Centre Henri Tudor |
| 21 | * (under the project TAO-SUSTAIN & TAO-DEV); |
| 22 | * 2012-2018 (update and modification) Open Assessment Technologies SA; |
| 23 | * |
| 24 | */ |
| 25 | |
| 26 | use oat\oatbox\filesystem\File; |
| 27 | |
| 28 | /** |
| 29 | * Short description of class tao_helpers_data_CsvFile |
| 30 | * |
| 31 | * @access public |
| 32 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 33 | * @package tao |
| 34 | */ |
| 35 | class tao_helpers_data_CsvFile |
| 36 | { |
| 37 | public const FIELD_DELIMITER = 'field_delimiter'; |
| 38 | public const FIELD_ENCLOSER = 'field_encloser'; |
| 39 | public const MULTI_VALUES_DELIMITER = 'multi_values_delimiter'; |
| 40 | public const FIRST_ROW_COLUMN_NAMES = 'first_row_column_names'; |
| 41 | /** |
| 42 | * Contains the CSV data as a simple 2-dimensional array. Keys are integer |
| 43 | * the mapping done separatyely if column names are provided. |
| 44 | * |
| 45 | * @access private |
| 46 | * @var array |
| 47 | */ |
| 48 | private $data = []; |
| 49 | /** |
| 50 | * Contains the mapping for column names if the CSV file contains a row |
| 51 | * with column names. |
| 52 | * |
| 53 | * [0] ='id' |
| 54 | * [1] = 'label' |
| 55 | * ... |
| 56 | * |
| 57 | * If it has no name, empty string for this index. |
| 58 | * |
| 59 | * @access private |
| 60 | * @var array |
| 61 | */ |
| 62 | private $columnMapping = []; |
| 63 | /** |
| 64 | * Options such as string delimiter, new line escaping sequence, ... |
| 65 | * |
| 66 | * @access private |
| 67 | * @var array |
| 68 | */ |
| 69 | private $options = []; |
| 70 | /** |
| 71 | * The count of columns in the CsvFile. Will be updated at each row |
| 72 | * The largest count will be taken into account. |
| 73 | * |
| 74 | * @access private |
| 75 | * @var Integer |
| 76 | */ |
| 77 | private $columnCount = null; |
| 78 | /** |
| 79 | * Short description of method __construct |
| 80 | * |
| 81 | * @access public |
| 82 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 83 | * @param array options |
| 84 | * @return mixed |
| 85 | */ |
| 86 | public function __construct($options = []) |
| 87 | { |
| 88 | $defaults = ['field_delimiter' => ';', |
| 89 | 'field_encloser' => '"', |
| 90 | // if empty - don't use multi_values |
| 91 | 'multi_values_delimiter' => '', |
| 92 | 'first_row_column_names' => true]; |
| 93 | $this->setOptions(array_merge($defaults, $options)); |
| 94 | $this->setColumnCount(0); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Short description of method setData |
| 99 | * |
| 100 | * @access protected |
| 101 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 102 | * @param array data |
| 103 | * @return void |
| 104 | */ |
| 105 | protected function setData($data) |
| 106 | { |
| 107 | $this->data = $data; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Short description of method getData |
| 112 | * |
| 113 | * @access public |
| 114 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 115 | * @return array |
| 116 | */ |
| 117 | public function getData() |
| 118 | { |
| 119 | return (array)$this->data; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Short description of method setColumnMapping |
| 124 | * |
| 125 | * @access protected |
| 126 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 127 | * @param array columnMapping |
| 128 | * @return void |
| 129 | */ |
| 130 | protected function setColumnMapping($columnMapping) |
| 131 | { |
| 132 | $this->columnMapping = $columnMapping; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Short description of method getColumnMapping |
| 137 | * |
| 138 | * @access public |
| 139 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 140 | * @return array |
| 141 | */ |
| 142 | public function getColumnMapping() |
| 143 | { |
| 144 | return (array)$this->columnMapping; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Load the file and parse csv lines |
| 149 | * |
| 150 | * Extract headers if `first_row_column_names` is in $this->options |
| 151 | * |
| 152 | * @access public |
| 153 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 154 | * @param string $source |
| 155 | * @return void |
| 156 | */ |
| 157 | public function load($source) |
| 158 | { |
| 159 | if ($source instanceof File) { |
| 160 | $resource = $source->readStream(); |
| 161 | } else { |
| 162 | if (!is_file($source)) { |
| 163 | throw new InvalidArgumentException("Expected CSV file '" . $source . "' could not be open."); |
| 164 | } |
| 165 | if (!is_readable($source)) { |
| 166 | throw new InvalidArgumentException("CSV file '" . $source . "' is not readable."); |
| 167 | } |
| 168 | $resource = fopen($source, 'r'); |
| 169 | } |
| 170 | |
| 171 | // More readable variables |
| 172 | $enclosure = preg_quote($this->options['field_encloser'], '/'); |
| 173 | $delimiter = $this->options['field_delimiter']; |
| 174 | $multiValueSeparator = $this->options['multi_values_delimiter']; |
| 175 | $adle = ini_get('auto_detect_line_endings'); |
| 176 | ini_set('auto_detect_line_endings', true); |
| 177 | if ($this->options['first_row_column_names']) { |
| 178 | $fields = fgetcsv($resource, 0, $delimiter, $enclosure); |
| 179 | $this->setColumnMapping($fields); |
| 180 | } |
| 181 | |
| 182 | $data = []; |
| 183 | while (($rowFields = fgetcsv($resource, 0, $delimiter, $enclosure)) !== false) { |
| 184 | $lineData = []; |
| 185 | foreach ($rowFields as $fieldData) { |
| 186 | // If there is nothing in the cell, replace by null for abstraction consistency. |
| 187 | if ($fieldData == '') { |
| 188 | $fieldData = null; |
| 189 | } elseif (!empty($multiValueSeparator) && mb_strpos($fieldData, $multiValueSeparator) !== false) { |
| 190 | // try to split by multi_value_delimiter |
| 191 | $multiField = []; |
| 192 | foreach (explode($multiValueSeparator, $fieldData) as $item) { |
| 193 | if (!empty($item)) { |
| 194 | $multiField[] = $item; |
| 195 | } |
| 196 | } |
| 197 | $fieldData = $multiField; |
| 198 | } |
| 199 | $lineData[] = $fieldData; |
| 200 | } |
| 201 | $data[] = $lineData; |
| 202 | // Update the column count. |
| 203 | $currentRowColumnCount = count($rowFields); |
| 204 | if ($this->getColumnCount() < $currentRowColumnCount) { |
| 205 | $this->setColumnCount($currentRowColumnCount); |
| 206 | } |
| 207 | } |
| 208 | ini_set('auto_detect_line_endings', $adle); |
| 209 | fclose($resource); |
| 210 | $this->setData($data); |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Short description of method setOptions |
| 215 | * |
| 216 | * @access public |
| 217 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 218 | * @param array array |
| 219 | * @return void |
| 220 | */ |
| 221 | public function setOptions($array = []) |
| 222 | { |
| 223 | $this->options = $array; |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * Short description of method getOptions |
| 228 | * |
| 229 | * @access public |
| 230 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 231 | * @return array |
| 232 | */ |
| 233 | public function getOptions() |
| 234 | { |
| 235 | return (array)$this->options; |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * Get a row at a given row $index. |
| 240 | * |
| 241 | * @access public |
| 242 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 243 | * @param int $index The row index. First = 0. |
| 244 | * @param boolean $associative Says that if the keys of the array must be the column names or not. If $associative |
| 245 | * is set to true but there are no column names in the CSV file, an |
| 246 | * IllegalArgumentException is thrown. |
| 247 | * @return array |
| 248 | */ |
| 249 | public function getRow($index, $associative = false) |
| 250 | { |
| 251 | $data = $this->getData(); |
| 252 | if (isset($data[$index])) { |
| 253 | if ($associative == false) { |
| 254 | $returnValue = $data[$index]; |
| 255 | } else { |
| 256 | $mapping = $this->getColumnMapping(); |
| 257 | if (!count($mapping)) { |
| 258 | // Trying to access by column name but no mapping detected. |
| 259 | throw new InvalidArgumentException("Cannot access column mapping for this CSV file."); |
| 260 | } else { |
| 261 | $mappedRow = []; |
| 262 | for ( |
| 263 | $i = 0; $i < count($mapping); $i++ |
| 264 | ) { |
| 265 | $mappedRow[$mapping[$i]] = $data[$index][$i]; |
| 266 | } |
| 267 | $returnValue = $mappedRow; |
| 268 | } |
| 269 | } |
| 270 | } else { |
| 271 | throw new InvalidArgumentException("No row at index ${index}."); |
| 272 | } |
| 273 | |
| 274 | return (array)$returnValue; |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * Counts the number of rows in the CSV File. |
| 279 | * |
| 280 | * @access public |
| 281 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 282 | * @return int |
| 283 | */ |
| 284 | public function count() |
| 285 | { |
| 286 | return (int)count($this->getData()); |
| 287 | } |
| 288 | |
| 289 | /** |
| 290 | * Get the value at the specified $row,$col. |
| 291 | * |
| 292 | * @access public |
| 293 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 294 | * @param int row Row index. If there is now row at $index, an IllegalArgumentException is thrown. |
| 295 | * @param int col |
| 296 | * @return mixed |
| 297 | */ |
| 298 | public function getValue($row, $col) |
| 299 | { |
| 300 | $returnValue = null; |
| 301 | $data = $this->getData(); |
| 302 | if (isset($data[$row][$col])) { |
| 303 | $returnValue = $data[$row][$col]; |
| 304 | } elseif (isset($data[$row]) && is_string($col)) { |
| 305 | // try to access by col name. |
| 306 | $mapping = $this->getColumnMapping(); |
| 307 | for ( |
| 308 | $i = 0; $i < count($mapping); $i++ |
| 309 | ) { |
| 310 | if ($mapping[$i] == $col && isset($data[$row][$col])) { |
| 311 | // Column with name $col extists. |
| 312 | $returnValue = $data[$row][$col]; |
| 313 | } |
| 314 | } |
| 315 | } else { |
| 316 | throw new InvalidArgumentException("No value at ${row},${col}."); |
| 317 | } |
| 318 | return $returnValue; |
| 319 | } |
| 320 | |
| 321 | /** |
| 322 | * Sets a value at the specified $row,$col. |
| 323 | * |
| 324 | * @access public |
| 325 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 326 | * @param int row Row Index. If there is no such row, an IllegalArgumentException is thrown. |
| 327 | * @param int col |
| 328 | * @param int value The value to set at $row,$col. |
| 329 | * @return void |
| 330 | */ |
| 331 | public function setValue($row, $col, $value) |
| 332 | { |
| 333 | $data = $this->getData(); |
| 334 | if (isset($data[$row][$col])) { |
| 335 | $this->data[$row][$col] = $value; |
| 336 | } elseif (isset($data[$row]) && is_string($col)) { |
| 337 | // try to access by col name. |
| 338 | $mapping = $this->getColumnMapping(); |
| 339 | for ( |
| 340 | $i = 0; $i < count($mapping); $i++ |
| 341 | ) { |
| 342 | if ($mapping[$i] == $col && isset($data[$row][$col])) { |
| 343 | // Column with name $col extists. |
| 344 | $this->data[$row][$col] = $value; |
| 345 | } |
| 346 | } |
| 347 | // Not found. |
| 348 | throw new InvalidArgumentException("Unknown column ${col}"); |
| 349 | } else { |
| 350 | throw new InvalidArgumentException("No value at ${row},${col}."); |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | /** |
| 355 | * Gets the count of columns contained in the CsvFile. |
| 356 | * |
| 357 | * @access public |
| 358 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 359 | * @return int |
| 360 | */ |
| 361 | public function getColumnCount() |
| 362 | { |
| 363 | return (int)$this->columnCount; |
| 364 | } |
| 365 | |
| 366 | /** |
| 367 | * Sets the column count. |
| 368 | * |
| 369 | * @access protected |
| 370 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 371 | * @param int count The column count. |
| 372 | * @return void |
| 373 | */ |
| 374 | protected function setColumnCount($count) |
| 375 | { |
| 376 | $this->columnCount = $count; |
| 377 | } |
| 378 | } |