From f9d36478f6f7f84826e1b3879661156e9a9f3e4f Mon Sep 17 00:00:00 2001 From: Akanksha Date: Thu, 1 Feb 2024 17:19:51 +0530 Subject: [PATCH] FDR-399: Switch to regex --- src/Form/SelectCslForm.php | 2 +- src/Normalizer/NormalizerBase.php | 90 +++++++++++++------------------ 2 files changed, 39 insertions(+), 53 deletions(-) diff --git a/src/Form/SelectCslForm.php b/src/Form/SelectCslForm.php index 869e60c..c895662 100644 --- a/src/Form/SelectCslForm.php +++ b/src/Form/SelectCslForm.php @@ -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; diff --git a/src/Normalizer/NormalizerBase.php b/src/Normalizer/NormalizerBase.php index 3586ae4..b55f068 100644 --- a/src/Normalizer/NormalizerBase.php +++ b/src/Normalizer/NormalizerBase.php @@ -48,74 +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': - $name_values = strpos($name, ',') !== FALSE ? - $this->getFirstNameAndLastName($name, ',') : - $this->getFirstNameAndLastName($name); - - return !empty($name_values) ? $name_values : ['family' => $name]; + return match ($type) { + 'person' => $this->getNameParts($name), + 'institution' => ['family' => $name], + default => ['family' => $name], + }; - case 'institution': - return ['family' => $name]; - } - - return $name; } /** - * Get the first name and last name from name. + * Gets the first name and last name from name. * * @param string $name - * The Person name. - * @param string $separator - * The separator used to explode name. + * The person's name. + * + * @return array + * An array of name parts. + * + * @throws \Exception */ - protected function getFirstNameAndLastName($name, $separator = ' ') { - $names = explode($separator, $name); - $name_index_count = count($names); - - // If the separator is a comma, the last name is the first index. - if ($separator === ',') { - // Handling the case when the name starts with comma. Eg: ,First. - if (!$names[0]) { - return [ - 'family' => trim($names[1]), - ]; - } - - return [ - 'given' => trim($names[1]), - 'family' => trim($names[0]), - ]; - } - else { - $first_name = []; - $last_name = ''; - - if ($name_index_count > 1) { - // The last index in the names array would be considered as the last - // name. - $last_name = $names[$name_index_count - 1]; - - // The rest all make up the first name. - for ($i = 0; $i < $name_index_count - 1; $i++) { - $first_name[] = $names[$i]; - } + 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 { - // Handling the case where only a single word name is provided. - return []; + $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'); } - $first_name = is_array($first_name) ? implode(' ', $first_name) : $first_name; return [ - 'family' => trim($last_name), - 'given' => trim($first_name), + 'given' => $firstName, + 'family' => $lastName, ]; } + catch (\Exception $e) { + return ['family' => $name]; + } + } }