From 7581f5155545e027491822e0c950a9a0b18a20b9 Mon Sep 17 00:00:00 2001 From: Tom Udding Date: Tue, 26 Sep 2023 20:55:13 +0200 Subject: [PATCH] Allow searching for decision using the English abbreviations for meetings --- module/Decision/src/Mapper/Decision.php | 11 +++---- .../Decision/src/Model/Enums/MeetingTypes.php | 32 +++++++++++++++++++ 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/module/Decision/src/Mapper/Decision.php b/module/Decision/src/Mapper/Decision.php index 9d733cc834..dca01c6f94 100644 --- a/module/Decision/src/Mapper/Decision.php +++ b/module/Decision/src/Mapper/Decision.php @@ -9,6 +9,7 @@ use Decision\Model\Enums\MeetingTypes; use function addcslashes; +use function implode; use function is_numeric; use function preg_match; @@ -36,7 +37,7 @@ public function search(string $query): array $qb->setParameter('query', '%' . addcslashes($query, '%_') . '%'); // Start by matching meeting type and meeting number, then we also match additional meeting points and decision - // numbers. + // numbers. Both the Dutch and English abbreviation for the meeting types can be used. // // To make it usable, we also split the meeting type and meeting number match into two separate capture groups. // In total there are four capture groups. @@ -52,13 +53,11 @@ public function search(string $query): array // [3]=> string(3) "456" // [4]=> string(3) "789" // } - $meetingRegex = '/(?:(' . MeetingTypes::ALV->value . '|' - . MeetingTypes::BV->value . '|' - . MeetingTypes::VV->value . '|' - . MeetingTypes::VIRT->value . ') ([0-9]+))(?:.([0-9]+))?(?:.([0-9]+))?/'; + $meetingRegex = '/(?:(' . implode('|', MeetingTypes::getSearchableStrings()) . ')' + . ' ([0-9]+))(?:.([0-9]+))?(?:.([0-9]+))?/'; $meetingInfo = []; if (1 === preg_match($meetingRegex, $query, $meetingInfo, PREG_UNMATCHED_AS_NULL)) { - $meetingType = MeetingTypes::from($meetingInfo[1]); + $meetingType = MeetingTypes::tryFromSearch($meetingInfo[1]); $meetingNumber = (int) $meetingInfo[2]; $where = 'd.meeting_type = :meeting_type AND d.meeting_number = :meeting_number'; diff --git a/module/Decision/src/Model/Enums/MeetingTypes.php b/module/Decision/src/Model/Enums/MeetingTypes.php index 68ee6b7a7a..14618901dc 100644 --- a/module/Decision/src/Model/Enums/MeetingTypes.php +++ b/module/Decision/src/Model/Enums/MeetingTypes.php @@ -4,8 +4,11 @@ namespace Decision\Model\Enums; +use InvalidArgumentException; use Laminas\Mvc\I18n\Translator; +use function array_column; + /** * Enum for the different address types. */ @@ -35,4 +38,33 @@ public function getAbbreviation(Translator $translator): string self::VIRT => $translator->translate('VIRT'), }; } + + /** + * @return string[] + */ + public static function getSearchableStrings(): array + { + return [ + ...array_column(self::cases(), 'value'), + 'GMM', + 'BM', + 'CM', + ]; + } + + public static function tryFromSearch(string $input): MeetingTypes + { + $value = self::tryFrom($input); + + if (null !== $value) { + return $value; + } + + return match ($input) { + 'GMM' => MeetingTypes::ALV, + 'BM' => MeetingTypes::BV, + 'CM' => MeetingTypes::VV, + default => throw new InvalidArgumentException('MeetingType is not recognized'), + }; + } }