You are here

public function YAMLFrontMatterImporter::import in Loft Data Grids 7.2

Import data from a format returning and ExportData object.

Parameters

string $string The data as returned by an ExporterClass::export().:

Return value

mixed

Overrides ImporterInterface::import

File

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

Class

YAMLFrontMatterImporter
Class YAMLFrontMatterImporter

Namespace

AKlump\LoftDataGrids

Code

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;
}