YAMLFrontMatterExporter.php in Loft Data Grids 7.2
File
vendor/aklump/loft_data_grids/src/AKlump/LoftDataGrids/YAMLFrontMatterExporter.php
View source
<?php
namespace AKlump\LoftDataGrids;
use Symfony\Component\Yaml\Yaml;
class YAMLFrontMatterExporter extends Exporter implements ExporterInterface {
public function __construct(ExportDataInterface $data = null, $filename = '') {
parent::__construct($data, $filename);
unset($this->extension);
}
public function __get($key) {
if ($key === 'extension') {
return $this->settings->extension;
}
return null;
}
protected function setSettingsDefault() {
parent::setSettingsDefault();
$this->settings->bodyKey = 'body';
$this->settings->extension = '.html';
return $this;
}
public function getInfo() {
$info = parent::getInfo();
$info = array(
'name' => 'YAML Front Matter Format',
'shortname' => 'YAML Front Matter',
'description' => 'Export data as text file with YAML Front Matter. For more information visit: http://www.yaml.org.',
) + $info;
return $info;
}
public function compile($page_id = null) {
$bodyKey = $this->settings->bodyKey;
$data = $this
->getData()
->getPage();
$row = reset($data);
$build = array();
$this->output = '';
if ($data) {
$build[] = '---';
if ($build['fm'] = array_diff_key($row, array(
$bodyKey => null,
))) {
$build['fm'] = trim(Yaml::dump($build['fm']));
}
$build[] = '---';
if (empty($build['fm'])) {
$build = array();
}
$build['body'] = isset($row[$bodyKey]) ? $row[$bodyKey] : '';
$this->output = implode(PHP_EOL, $build);
}
return $this;
}
}