Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FDR 399: Name errors #66

Merged
merged 11 commits into from
Feb 1, 2024
6 changes: 3 additions & 3 deletions src/Form/SelectCslForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
use Drupal\path_alias\AliasManagerInterface;
use Drupal\islandora_citations\IslandoraCitationsHelper;
use Drupal\path_alias\AliasManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

Expand Down Expand Up @@ -136,7 +136,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
];

// Log error message.
$this->logger->error($csl);
$this->logger->error(json_encode($csl));

return $form;
}
Expand Down Expand Up @@ -225,7 +225,7 @@ public function getDefaultCitation($csl_name) {
try {
// Method call to render citation.
$rendered = $this->renderCitation($csl_name);
return $rendered['data'];
return $rendered['data'] ?? NULL;
}
catch (\Throwable $e) {
return $e->getMessage();
Expand Down
66 changes: 49 additions & 17 deletions src/Normalizer/NormalizerBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,28 +48,60 @@ protected function formatDateVariables($date): array {

/**
* Formats name variables as per csl json.
*
* @throws \Exception
*/
protected function formatNameVariables($name, $type): array {
switch ($type) {
case 'person':
if (strpos($name, ',') !== FALSE) {
$names = explode(',', $name);
return is_array($names) ?
['family' => $names[1], 'given' => $names[0]] :
['family' => $name];
}
else {
$names = explode(' ', $name);
return is_array($names) ?
['given' => $names[1], 'family' => $names[0]] :
['family' => $name];
}
return match ($type) {
'person' => $this->getNameParts($name),
'institution' => ['family' => $name],
default => ['family' => $name],
};

case 'institution':
return ['family' => $name];
}

/**
* Gets the first name and last name from name.
*
* @param string $name
* The person's name.
*
* @return array
* An array of name parts.
*
* @throws \Exception
*/
protected function getNameParts(string $name): array {
try {
// If name has a comma, we assume that it's
// formatted as Lastname,Firstname
// This is kind of dicey, names might just contain commas.
// @todo but that's an edge case which can be handled when
// encountered.
if (str_contains($name, ',')) {
$nameParts = explode(',', $name);
$firstName = $nameParts[1] ?? '';
$lastName = $nameParts[0] ?? '';
}
else {
$name = trim($name);
$lastName = (!str_contains($name, ' ')) ? '' : preg_replace('#.*\s([\w-]*)$#', '$1', $name);
$firstName = trim(preg_replace('#' . preg_quote($lastName, '#') . '#', '', $name));
}

if (empty($firstName) || empty($lastName)) {
throw new \Exception('Name is not formatted properly');
}

return [
'given' => $firstName,
'family' => $lastName,
];
}
catch (\Exception $e) {
return ['family' => $name];
}

return $name;
}

}
Loading