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; } }