You are here

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

Expanded class hierarchy of YAMLFrontMatterImporter

See also

YAMLFrontMatterExporter

File

vendor/aklump/loft_data_grids/src/AKlump/LoftDataGrids/YAMLFrontMatterImporter.php, line 17

Namespace

AKlump\LoftDataGrids
View 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

Namesort descending Modifiers Type Description Overrides
YAMLFrontMatterImporter::$settings protected property
YAMLFrontMatterImporter::addSetting public function Adds/Updates a single setting by name. Overrides ImporterInterface::addSetting
YAMLFrontMatterImporter::getInfo public function Return info about this class Overrides ImporterInterface::getInfo
YAMLFrontMatterImporter::import public function Import data from a format returning and ExportData object. Overrides ImporterInterface::import