/**
* Analyzes the request and tries to match an api method.
*
* @param array $classData Class array form compiled cache file.
*
* @return CallbackResult
* @throws RestException
* @throws \Exception
*/
private function matchRequest(&$classData)
{
if (!is_array($classData)) {
throw new RestException("Invalid class cache data.");
}
// build the request url upon which we will do the matching
try {
$url = $this->getUrl();
} catch (\Exception $e) {
throw $e;
}
// get request method
$method = $this->getMethod();
if (!in_array($method, self::$supportedRequestTypes)) {
throw new RestException('Unsupported request method: "' . $method . '"');
}
$callbacks = empty($classData[$method]) ? [] : $classData[$method];
// match array
$matchedMethod = ['methodData' => false, 'matchedParameters' => false];
// validate that we have the ending class name in the url
$classUrl = PathTransformations::classNameToUrl($this->class, $this->normalize);
if (strpos($url, '/' . $classUrl . '/') !== false) {
$matchedMethod = $this->matchMethod($callbacks, $url);
// if method was not matched
if (!$matchedMethod['methodData']) {
// if no method was matched, let's try to match a default method
$matchedMethod = $this->matchDefaultMethod($callbacks, $url, $classUrl);
}
}
$methodData = isset($matchedMethod['methodData']) ? $matchedMethod['methodData'] : false;
$matchedParameters = $matchedMethod['matchedParameters'] ? $matchedMethod['matchedParameters'] : [];
$requestBag = new RequestBag();
$requestBag->setClassData($classData)->setMethodData($methodData)->setMethodParameters($matchedParameters)->setApi($this->api);
$callback = new Callback($requestBag);
return $callback->getCallbackResult();
}