You are here

protected function MarcEncoder::buildEntry 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::buildEntry()

Build Marc entry string.

Parameters

array $data: Array of Marc values.

Return value

string Formatted Marc string.

1 call to MarcEncoder::buildEntry()
MarcEncoder::encode in modules/bibcite_marc/src/Encoder/MarcEncoder.php

File

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

Class

MarcEncoder
Marc format encoder.

Namespace

Drupal\bibcite_marc\Encoder

Code

protected function buildEntry(array $data) {
  $record = new Record();
  $leader = $record
    ->leader();
  $record
    ->append_fields(new Field());
  if ($data['type'] == 'book') {
    $type = 'nam a';
  }
  else {
    $type = 'nas a';
  }
  unset($data['type']);
  unset($data['reference']);
  $record
    ->leader(substr_replace($leader, $type, 5, 5));
  $rec_eight = str_repeat(' ', 40);
  if (isset($data['year'])) {
    $rec_eight = substr_replace($rec_eight, $data['year'], 7, 4);
  }
  if (isset($data['lang'])) {
    $rec_eight = substr_replace($rec_eight, $data['lang'], 35, 3);
  }
  $rec_eight = substr_replace($rec_eight, 'd', 39, 1);
  $field = new Field("008", $rec_eight);
  $record
    ->append_fields($field);
  unset($data['year']);
  unset($data['lang']);
  $config = \Drupal::config('bibcite_entity.mapping.marc');
  $indexes = $config
    ->get('indexes');
  foreach ($data as $key => $value) {
    switch ($key) {
      case 'publisher':
      case 'pub-location':
      case 'date':
        $type = explode('_', $indexes[$key]);
        if ($type) {
          $subfields[$type[3]] = $value;
        }
        unset($data[$key]);
    }
  }
  if (isset($subfields)) {
    $field = new Field(explode('_', $indexes['publisher'])[0], '', '', $subfields);
    $record
      ->append_fields($field);
  }
  foreach ($data as $key => $value) {
    $index = explode('_', $indexes[$key]);
    if ($index) {
      switch ($key) {
        case 'authors':
          foreach ($value as $i => $author) {
            $tag = $i == 0 ? 100 : 700;
            $field = new Field($tag, $index[1], $index[2], [
              $index[3] => $author,
            ]);
            $record
              ->append_fields($field);
          }
          break;
        case 'keywords':
          foreach ($value as $keyword) {
            $field = new Field($index[0], $index[1], $index[2], [
              $index[3] => $keyword,
            ]);
            $record
              ->append_fields($field);
          }
          break;
        default:
          $field = new Field($index[0], $index[1], $index[2], [
            $index[3] => $value,
          ]);
          $record
            ->append_fields($field);
          break;
      }
    }
    unset($data[$key]);
  }
  return $record
    ->raw();
}