@@ -54,6 +54,16 @@ class ModelMetadata extends AbstractDataValueObject
54
54
*/
55
55
protected array $ supportedOptions ;
56
56
57
+ /**
58
+ * @var array<string, true> Map of supported capabilities for O(1) lookups.
59
+ */
60
+ private array $ capabilitiesMap = [];
61
+
62
+ /**
63
+ * @var array<string, SupportedOption> Map of supported options by name for O(1) lookups.
64
+ */
65
+ private array $ optionsMap = [];
66
+
57
67
/**
58
68
* Constructor.
59
69
*
@@ -80,6 +90,16 @@ public function __construct(string $id, string $name, array $supportedCapabiliti
80
90
$ this ->name = $ name ;
81
91
$ this ->supportedCapabilities = $ supportedCapabilities ;
82
92
$ this ->supportedOptions = $ supportedOptions ;
93
+
94
+ // Build capability map for efficient lookups
95
+ foreach ($ supportedCapabilities as $ capability ) {
96
+ $ this ->capabilitiesMap [$ capability ->value ] = true ;
97
+ }
98
+
99
+ // Build options map for efficient lookups
100
+ foreach ($ supportedOptions as $ option ) {
101
+ $ this ->optionsMap [$ option ->getName ()] = $ option ;
102
+ }
83
103
}
84
104
85
105
/**
@@ -199,22 +219,22 @@ public function toArray(): array
199
219
*/
200
220
public function meetsRequirements (ModelRequirements $ requirements ): bool
201
221
{
202
- // Check if all required capabilities are supported
222
+ // Check if all required capabilities are supported using map lookup
203
223
foreach ($ requirements ->getRequiredCapabilities () as $ requiredCapability ) {
204
- if (!in_array ( $ requiredCapability , $ this ->supportedCapabilities , true )) {
224
+ if (!isset ( $ this ->capabilitiesMap [ $ requiredCapability -> value ] )) {
205
225
return false ;
206
226
}
207
227
}
208
228
209
229
// Check if all required options are supported with the specified values
210
230
foreach ($ requirements ->getRequiredOptions () as $ requiredOption ) {
211
- $ supportedOption = $ this ->findSupportedOption ($ requiredOption ->getName ());
212
-
213
- // If the option is not supported at all, fail
214
- if ($ supportedOption === null ) {
231
+ // Use map lookup instead of linear search
232
+ if (!isset ($ this ->optionsMap [$ requiredOption ->getName ()])) {
215
233
return false ;
216
234
}
217
235
236
+ $ supportedOption = $ this ->optionsMap [$ requiredOption ->getName ()];
237
+
218
238
// Check if the required value is supported by this option
219
239
if (!$ supportedOption ->isSupportedValue ($ requiredOption ->getValue ())) {
220
240
return false ;
@@ -224,24 +244,6 @@ public function meetsRequirements(ModelRequirements $requirements): bool
224
244
return true ;
225
245
}
226
246
227
- /**
228
- * Finds a supported option by name.
229
- *
230
- * @since n.e.x.t
231
- *
232
- * @param string $name The option name to find.
233
- * @return SupportedOption|null The supported option, or null if not found.
234
- */
235
- private function findSupportedOption (string $ name ): ?SupportedOption
236
- {
237
- foreach ($ this ->supportedOptions as $ supportedOption ) {
238
- if ($ supportedOption ->getName () === $ name ) {
239
- return $ supportedOption ;
240
- }
241
- }
242
-
243
- return null ;
244
- }
245
247
246
248
/**
247
249
* {@inheritDoc}
0 commit comments