You are here

public function EndnoteEncoder::encode in Bibliography & Citation 8

Same name and namespace in other branches
  1. 2.0.x modules/bibcite_endnote/src/Encoder/EndnoteEncoder.php \Drupal\bibcite_endnote\Encoder\EndnoteEncoder::encode()

File

modules/bibcite_endnote/src/Encoder/EndnoteEncoder.php, line 161

Class

EndnoteEncoder
Endnote format encoder.

Namespace

Drupal\bibcite_endnote\Encoder

Code

public function encode($data, $format, array $context = []) {
  if (isset($data['type'])) {
    $data = [
      $data,
    ];
  }
  if ($format === 'tagged') {
    return $this
      ->encodeTagged($data);
  }
  $sxml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><xml><records></records></xml>');
  $records = $sxml->records;
  foreach ($data as $id => $ref) {
    if ($records instanceof SimpleXMLElement) {
      switch ($format) {
        case 'endnote7':
          $record = $records
            ->addChild('RECORD');
          $contrib_key = 'CONTRIBUTORS';
          $author_key = 'AUTHOR';
          $titles_key = 'TITLES';
          $keywords_key = 'KEYWORDS';
          $dates_key = 'DATES';
          $web_key = 'WEB-URLS';
          $pub_key = 'PUB-DATES';
          $keyword_key = 'KEYWORD';
          $authors_key = 'AUTHORS';
          break;
        case 'endnote8':
        default:
          $record = $records
            ->addChild('record');
          $contrib_key = 'contributors';
          $author_key = 'author';
          $titles_key = 'titles';
          $keywords_key = 'keywords';
          $dates_key = 'dates';
          $web_key = 'web-urls';
          $pub_key = 'pub-dates';
          $keyword_key = 'keyword';
          $authors_key = 'authors';
          break;
      }
      $source = $record
        ->addChild('source-app', 'Drupal-Bibcite');
      $source
        ->addAttribute('name', 'Bibcite');
      $source
        ->addAttribute('version', '8.x');
      $config = \Drupal::config('bibcite_entity.mapping.' . $format);
      $indexes = $config
        ->get('fields');
      $ref['type_en8id'] = $config
        ->get('indexes')[$ref['type']];
      $type_key = array_search('type', $indexes);
      if ($type_key) {
        $record
          ->addChild($type_key, $ref['type_en8id']);
      }
      unset($ref[$type_key]);
      unset($ref['type']);
      unset($ref['type_en8id']);
      unset($ref['reference']);
      if ($authors_key) {
        $authors = $record
          ->addChild($contrib_key)
          ->addChild($authors_key);

        // Hotfix: contributorKey property is hardcoded in the
        // bibcite_endnote.services.xml. Access authors by this key
        // explicitly for the moment, mapping is not working anyway.
        // @todo Rework this place when mapping of authors is fixed and flexible.
        if (isset($ref['authors'])) {
          foreach ($ref['authors'] as $author) {
            $author_xml = $authors
              ->addChild($author_key);
            $this
              ->setStyledText($author_xml, $author);
          }
          unset($ref['authors']);
        }
      }
      $titles = $record
        ->addChild($titles_key);
      $this
        ->addTitles($titles, $ref, $indexes);
      $keywords = $record
        ->addChild($keywords_key);
      $this
        ->addKeywords($keywords, $ref, $indexes, $keyword_key);
      unset($ref[$keywords_key]);
      $dates = $record
        ->addChild($dates_key);
      $this
        ->addDates($dates, $ref, $indexes, $pub_key);
      $urls_key = array_search('urls', $indexes);
      if (isset($ref[$urls_key])) {
        $this
          ->addTag($record
          ->addChild($web_key), $urls_key, $ref[$urls_key]);
        unset($ref[$urls_key]);
      }

      // Only in endnote X3.
      if (isset($ref['full-title'])) {
        $this
          ->addTag($record
          ->addChild('periodical'), 'full-title', $ref['full-title']);
        unset($ref['full-title']);
      }
      $this
        ->addFields($record, $ref);
    }
  }
  return $sxml
    ->asXML();
}