class YAMLFrontMatterImporter in Loft Data Grids 7.2
Class YAMLFrontMatterImporter
Pull data out of a YAMLFrontMatterExporter output string into an ExportData object.
@package AKlump\LoftDataGrids
Hierarchy
- class \AKlump\LoftDataGrids\YAMLFrontMatterImporter implements ImporterInterface
Expanded class hierarchy of YAMLFrontMatterImporter
See also
File
- vendor/
aklump/ loft_data_grids/ src/ AKlump/ LoftDataGrids/ YAMLFrontMatterImporter.php, line 17
Namespace
AKlump\LoftDataGridsView source
class YAMLFrontMatterImporter implements ImporterInterface {
protected $settings = array(
'bodyKey' => 'body',
);
public function getInfo() {
$info = array(
'name' => 'YAML Text File with Front Matter Format',
'shortname' => 'YAML + Text',
'description' => 'Import data from a text file with YAML Front Matter.',
);
return $info;
}
public function addSetting($key, $value) {
$this->settings[$key] = $value;
return $this;
}
public function import($string) {
$obj = new ExportData();
$header = $body = null;
$bodyKey = $this->settings['bodyKey'];
$chunk = strtok($string, '---');
while ($chunk !== false) {
$chunk = trim($chunk);
if (is_null($header)) {
try {
$header = Yaml::parse($chunk);
foreach ($header as $key => $item) {
$obj
->add($key, $item);
}
} catch (\Exception $exception) {
$header = false;
$obj
->add($bodyKey, $chunk);
}
}
elseif (is_null($body)) {
$obj
->add($bodyKey, $chunk);
}
else {
break;
}
$chunk = strtok('---');
}
// Give the body a default empty string.
if (!in_array($bodyKey, $obj
->getKeys())) {
$obj
->add($bodyKey, '');
}
return $obj;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
YAMLFrontMatterImporter:: |
protected | property | ||
YAMLFrontMatterImporter:: |
public | function |
Adds/Updates a single setting by name. Overrides ImporterInterface:: |
|
YAMLFrontMatterImporter:: |
public | function |
Return info about this class Overrides ImporterInterface:: |
|
YAMLFrontMatterImporter:: |
public | function |
Import data from a format returning and ExportData object. Overrides ImporterInterface:: |