protected function parse($response) { $file = $this->temp->createTmpFile($response->fileName); $filePath = $file->getPathname(); $this->client->get($this->getFileUrl($response), ['save_to' => $filePath]); Logger::log("debug", "Report downloaded", Utils::to_assoc($response)); $table = Utils::createCsv($this->temp, $response->fileName); if (!empty($this->config["primaryKey"])) { foreach (explode(",", $this->config["primaryKey"]) as $column) { $pk[] = $this->createSafeSapiName($column); } $table->setPrimaryKey(join(",", $pk)); } $csvHandle = fopen($table->getPathname(), "w+"); $handle = fopen($filePath, "r"); $desc = array(); $lineNum = 1; while (($line = fgets($handle)) !== false) { if ($lineNum == 1) { // 1st row = Report Name $reportName = trim($line); $desc["DFA.Report_name"] = $reportName; } elseif ($line == "\n") { // skip empty lines } elseif ($line == "Report Fields\n") { // Report CSV data start $reportStartLine = $lineNum + 1; } elseif (strpos($line, "Grand Total:,") === 0) { // Last line in CSV - ignore break; } elseif (isset($reportStartLine) && $lineNum == $reportStartLine) { // First row = header $header = $this->validateHeader($line); fwrite($csvHandle, $header); } elseif (isset($reportStartLine) && $lineNum > $reportStartLine) { // CSV Data fwrite($csvHandle, $line); } else { // File "header" data list($key, $val) = explode(",", $line, 2); $key = preg_replace('/[^A-Za-z0-9-.]/', '_', $key); $desc["DFA.metadata." . $key] = trim($val); } $lineNum++; if ($lineNum > 99999 && $lineNum % 10000 == 0) { Logger::log("warning", "Report '{$reportName}' size exceeded {$lineNum} rows."); } } $table->setAttributes(array_replace($this->attributes, $desc)); Logger::log("debug", "Report {$this->config["reportId"]}: {$reportName} parsed!"); $this->tables[$this->config["reportId"] . "_" . $response->fileName] = $table; }