Skip to content

Commit

Permalink
ISSUE-132: Respect Result Count (#133)
Browse files Browse the repository at this point in the history
* Respect Result Count

Slices the array if the result count >  $count. Cache already uses results in its Bin naming so we are good there.
  • Loading branch information
DiegoPino committed Sep 20, 2021
1 parent 2d498fe commit 8012503
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/Controller/AuthAutocompleteController.php
Expand Up @@ -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 = [];


Expand Down Expand Up @@ -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,
Expand All @@ -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);
}

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -412,7 +421,6 @@ protected function getty($input, $vocab = 'aat', $mode = 'fuzzy') {
return $results;
}


$input = trim($input);
$queries = [];
$results = [];
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 8012503

Please sign in to comment.