vendor/pagerfanta/pagerfanta/lib/Twig/Extension/PagerfantaRuntime.php line 41

Open in your IDE?
  1. <?php
  2. namespace Pagerfanta\Twig\Extension;
  3. use Pagerfanta\Exception\OutOfRangeCurrentPageException;
  4. use Pagerfanta\PagerfantaInterface;
  5. use Pagerfanta\RouteGenerator\RouteGeneratorFactoryInterface;
  6. use Pagerfanta\RouteGenerator\RouteGeneratorInterface;
  7. use Pagerfanta\View\ViewFactoryInterface;
  8. use Twig\Extension\RuntimeExtensionInterface;
  9. final class PagerfantaRuntime implements RuntimeExtensionInterface
  10. {
  11.     /**
  12.      * @var string
  13.      */
  14.     private $defaultView;
  15.     /**
  16.      * @var ViewFactoryInterface
  17.      */
  18.     private $viewFactory;
  19.     /**
  20.      * @var RouteGeneratorFactoryInterface
  21.      */
  22.     private $routeGeneratorFactory;
  23.     public function __construct(string $defaultViewViewFactoryInterface $viewFactoryRouteGeneratorFactoryInterface $routeGeneratorFactory)
  24.     {
  25.         $this->defaultView $defaultView;
  26.         $this->viewFactory $viewFactory;
  27.         $this->routeGeneratorFactory $routeGeneratorFactory;
  28.     }
  29.     /**
  30.      * @param string|array|null $viewName the view name
  31.      *
  32.      * @throws \InvalidArgumentException if the $viewName argument is an invalid type
  33.      */
  34.     public function renderPagerfanta(PagerfantaInterface $pagerfanta$viewName null, array $options = []): string
  35.     {
  36.         if (\is_array($viewName)) {
  37.             [$viewName$options] = [null$viewName];
  38.         } elseif (null !== $viewName && !\is_string($viewName)) {
  39.             throw new \InvalidArgumentException(sprintf('The $viewName argument of %s() must be an array, a string, or a null value; %s given.'__METHOD__get_debug_type($viewName)));
  40.         }
  41.         $viewName $viewName ?: $this->defaultView;
  42.         return $this->viewFactory->get($viewName)->render($pagerfanta$this->createRouteGenerator($options), $options);
  43.     }
  44.     /**
  45.      * @throws OutOfRangeCurrentPageException if the page is out of bounds
  46.      */
  47.     public function getPageUrl(PagerfantaInterface $pagerfantaint $page, array $options = []): string
  48.     {
  49.         if ($page || $page $pagerfanta->getNbPages()) {
  50.             throw new OutOfRangeCurrentPageException("Page '{$page}' is out of bounds");
  51.         }
  52.         $routeGenerator $this->createRouteGenerator($options);
  53.         return $routeGenerator($page);
  54.     }
  55.     private function createRouteGenerator(array $options = []): RouteGeneratorInterface
  56.     {
  57.         return $this->routeGeneratorFactory->create($options);
  58.     }
  59. }