3 namespace Wallabag\ApiBundle\Controller
;
5 use Hateoas\Configuration\Route
;
6 use Hateoas\Representation\Factory\PagerfantaFactory
;
7 use JMS\Serializer\SerializationContext
;
8 use Nelmio\ApiDocBundle\Annotation\ApiDoc
;
9 use Pagerfanta\Adapter\DoctrineORMAdapter
;
10 use Pagerfanta\Pagerfanta
;
11 use Symfony\Component\HttpFoundation\JsonResponse
;
12 use Symfony\Component\HttpFoundation\Request
;
13 use Symfony\Component\Routing\Generator\UrlGeneratorInterface
;
15 class SearchRestController
extends WallabagRestController
18 * Search all entries by term.
22 * {"name"="term", "dataType"="string", "required"=false, "format"="any", "description"="Any query term"},
23 * {"name"="page", "dataType"="integer", "required"=false, "format"="default '1'", "description"="what page you want."},
24 * {"name"="perPage", "dataType"="integer", "required"=false, "format"="default'30'", "description"="results per page."}
28 * @return JsonResponse
30 public function getSearchAction(Request
$request)
32 $this->validateAuthentication();
34 $term = $request->query
->get('term');
35 $page = (int) $request->query
->get('page', 1);
36 $perPage = (int) $request->query
->get('perPage', 30);
38 $qb = $this->get('wallabag_core.entry_repository')
39 ->getBuilderForSearchByUser(
40 $this->getUser()->getId(),
45 $pagerAdapter = new DoctrineORMAdapter($qb->getQuery(), true, false);
46 $pager = new Pagerfanta($pagerAdapter);
48 $pager->setMaxPerPage($perPage);
49 $pager->setCurrentPage($page);
51 $pagerfantaFactory = new PagerfantaFactory('page', 'perPage');
52 $paginatedCollection = $pagerfantaFactory->createRepresentation(
59 'perPage' => $perPage,
61 UrlGeneratorInterface
::ABSOLUTE_URL
65 return $this->sendResponse($paginatedCollection);
69 * Shortcut to send data serialized in json.
73 * @return JsonResponse
75 private function sendResponse($data)
77 // https://github.com/schmittjoh/JMSSerializerBundle/issues/293
78 $context = new SerializationContext();
79 $context->setSerializeNull(true);
81 $json = $this->get('jms_serializer')->serialize($data, 'json', $context);
83 return (new JsonResponse())->setJson($json);