/** * Parse a CSV file, adding the data to the stockData array. * * @param $filepath string Path to the .csv file to try and parse. */ private function __parseCsv($filepath) { $csvReader = new CsvReader(); if ($csvReader->readFile(makeAbsolutePath($filepath))) { $numLines = $csvReader->getNumLines(); // If the .csv is sane, the first line is the headers $headers = $csvReader->getLine(0); if ($headers != null) { for ($i = 1; $i < $numLines; $i++) { // For every line, convert the indexed array to an associated array // using the headers as keys $line = $csvReader->getLine($i); if ($line != null && count($line) == count($headers)) { $assocLine = array(); $numLineParts = count($line); for ($j = 0; $j < $numLineParts; $j++) { $assocLine[$headers[$j]] = $line[$j]; } array_push($this->__stockData, $assocLine); } } } } }
/** * Get the line at index. * * @param int $index The index of the line to retrieve. * @return mixed An array of line data if index is valid, otherwise null. */ public function getLine($index) { return $this->__csvReader->getLine($index); }