/** * @desc Validates fields and executes the provided method * * @param $method - Search method name to be called * @param array $fields = array of required request field names * @param bool $pagination - get pagination values from request * @throws NotFoundApiException * @throws InvalidParameterApiException */ private function executeSearch($method, array $fields = [], $pagination = false) { $searchParams = new LyricsApiSearchParams(); // Validate fields foreach ($fields as $fieldName) { $fieldValue = $this->wg->Request->getVal($fieldName); if (empty($fieldValue)) { throw new InvalidParameterApiException($fieldName); } $searchParams->addField($fieldName, $fieldValue); } // Get pagination if ($pagination) { list($limit, $offset) = $this->getPaginationParamsFromRequest(); $searchParams->setLimit($limit); $searchParams->setOffset($offset); } $this->response->setFormat(WikiaResponse::FORMAT_JSON); $results = $this->lyricsApiHandler->{$method}($searchParams); // Validate result if (is_null($results)) { // New songs that we don't have yet are likely to get hit repeatedly by a // ton of users until we get the song, so this is a special case where it // is helpful to cache 404s for a little while. $this->response->setCacheValidity(WikiaResponse::CACHE_VERY_SHORT); throw new NotFoundApiException($this->getNotFoundDetails($method)); } $this->response->setVal('result', $results); $this->response->setCacheValidity(WikiaResponse::CACHE_STANDARD); }