You are here

public function EndnoteEncoder::decode 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::decode()

File

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

Class

EndnoteEncoder
Endnote format encoder.

Namespace

Drupal\bibcite_endnote\Encoder

Code

public function decode($data, $format, array $context = []) {
  if ($format === 'tagged') {
    return $this
      ->decodeTagged($data);
  }
  $result = [];
  try {
    $sxml = new SimpleXMLElement($data);
  } catch (\Exception $ex) {
    $format_definition = \Drupal::service('plugin.manager.bibcite_format')
      ->getDefinition($format);
    $format_label = $format_definition['label'];
    throw new UnexpectedValueException("Incorrect '{$format_label}' format.");
  }
  $records = $sxml->records;
  $config = \Drupal::config('bibcite_entity.mapping.' . $format);
  $indexes = $config
    ->get('indexes');
  if ($records instanceof SimpleXMLElement) {
    foreach ($records
      ->children() as $record) {
      $rec = [];
      if ($record instanceof SimpleXMLElement) {
        foreach ($record
          ->children() as $child) {
          if ($child instanceof SimpleXMLElement) {
            switch ($child
              ->getName()) {
              case 'REFERENCE_TYPE':
              case 'ref-type':
                if (strlen($child
                  ->__toString()) > 0) {
                  $type = array_search($child
                    ->__toString(), $indexes);
                  if ($type) {
                    $rec['type'] = $type;
                  }
                  else {
                    $rec['type'] = -1;
                  }
                }
                break;
              case 'DATES':
              case 'dates':
                foreach ($child
                  ->children() as $dates) {
                  if ($dates instanceof SimpleXMLElement) {
                    switch ($dates
                      ->getName()) {
                      case 'YEAR':
                      case 'year':
                        $rec[$dates
                          ->getName()] = $this
                          ->getString($dates);
                        break;
                      case 'DATE':
                      case 'date':
                        $rec[$dates->{'pub-dates'}
                          ->getName()] = $this
                          ->getString($dates->{'pub-dates'});
                        break;
                    }
                  }
                }
                break;
              case 'CONTRIBUTORS':
              case 'contributors':
                foreach ($child
                  ->children() as $authors) {
                  if ($authors instanceof SimpleXMLElement) {
                    foreach ($authors
                      ->children() as $author) {

                      // 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.
                      $rec['authors'][] = $this
                        ->getString($author);
                    }
                  }
                }
                break;
              case 'TITLES':
              case 'titles':
                foreach ($child
                  ->children() as $title) {
                  if ($title instanceof SimpleXMLElement) {
                    $rec[$title
                      ->getName()] = $this
                      ->getString($title);
                  }
                }
                break;
              case 'KEYWORDS':
              case 'keywords':
                foreach ($child
                  ->children() as $keyword) {
                  if ($keyword instanceof SimpleXMLElement) {
                    $rec['keywords'][] = $this
                      ->getString($keyword);
                  }
                }
                break;
              case 'source-app':
                break;
              default:
                $rec[$child
                  ->getName()] = $this
                  ->getString($child);
                break;
            }
          }
        }
      }
      $result[] = $rec;
    }
  }
  return $result;
}