You are here

public function MarcEncoder::decode in Bibliography & Citation 2.0.x

Same name and namespace in other branches
  1. 8 modules/bibcite_marc/src/Encoder/MarcEncoder.php \Drupal\bibcite_marc\Encoder\MarcEncoder::decode()

File

modules/bibcite_marc/src/Encoder/MarcEncoder.php, line 34

Class

MarcEncoder
Marc format encoder.

Namespace

Drupal\bibcite_marc\Encoder

Code

public function decode($data, $format, array $context = []) {
  $parsed = [];
  $records = explode(File::END_OF_RECORD, $data);
  foreach ($records as $record) {
    if (strlen($record) > 0) {
      $rec = $this
        ->decodeFile($record . File::END_OF_RECORD);
      $fields = $rec
        ->fields();
      $leader = $rec
        ->leader();
      $pubtype = $leader[6];
      $pubtype .= $leader[7];
      $fields['type'] = $pubtype === 'am' ? 'book' : 'misc';
      $parsed[] = $fields;
    }
  }
  $keys = array_keys($parsed);
  if (count($keys) === 0 || $keys[0] === -1) {
    $format_definition = \Drupal::service('plugin.manager.bibcite_format')
      ->getDefinition($format);
    $format_label = $format_definition['label'];
    throw new UnexpectedValueException("Incorrect '{$format_label}' format or empty set.");
  }
  $this
    ->processEntries($parsed);
  return $parsed;
}