bddRuleset = $ruleset; $this->bddEvaluator = new BddEvaluator( $ruleset, new BddResultResolver($ruleset) ); } elseif ($ruleset instanceof Ruleset) { $this->ruleset = $ruleset; } elseif (is_array($ruleset)) { $this->ruleset = new Ruleset($ruleset, $partitions); } else { throw new \InvalidArgumentException( 'EndpointProviderV2 expects an array, Ruleset, or BddRuleset' . ' but received ' . (is_object($ruleset) ? get_class($ruleset) : gettype($ruleset)) ); } $this->cache = new LruArrayCache(100); } /** * Returns the parsed tree ruleset for services using the legacy * `endpointRuleSet` trait. Returns null when the provider was built from * an `endpointBdd` trait. * * @return Ruleset|null */ public function getRuleset() { return $this->ruleset; } /** * Returns the parsed BDD ruleset for services using the `endpointBdd` * trait, or null when the provider was built from a tree ruleset. */ public function getBddRuleset(): ?BddRuleset { return $this->bddRuleset; } /** * Given input parameters, determines the correct endpoint or an error * to be thrown for a given request. * * @return RulesetEndpoint * @throws UnresolvedEndpointException */ public function resolveEndpoint(array $inputParameters) { $hashedParams = $this->hashInputParameters($inputParameters); $match = $this->cache->get($hashedParams); if (!is_null($match)) { return $match; } $endpoint = $this->bddEvaluator !== null ? $this->bddEvaluator->evaluate($inputParameters) : $this->ruleset->evaluate($inputParameters); // This condition just applies to endpoint resolution // through the decision tree evaluation process. if ($endpoint === false) { throw new UnresolvedEndpointException( 'Unable to resolve an endpoint using the provider arguments: ' . json_encode($inputParameters) ); } $this->cache->set($hashedParams, $endpoint); return $endpoint; } private function hashInputParameters($inputParameters) { return md5(serialize($inputParameters)); } /** * @return array */ public function getActiveParameters(): array { if ($this->bddRuleset !== null) { return $this->bddRuleset->getParameters(); } return $this->ruleset->getParameters(); } }