From 822feba7bba07deecbc4803c4abadc1341692356 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcus=20F=C3=B6rster?= Date: Fri, 7 Mar 2025 13:30:12 +0100 Subject: [PATCH] improve/fallback country-lookup #74 --- src/ISO3166.php | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/ISO3166.php b/src/ISO3166.php index efeef76..0788491 100644 --- a/src/ISO3166.php +++ b/src/ISO3166.php @@ -157,12 +157,21 @@ public function getIterator(): \Generator */ private function lookup(string $key, string $value): array { - $value = mb_strtolower($value); + $value = mb_strtolower(mb_trim($value)); + $valueStringLength = mb_strlen($value); + + if(!(bool)$valueStringLength) + { + throw new OutOfBoundsException('Value should not be empty!'); + } foreach ($this->countries as $country) { $comparison = mb_strtolower($country[$key]); - if ($value === $comparison || $value === mb_substr($comparison, 0, mb_strlen($value))) { + if ($value === $comparison + || $value === mb_substr($comparison, 0, $valueStringLength) + || $comparison == mb_substr($value, 0, mb_strlen($comparison)) + ) { return $country; } }