* Read the Data * @param $option * @return array available options of the data source * @throws NotFoundException * @throws \OCP\Files\NotPermittedException */
($option)
| 75 | * @throws \OCP\Files\NotPermittedException |
| 76 | */ |
| 77 | public function readData($option): array |
| 78 | { |
| 79 | $error = 0; |
| 80 | $file = $this->rootFolder->getUserFolder($option['user_id'])->get($option['link']); |
| 81 | $cache = $this->getCacheMetadata($option, $file->getMTime()); |
| 82 | if ($cache['notModified'] === true) { |
| 83 | return [ |
| 84 | 'header' => [], |
| 85 | 'dimensions' => [], |
| 86 | 'data' => [], |
| 87 | 'error' => $error, |
| 88 | 'cache' => $cache, |
| 89 | ]; |
| 90 | } |
| 91 | |
| 92 | $rows = str_getcsv($file->getContent(), "\n"); |
| 93 | |
| 94 | // remove x number of rows from the beginning |
| 95 | if (isset($option['offset']) and is_numeric($option['offset'])) { |
| 96 | $rows = array_slice($rows, $option['offset']); |
| 97 | } |
| 98 | |
| 99 | $selectedColumns = array(); |
| 100 | if (isset($option['columns']) && strlen($option['columns']) > 0) { |
| 101 | $selectedColumns = str_getcsv($option['columns'], ','); |
| 102 | } |
| 103 | |
| 104 | // get the delimiter by reading the first row |
| 105 | $delimiter = $this->detectDelimiter($rows[0]); |
| 106 | |
| 107 | // the first row will define the column headers, even if it is not a real header |
| 108 | // trim removes any leading or ending spaces |
| 109 | $header = array_map('trim', str_getcsv($rows[0], $delimiter)); |
| 110 | |
| 111 | // if the data has a real header, remove the first row |
| 112 | if (!isset($option['hasHeader']) or $option['hasHeader'] !== 'false') { |
| 113 | $rows = array_slice($rows, 1); |
| 114 | } |
| 115 | |
| 116 | $data = array(); |
| 117 | if (count($selectedColumns) !== 0) { |
| 118 | // if only a subset of columns or fixed column values are set, they are replaced here |
| 119 | $header = $this->minimizeRow($selectedColumns, $header); |
| 120 | foreach ($rows as $row) { |
| 121 | $data[] = $this->minimizeRow($selectedColumns, array_map('trim', str_getcsv($row, $delimiter))); |
| 122 | } |
| 123 | } else { |
| 124 | foreach ($rows as $row) { |
| 125 | $columns = array_map('trim', str_getcsv($row, $delimiter)); |
| 126 | if (count($columns) === 1) { |
| 127 | $columns[2] = end($columns); |
| 128 | $columns[1] = null; |
| 129 | $columns[0] = null; |
| 130 | } |
| 131 | $data[] = $columns; |
| 132 | } |
| 133 | } |
| 134 | unset($rows); |
nothing calls this directly
no test coverage detected