resolveNoMatch($inputParameters); } // The serialized `results` array omits the implicit no-match rule, // so defined result index N lives at array offset N - 1. $results = $this->ruleset->getResults(); $result = $results[$resultIndex - 1] ?? null; if ($result === null) { throw new UnresolvedEndpointException(sprintf( 'Endpoint BDD referenced unknown result index %d.', $resultIndex )); } if (isset($result['error'])) { $this->throwError($result, $inputParameters); } if (!isset($result['endpoint'])) { throw new UnresolvedEndpointException( 'Endpoint BDD result is missing an `endpoint` or `error` block.' ); } return $this->buildEndpoint($result['endpoint'], $inputParameters); } /** * @throws UnresolvedEndpointException */ public function resolveNoMatch(array $inputParameters): never { throw new UnresolvedEndpointException( 'Unable to resolve an endpoint using the provider arguments: ' . json_encode($inputParameters) ); } /** * @throws UnresolvedEndpointException */ private function throwError(array $result, array $inputParameters): never { $message = $this->ruleset->standardLibrary->resolveValue( $result['error'], $inputParameters ); throw new UnresolvedEndpointException((string) $message); } private function buildEndpoint(array $endpoint, array $inputParameters): RulesetEndpoint { $library = $this->ruleset->standardLibrary; $url = $library->resolveValue($endpoint['url'], $inputParameters); $properties = isset($endpoint['properties']) ? $this->resolveProperties($endpoint['properties'], $inputParameters, $library) : null; $headers = isset($endpoint['headers']) ? $this->resolveHeaders($endpoint['headers'], $inputParameters, $library) : null; return new RulesetEndpoint($url, $properties, $headers); } private function resolveProperties( $properties, array $inputParameters, RulesetStandardLibrary $library ) { if (is_array($properties)) { $resolved = []; foreach ($properties as $key => $value) { $resolved[$key] = $this->resolveProperties( $value, $inputParameters, $library ); } return $resolved; } // Inline some of the isTemplate check here to avoid unnecessary resolution attempts on simple strings if (is_string($properties) && str_contains($properties, '{') && $library->isTemplate($properties)) { return $library->resolveTemplateString($properties, $inputParameters); } return $properties; } private function resolveHeaders( array $headers, array $inputParameters, RulesetStandardLibrary $library ): array { $resolved = []; foreach ($headers as $name => $values) { $resolvedValues = []; foreach ($values as $value) { $resolvedValues[] = $library->resolveValue($value, $inputParameters); } $resolved[$name] = $resolvedValues; } return $resolved; } }