/**
  * __invoke Magic Method
  *
  * @param Request  $request  Request
  * @param Response $response Response
  * @param callable $next     Next
  *
  * @return Response Response
  */
 public function __invoke(Request $request, Response $response, callable $next)
 {
     try {
         $response = $next($request, $response);
         $response = $this->applyFractal($request, $response);
     } catch (HttpMessages_Exception $exception) {
         $body = ['message' => $exception->getMessage()];
         if ($exception->getErrors()) {
             $body['errors'] = $exception->getErrors();
         }
         if ($exception->getInput()) {
             $body['input'] = $exception->getInput();
         }
         if ($this->config->get('devMode')) {
             $body['trace'] = $exception->getTrace();
         }
         $json = JsonHelper::encode($body);
         $response = $response->withStatus($exception->getStatusCode(), $exception->getStatusPhrase())->withJson($json);
     } catch (\CException $exception) {
         $body = ['message' => $exception->getMessage()];
         if ($this->config->get('devMode')) {
             $body['trace'] = $exception->getTrace();
         }
         $json = JsonHelper::encode($body);
         $response = $response->withStatus(500)->withJson($json);
     }
     return $response;
 }
 /**
  * Get the url for the given page.
  *
  * @param int $page
  *
  * @return string
  */
 public function getUrl($page)
 {
     $pagination_base_url = $this->config->get('paginationBaseUrl');
     $page_trigger = $this->config->get('paginationParameter');
     return $pagination_base_url . '?' . http_build_query(array_merge(craft()->request->getQuery(), [$page_trigger => $page]));
 }