diff --git a/src/Metadata/Collection/TypeCollection.php b/src/Metadata/Collection/TypeCollection.php index bfbf490..1a30c7d 100644 --- a/src/Metadata/Collection/TypeCollection.php +++ b/src/Metadata/Collection/TypeCollection.php @@ -9,6 +9,8 @@ use IteratorAggregate; use Soap\Engine\Exception\MetadataException; use Soap\Engine\Metadata\Model\Type; +use function array_key_exists; +use function Psl\Dict\reindex; /** * @implements IteratorAggregate, Type> @@ -20,12 +22,24 @@ final class TypeCollection implements Countable, IteratorAggregate */ private array $types; + /** + * @var array + */ + private array $qualifiedLookup; + /** * @no-named-arguments */ public function __construct(Type ...$types) { $this->types = $types; + $this->qualifiedLookup = reindex( + $types, + static fn (Type $type): string => self::createLookupKey( + $type->getXsdType()->getName(), + $type->getXsdType()->getXmlNamespace() + ) + ); } /** @@ -99,12 +113,16 @@ public function fetchFirstByName(string $name): Type */ public function fetchByNameAndXmlNamespace(string $name, string $namespace): Type { - foreach ($this->types as $type) { - if ($name === $type->getName() && $namespace === $type->getXsdType()->getXmlNamespace()) { - return $type; - } + $lookupKey = self::createLookupKey($name, $namespace); + if (!array_key_exists($lookupKey, $this->qualifiedLookup)) { + throw MetadataException::typeNotFound($name); } - throw MetadataException::typeNotFound($name); + return $this->qualifiedLookup[$lookupKey]; + } + + private static function createLookupKey(string $name, string $namespace): string + { + return $namespace . ':' . $name; } }