validDataTypes() ); } // If the data was provided to us as a single array, then // convert it to a single row. if (is_array($structuredData) && !empty($structuredData)) { $firstRow = reset($structuredData); if (!is_array($firstRow)) { return [$structuredData]; } } return $structuredData; } /** * Return default values for formatter options * @return array */ protected function getDefaultFormatterOptions() { return [ FormatterOptions::INCLUDE_FIELD_LABELS => true, FormatterOptions::DELIMITER => ',', ]; } /** * @inheritdoc */ public function write(OutputInterface $output, $data, FormatterOptions $options) { $defaults = $this->getDefaultFormatterOptions(); $includeFieldLabels = $options->get(FormatterOptions::INCLUDE_FIELD_LABELS, $defaults); if ($includeFieldLabels && ($data instanceof TableTransformation)) { $headers = $data->getHeaders(); $this->writeOneLine($output, $headers, $options); } foreach ($data as $line) { $this->writeOneLine($output, $line, $options); } } protected function writeOneLine(OutputInterface $output, $data, $options) { $defaults = $this->getDefaultFormatterOptions(); $delimiter = $options->get(FormatterOptions::DELIMITER, $defaults); $output->write($this->csvEscape($data, $delimiter)); } protected function csvEscape($data, $delimiter = ',') { $buffer = fopen('php://temp', 'r+'); fputcsv($buffer, $data, $delimiter); rewind($buffer); $csv = fgets($buffer); fclose($buffer); return $csv; } }