You are here

class MarkdownTableExporter in Loft Data Grids 7.2

Same name and namespace in other branches
  1. 6.2 vendor/aklump/loft_data_grids/src/AKlump/LoftDataGrids/MarkdownTableExporter.php \AKlump\LoftDataGrids\MarkdownTableExporter

Class FlatTextExporter

Hierarchy

Expanded class hierarchy of MarkdownTableExporter

File

vendor/aklump/loft_data_grids/src/AKlump/LoftDataGrids/MarkdownTableExporter.php, line 7

Namespace

AKlump\LoftDataGrids
View source
class MarkdownTableExporter extends FlatTextExporter implements ExporterInterface {
  protected $extension = '.md';
  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
      ->showPageIds();
  }
  public function getInfo() {
    $info = parent::getInfo();
    $info = array(
      'name' => 'Advanced Markdown Table',
      'shortname' => 'Markdown Table',
      'description' => 'Export data in markdown table format.',
    ) + $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 .= '## ' . $page_id . $this->format->cr;
      }
      $header = $this
        ->getHeader($page_id);
      $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
      $hrule = array();
      foreach ($data as $row_key => $row) {
        foreach ($row as $key => $value) {
          $data[$row_key][$key] = str_pad($value, $columns[$key], ' ');
          if (empty($hrule[$key])) {
            $hrule[$key] = $this->format->vline . str_pad('', 2 + $columns[$key], $this->format->hline);
          }
        }
      }
      $hrule = implode($hrule) . $this->format->vline;

      // Build the output
      $header = false;
      foreach ($data as $row) {
        $this->output .= $this
          ->collapseRow($row);
        if (!$header) {
          $this->output .= $hrule . $this->format->cr;
          $header = true;
        }
      }

      // $this->output .= $this->format->cr;
      return $this;
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CSVExporter::$page protected property
CSVExporter::collapseCell protected function Collapse a single cell in a row. 1
Exporter::$export_data protected property
Exporter::$header protected property
Exporter::$settings protected property
Exporter::addSetting public function Adds/Updates a single setting by name. Overrides ExporterInterface::addSetting
Exporter::cssSafe protected function
Exporter::dataTransform protected function Iterate over all cells and transform data as appropriate.
Exporter::export public function Export data as a string Overrides ExporterInterface::export 1
Exporter::filenameSafe protected function Return a string as a safe filename
Exporter::formatColumn public function Format a single column with format by string Overrides ExporterInterface::formatColumn 1
Exporter::getData public function Return the ExportDataInterface object Overrides ExporterInterface::getData
Exporter::getDataAsTransformedArray protected function Convert ExportData to an array transforming every cell.
Exporter::getFilename public function Get the filename Overrides ExporterInterface::getFilename
Exporter::getHeader public function Return an array each of the keys present in the data on a page. Overrides ExporterInterface::getHeader
Exporter::getSettings public function Return the settings object. Overrides ExporterInterface::getSettings
Exporter::getShowPageIds public function Return the showPageIds. Overrides ExporterInterface::getShowPageIds
Exporter::getTitle public function Overrides ExporterInterface::getTitle
Exporter::hidePageIds public function Set the exporter to hide page ids. Overrides ExporterInterface::hidePageIds
Exporter::save public function Stream as a file to the server with headers. Overrides ExporterInterface::save 1
Exporter::saveFile public function Compile and and save to a filepath. Overrides ExporterInterface::saveFile 2
Exporter::setData public function Set the export data object Overrides ExporterInterface::setData
Exporter::setFilename public function Getter/Setter for the filename Overrides ExporterInterface::setFilename
Exporter::setSettings public function Set the settings object. Overrides ExporterInterface::setSettings
Exporter::setSettingsDefault protected function Setup default values on object data. 4
Exporter::setTitle public function Set a title for the exported document Overrides ExporterInterface::setTitle 1
Exporter::showPageIds public function Set the exporter to display page ids. Overrides ExporterInterface::showPageIds
FlatTextExporter::collapseRow protected function Collapse a row Overrides CSVExporter::collapseRow
MarkdownTableExporter::$extension protected property Overrides FlatTextExporter::$extension
MarkdownTableExporter::$format protected property Overrides FlatTextExporter::$format
MarkdownTableExporter::compile public function Build the string content of $this->output and return $this for chaining. Overrides FlatTextExporter::compile
MarkdownTableExporter::getInfo public function Return info about this class Overrides FlatTextExporter::getInfo
MarkdownTableExporter::__construct public function Constructor Overrides FlatTextExporter::__construct