class FlatTextExporter in Loft Data Grids 7.2
Same name and namespace in other branches
- 6.2 vendor/aklump/loft_data_grids/src/AKlump/LoftDataGrids/FlatTextExporter.php \AKlump\LoftDataGrids\FlatTextExporter
Class FlatTextExporter
Hierarchy
- class \AKlump\LoftDataGrids\Exporter implements ExporterInterface
- class \AKlump\LoftDataGrids\CSVExporter implements ExporterInterface
- class \AKlump\LoftDataGrids\FlatTextExporter implements ExporterInterface
- class \AKlump\LoftDataGrids\CSVExporter implements ExporterInterface
Expanded class hierarchy of FlatTextExporter
File
- vendor/
aklump/ loft_data_grids/ src/ AKlump/ LoftDataGrids/ FlatTextExporter.php, line 7
Namespace
AKlump\LoftDataGridsView source
class FlatTextExporter extends CSVExporter implements ExporterInterface {
protected $extension = '.txt';
protected $format;
/**
* Constructor
*/
public function __construct(ExportDataInterface $data = null, $filename = '') {
parent::__construct($data, $filename);
$this->format = new \stdClass();
$this->format->cr = "\n";
$this->format->hline = "-";
$this->format->vline = "|";
$this->format->bol = $this->format->vline;
$this->format->eol = $this->format->vline . $this->format->cr;
$this->format->left = ' ';
$this->format->right = ' ';
$this->format->sep = $this->format->vline;
$this->format->escape = '';
$this->format->html = true;
$this
->hidePageIds();
}
public function getInfo() {
$info = parent::getInfo();
$info = array(
'name' => 'Monospace Flatfile Text',
'shortname' => 'Flat Text',
'description' => 'Export data in a plain-text format. Columns and rows are drawn with text pipes and hyphens. Best results when using monospaced fonts.',
) + $info;
return $info;
}
public function compile($page_id = null) {
$this->output = '';
$pages = $this
->getData()
->get();
if ($page_id && array_key_exists($page_id, $pages)) {
$pages = array(
$pages[$page_id],
);
}
foreach ($pages as $page_id => $data) {
if ($this
->getShowPageIds()) {
$this->output .= '-- ' . strtoupper($page_id) . ' --' . $this->format->cr;
}
$header = $this
->getHeader($page_id);
foreach ($header as $key => $title) {
$header[$key] = strtoupper($title);
}
$header = array_combine(array_keys(reset($data)), $header);
array_unshift($data, $header);
// Scan the data to determine the total width of each column
$columns = array();
foreach ($data as $row) {
foreach ($row as $key => $value) {
if (empty($columns[$key])) {
$columns[$key] = 0;
}
$columns[$key] = max($columns[$key], strlen($value));
}
}
// Pad all the cells based on our determination from above
foreach ($data as $row_key => $row) {
foreach ($row as $key => $value) {
$data[$row_key][$key] = str_pad($value, $columns[$key], ' ');
}
}
// Determine the width of a single row in chars
$row_width = 0;
$row_width += array_sum($columns);
$row_width += strlen($this->format->bol);
$row_width += strlen($this->format->left) * count($columns);
$row_width += strlen($this->format->sep) * count($columns) - 2;
$row_width += strlen($this->format->right) * count($columns);
$row_width += strlen($this->format->eol);
$hrule = str_repeat($this->format->hline, $row_width);
// Build the output
$this->output .= $hrule . $this->format->cr;
foreach ($data as $row) {
$this->output .= $this
->collapseRow($row);
$this->output .= $hrule . $this->format->cr;
}
}
return $this;
}
/**
* Collapse a row
*/
protected function collapseRow($row) {
$output = '';
// Check if we're dealing with a simple or complex row
if (isset($row['data'])) {
foreach ($row as $key => $value) {
if ($key == 'data') {
$cells = $value;
}
}
}
else {
$cells = $row;
}
$output = array();
if (count($cells)) {
foreach ($cells as $cell) {
//compress a complex cell
if (is_array($cell)) {
$cell = isset($cell['data']) ? $cell['data'] : '';
}
if (!$this->format->html) {
$cell = strip_tags($cell);
}
// Escape chars that conflice with delimiters
if (!empty($this->format->escape)) {
$escapeables = array(
$this->format->left,
$this->format->right,
);
$escapeables = array_filter(array_unique($escapeables));
foreach ($escapeables as $find) {
$cell = str_replace($find, $this->format->escape . $find, $cell);
}
}
// A cell cannot contain line breaks so we replace them
$cell = preg_replace('/\\r\\n|\\r|\\n/', '; ', $cell);
$output[] = $this->format->left . $cell . $this->format->right;
}
}
$output = $this->format->bol . implode($this->format->sep, $output) . $this->format->eol;
return $output;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CSVExporter:: |
protected | property | ||
CSVExporter:: |
protected | function | Collapse a single cell in a row. | 1 |
Exporter:: |
protected | property | ||
Exporter:: |
protected | property | ||
Exporter:: |
protected | property | ||
Exporter:: |
public | function |
Adds/Updates a single setting by name. Overrides ExporterInterface:: |
|
Exporter:: |
protected | function | ||
Exporter:: |
protected | function | Iterate over all cells and transform data as appropriate. | |
Exporter:: |
public | function |
Export data as a string Overrides ExporterInterface:: |
1 |
Exporter:: |
protected | function | Return a string as a safe filename | |
Exporter:: |
public | function |
Format a single column with format by string Overrides ExporterInterface:: |
1 |
Exporter:: |
public | function |
Return the ExportDataInterface object Overrides ExporterInterface:: |
|
Exporter:: |
protected | function | Convert ExportData to an array transforming every cell. | |
Exporter:: |
public | function |
Get the filename Overrides ExporterInterface:: |
|
Exporter:: |
public | function |
Return an array each of the keys present in the data on a page. Overrides ExporterInterface:: |
|
Exporter:: |
public | function |
Return the settings object. Overrides ExporterInterface:: |
|
Exporter:: |
public | function |
Return the showPageIds. Overrides ExporterInterface:: |
|
Exporter:: |
public | function |
Overrides ExporterInterface:: |
|
Exporter:: |
public | function |
Set the exporter to hide page ids. Overrides ExporterInterface:: |
|
Exporter:: |
public | function |
Stream as a file to the server with headers. Overrides ExporterInterface:: |
1 |
Exporter:: |
public | function |
Compile and and save to a filepath. Overrides ExporterInterface:: |
2 |
Exporter:: |
public | function |
Set the export data object Overrides ExporterInterface:: |
|
Exporter:: |
public | function |
Getter/Setter for the filename Overrides ExporterInterface:: |
|
Exporter:: |
public | function |
Set the settings object. Overrides ExporterInterface:: |
|
Exporter:: |
protected | function | Setup default values on object data. | 4 |
Exporter:: |
public | function |
Set a title for the exported document Overrides ExporterInterface:: |
1 |
Exporter:: |
public | function |
Set the exporter to display page ids. Overrides ExporterInterface:: |
|
FlatTextExporter:: |
protected | property |
Overrides CSVExporter:: |
1 |
FlatTextExporter:: |
protected | property |
Overrides CSVExporter:: |
1 |
FlatTextExporter:: |
protected | function |
Collapse a row Overrides CSVExporter:: |
|
FlatTextExporter:: |
public | function |
Build the string content of $this->output and return $this for chaining. Overrides CSVExporter:: |
1 |
FlatTextExporter:: |
public | function |
Return info about this class Overrides CSVExporter:: |
1 |
FlatTextExporter:: |
public | function |
Constructor Overrides CSVExporter:: |
1 |