9
9
use IteratorAggregate ;
10
10
use Soap \Engine \Exception \MetadataException ;
11
11
use Soap \Engine \Metadata \Model \Type ;
12
+ use function array_key_exists ;
13
+ use function Psl \Dict \reindex ;
12
14
13
15
/**
14
16
* @implements IteratorAggregate<int<0,max>, Type>
@@ -20,12 +22,24 @@ final class TypeCollection implements Countable, IteratorAggregate
20
22
*/
21
23
private array $ types ;
22
24
25
+ /**
26
+ * @var array<string, Type>
27
+ */
28
+ private array $ qualifiedLookup ;
29
+
23
30
/**
24
31
* @no-named-arguments
25
32
*/
26
33
public function __construct (Type ...$ types )
27
34
{
28
35
$ this ->types = $ types ;
36
+ $ this ->qualifiedLookup = reindex (
37
+ $ types ,
38
+ static fn (Type $ type ): string => self ::createLookupKey (
39
+ $ type ->getXsdType ()->getName (),
40
+ $ type ->getXsdType ()->getXmlNamespace ()
41
+ )
42
+ );
29
43
}
30
44
31
45
/**
@@ -99,12 +113,16 @@ public function fetchFirstByName(string $name): Type
99
113
*/
100
114
public function fetchByNameAndXmlNamespace (string $ name , string $ namespace ): Type
101
115
{
102
- foreach ($ this ->types as $ type ) {
103
- if ($ name === $ type ->getName () && $ namespace === $ type ->getXsdType ()->getXmlNamespace ()) {
104
- return $ type ;
105
- }
116
+ $ lookupKey = self ::createLookupKey ($ name , $ namespace );
117
+ if (!array_key_exists ($ lookupKey , $ this ->qualifiedLookup )) {
118
+ throw MetadataException::typeNotFound ($ name );
106
119
}
107
120
108
- throw MetadataException::typeNotFound ($ name );
121
+ return $ this ->qualifiedLookup [$ lookupKey ];
122
+ }
123
+
124
+ private static function createLookupKey (string $ name , string $ namespace ): string
125
+ {
126
+ return $ namespace . ': ' . $ name ;
109
127
}
110
128
}
0 commit comments