<commit_before><?php

namespace Fuzz\ApiServer\Utility;

trait ExportsData
{
	/**
	 * Map export content types to supported formats
	 *
	 * @var array
	 */
	protected $content_types = [
		'csv' => 'text/csv'
	];

	/**
	 * Export data as a file
	 *
	 * @param string      $format
	 * @param array       $data
	 * @param array       $column_mappings
	 * @param null|string $filename
	 * @return mixed
	 */
	public function export($format, array $data, array $column_mappings, $filename = null)
	{
		$method = camel_case('build_' . $format);
		$output = $this->$method($data, $column_mappings);

		$headers = [
			'Content-Type'        => $this->content_types[$format],
			'Content-Disposition' => sprintf('attachment; filename="%s"', $filename),
		];

		return $this->getResponder()->send(rtrim($output, "\n"), 200, $headers, false);
	}

	/**
	 * Build the CSV string and support dot nested column mappings
	 *
	 * @param array $data
	 * @param array $column_mappings
	 * @return string
	 */
	protected function buildCsv(array $data, array $column_mappings)
	{
		$output = $this->buildCsvHeaders($column_mappings);
		foreach ($data as $row) {
			$row_data = [];

			// Map row data to columns
			foreach ($column_mappings as $column => $header) {
				$path = explode('.', $column);

				// Accept dot nested notations
				if (count($path) > 1) {
					$location = $row;

					foreach ($path as $step) {
						$location = $location[$step];
					}

					$row_data[$header] = $location;
				} else {
					$row_data[$header] = $row[$column];
				}
			}

			$output .= implode(',', $row_data) . PHP_EOL;
		}

		return $output;
	}

	/**
	 * Build the first row of a CSV
	 *
	 * @param array $column_mappings
	 * @return string
	 */
	protected function buildCsvHeaders($column_mappings)<commit_msg>Escape commas and double quotes<commit_after><?php

namespace Fuzz\ApiServer\Utility;

trait ExportsData
{
	/**
	 * Map export content types to supported formats
	 *
	 * @var array
	 */
	protected $content_types = [
		'csv' => 'text/csv'
	];

	/**
	 * Export data as a file
	 *
	 * @param string      $format
	 * @param array       $data
	 * @param array       $column_mappings
	 * @param null|string $filename
	 * @return mixed
	 */
	public function export($format, array $data, array $column_mappings, $filename = null)
	{
		$method = camel_case('build_' . $format);
		$output = $this->$method($data, $column_mappings);

		$headers = [
			'Content-Type'        => $this->content_types[$format],
			'Content-Disposition' => sprintf('attachment; filename="%s"', $filename),
		];

		return $this->getResponder()->send(rtrim($output, "\n"), 200, $headers, false);
	}

	/**
	 * Build the CSV string and support dot nested column mappings
	 *
	 * @param array $data
	 * @param array $column_mappings
	 * @return string
	 */
	protected function buildCsv(array $data, array $column_mappings)
	{
		$output = $this->buildCsvHeaders($column_mappings);
		foreach ($data as $row) {
			$row_data = [];

			// Map row data to columns
			foreach ($column_mappings as $column => $header) {
				$path = explode('.', $column);

				// Accept dot nested notations
				if (count($path) > 1) {
					$location = $row;

					foreach ($path as $step) {
						$location = $location[$step];
					}

					$row_data[$header] = '"' . $location . '"';
				} else {
					$row_data[$header] = '"' . $row[$column] . '"';
				}
			}

			$output .= implode(',', $row_data) . PHP_EOL;
		}

		return $output;
	}

	/**
	 * Build the first row of a CSV
	 *
	 * @param array $column_mappings
	 * @return string
	 */
	protected function buildCsvHeaders($column_mappings)
{'fraction_non_alphanumeric': 0.15809839690436706, 'fraction_numerical': 0.002211166390270868, 'mean_word_length': 3.0985277463193657, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1654897', 'n_tokens_mistral': 1374, 'n_tokens_neox': 1162, 'n_words': 303}