public function RISEncoder::decode in Bibliography & Citation 8
Same name and namespace in other branches
- 2.0.x modules/bibcite_ris/src/Encoder/RISEncoder.php \Drupal\bibcite_ris\Encoder\RISEncoder::decode()
File
- modules/
bibcite_ris/ src/ Encoder/ RISEncoder.php, line 32
Class
- RISEncoder
- RIS format encoder.
Namespace
Drupal\bibcite_ris\EncoderCode
public function decode($data, $format, array $context = []) {
/*
* Workaround for weird behavior of "LibRIS" library.
*
* Replace LF line ends by CRLF.
*/
$data = str_replace("\n", "\r\n", $data);
$config = \Drupal::config('bibcite_entity.mapping.' . $format);
$fields = $config
->get('fields');
$ris = new RISReader();
$ris
->parseString($data);
$records = $ris
->getRecords();
// Workaround for weird behavior of "LibRIS" library.
foreach ($records as &$record) {
foreach ($record as $key => $value) {
if (is_array($value) && count($value) == 1) {
$record[$key] = reset($value);
}
}
// Additional pages parsing.
$pages_string = '';
if (array_key_exists('SP', $record) || array_key_exists('EP', $record)) {
if (array_key_exists('SP', $record)) {
$record['SP'] = (array) $record['SP'];
}
if (array_key_exists('EP', $record)) {
$record['EP'] = (array) $record['EP'];
}
$max_sp = array_key_exists('SP', $record) ? max($record['SP']) : NULL;
$max_ep = array_key_exists('EP', $record) ? max($record['EP']) : 0;
if ($max_sp && $max_sp > $max_ep) {
$pages_string .= $max_sp . '+';
array_splice($record['SP'], array_search($max_sp, $record['SP']), 1);
}
while ($max_ep) {
$pages_string = $max_ep . ', ' . $pages_string;
array_splice($record['EP'], array_search($max_ep, $record['EP']), 1);
$max_ep = count($record['EP']) > 0 ? max($record['EP']) : NULL;
$max_sp = array_key_exists('SP', $record) ? max($record['SP']) : NULL;
if ($max_sp && (!$max_ep || $max_sp > $max_ep)) {
$pages_string = $max_sp . '-' . $pages_string;
array_splice($record['SP'], array_search($max_sp, $record['SP']), 1);
}
}
$record['SP'] = $pages_string;
$record['EP'] = $pages_string;
}
// From old format import fix.
// User fields to custom.
if (isset($record['U1']) && !isset($fields['U1']) && !isset($records['C1'])) {
$record['C1'] = $record['U1'];
unset($record['U1']);
}
if (isset($record['U2']) && !isset($fields['U2']) && !isset($records['C2'])) {
$record['C2'] = $record['U2'];
unset($record['U2']);
}
if (isset($record['U3']) && !isset($fields['U3']) && !isset($records['C3'])) {
$record['C3'] = $record['U3'];
unset($record['U3']);
}
if (isset($record['U4']) && !isset($fields['U4']) && !isset($records['C4'])) {
$record['C4'] = $record['U4'];
unset($record['U4']);
}
if (isset($record['U5']) && !isset($fields['U5']) && !isset($records['C5'])) {
$record['C5'] = $record['U5'];
unset($record['U5']);
}
// Year of publication.
if (isset($record['Y1']) && !isset($fields['Y1']) && !isset($records['PY'])) {
$record['PY'] = $record['Y1'];
unset($record['Y1']);
}
// Titles.
if ($this
->checkKeys([
'TI',
'T1',
'ST',
'CT',
'BT',
], $record)) {
$title = $record['TI'];
if ($title === $record['T1'] && $title === $record['ST'] && $title === $record['CT'] && $title === $record['BT']) {
unset($record['T1']);
unset($record['ST']);
unset($record['CT']);
unset($record['BT']);
}
}
// Issue.
if ($this
->checkKeys([
'CP',
'IS',
], $record) && $record['CP'] === $record['IS']) {
unset($record['CP']);
}
// Short title.
if ($this
->checkKeys([
'J1',
'J2',
'JO',
], $record) && $record['J1'] === $record['J2'] && $record['J1'] === $record['JO']) {
$record['ST'] = $record['J1'];
unset($record['J1']);
unset($record['J2']);
unset($record['JO']);
}
// Secondary title.
if ($this
->checkKeys([
'JA',
'JF',
'T2',
], $record) && $record['JA'] === $record['JF'] && $record['JA'] === $record['T2']) {
unset($record['JA']);
unset($record['JF']);
}
// Abstract.
if ($this
->checkKeys([
'AB',
'N2',
], $record) && $record['AB'] && $record['AB'] === $record['N2']) {
unset($record['N2']);
}
}
if (count($records) === 0) {
$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.");
}
return $records;
}