Changeset View
Changeset View
Standalone View
Standalone View
src/infrastructure/export/PhabricatorCSVExportFormat.php
Show All 17 Lines | final class PhabricatorCSVExportFormat | ||||
public function getFileExtension() { | public function getFileExtension() { | ||||
return 'csv'; | return 'csv'; | ||||
} | } | ||||
public function getMIMEContentType() { | public function getMIMEContentType() { | ||||
return 'text/csv'; | return 'text/csv'; | ||||
} | } | ||||
public function addHeaders(array $fields) { | |||||
$headers = mpull($fields, 'getLabel'); | |||||
$this->addRow($headers); | |||||
} | |||||
public function addObject($object, array $fields, array $map) { | public function addObject($object, array $fields, array $map) { | ||||
$values = array(); | $values = array(); | ||||
foreach ($fields as $key => $field) { | foreach ($fields as $key => $field) { | ||||
$value = $map[$key]; | $value = $map[$key]; | ||||
$value = $field->getTextValue($value); | $value = $field->getTextValue($value); | ||||
$values[] = $value; | |||||
} | |||||
$this->addRow($values); | |||||
} | |||||
private function addRow(array $values) { | |||||
$row = array(); | |||||
foreach ($values as $value) { | |||||
if (preg_match('/\s|,|\"/', $value)) { | if (preg_match('/\s|,|\"/', $value)) { | ||||
$value = str_replace('"', '""', $value); | $value = str_replace('"', '""', $value); | ||||
$value = '"'.$value.'"'; | $value = '"'.$value.'"'; | ||||
} | } | ||||
$values[] = $value; | $row[] = $value; | ||||
} | } | ||||
$this->rows[] = implode(',', $values); | $this->rows[] = implode(',', $row); | ||||
} | } | ||||
public function newFileData() { | public function newFileData() { | ||||
return implode("\n", $this->rows); | return implode("\n", $this->rows); | ||||
} | } | ||||
} | } |