From 801250362a22c30f3bffc8c506adfa09791d4218 Mon Sep 17 00:00:00 2001 From: Diego Pino Navarro Date: Mon, 20 Sep 2021 15:22:13 -0400 Subject: [PATCH] ISSUE-132: Respect Result Count (#133) * Respect Result Count Slices the array if the result count > $count. Cache already uses results in its Bin naming so we are good there. --- src/Controller/AuthAutocompleteController.php | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/Controller/AuthAutocompleteController.php b/src/Controller/AuthAutocompleteController.php index 7651ef3..d4c9011 100644 --- a/src/Controller/AuthAutocompleteController.php +++ b/src/Controller/AuthAutocompleteController.php @@ -147,7 +147,7 @@ public static function create(ContainerInterface $container) { * * @return \Symfony\Component\HttpFoundation\JsonResponse */ - public function handleAutocomplete(Request $request, $auth_type, $vocab = 'subjects', $rdftype = NULL, $count) { + public function handleAutocomplete(Request $request, $auth_type, $vocab = 'subjects', $rdftype = NULL, $count = 10) { $results = []; @@ -219,7 +219,13 @@ public function handleAutocomplete(Request $request, $auth_type, $vocab = 'subje } } // DO not cache NULL or FALSE. Those will be 401/403/500; - if ($results) { + if ($results && is_array($results)) { + // Cut the results to the desired number + // Easier than dealing with EACH API's custom return options + if (count($results) > $count) { + $results = array_slice($results, 0, $count); + } + //setting cache for anonymous or logged in if (!$is_internal) { $this->cacheSet($cache_id, $results, @@ -231,7 +237,9 @@ public function handleAutocomplete(Request $request, $auth_type, $vocab = 'subje $this->cacheSet($cache_id, $results, ($this->time->getRequestTime() + static::MAX_CACHE_AGE)); } } - $results = $results ?? []; + else { + $results = []; + } return new JsonResponse($results); } @@ -293,6 +301,7 @@ protected function loc($input, $vocab, $rdftype) { $baseurl = 'https://id.loc.gov'; $remoteUrl = $baseurl . $urlindex; + $options['headers'] = ['Accept' => 'application/json']; $body = $this->getRemoteJsonData($remoteUrl, $options); @@ -412,7 +421,6 @@ protected function getty($input, $vocab = 'aat', $mode = 'fuzzy') { return $results; } - $input = trim($input); $queries = []; $results = []; @@ -913,7 +921,6 @@ protected function mesh($input, $vocab, $rdftype) { $remoteUrl = $baseurl . $urlindex; $options['headers'] = ['Accept' => 'application/json']; $body = $this->getRemoteJsonData($remoteUrl, $options); - $results = []; $jsondata = json_decode($body, TRUE); $json_error = json_last_error();