vendor/babdev/pagerfanta-bundle/View/TranslatedView.php line 38

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace BabDev\PagerfantaBundle\View;
  3. use Pagerfanta\PagerfantaInterface;
  4. use Pagerfanta\Twig\View\TwigView;
  5. use Pagerfanta\View\ViewInterface;
  6. use Symfony\Component\Translation\TranslatorInterface as LegacyTranslatorInterface;
  7. use Symfony\Contracts\Translation\TranslatorInterface;
  8. /**
  9.  * @deprecated to be removed in BabDevPagerfantaBundle 3.0. Use the Twig view class instead.
  10.  */
  11. abstract class TranslatedView implements ViewInterface
  12. {
  13.     /**
  14.      * @var ViewInterface
  15.      */
  16.     private $view;
  17.     /**
  18.      * @var LegacyTranslatorInterface|TranslatorInterface
  19.      */
  20.     private $translator;
  21.     public function __construct(ViewInterface $viewobject $translator)
  22.     {
  23.         if (!($translator instanceof LegacyTranslatorInterface) && !($translator instanceof TranslatorInterface)) {
  24.             throw new \InvalidArgumentException(sprintf('The $translator argument of %s must be an instance of %s or %s, a %s was given.', static::class, LegacyTranslatorInterface::class, TranslatorInterface::class, \get_class($translator)));
  25.         }
  26.         $this->view $view;
  27.         $this->translator $translator;
  28.     }
  29.     public function render(PagerfantaInterface $pagerfanta$routeGenerator, array $options = [])
  30.     {
  31.         trigger_deprecation('babdev/pagerfanta-bundle''2.2''The "%s" class is deprecated and will be removed in 3.0. Use the "%s" class instead.', static::class, TwigView::class);
  32.         $optionsWithTranslations $this->addTranslationOptions($options);
  33.         return $this->view->render($pagerfanta$routeGenerator$optionsWithTranslations);
  34.     }
  35.     private function addTranslationOptions($options)
  36.     {
  37.         return $this->addNextTranslationOption(
  38.             $this->addPreviousTranslationOption($options)
  39.         );
  40.     }
  41.     private function addPreviousTranslationOption($options)
  42.     {
  43.         return $this->addTranslationOption($options$this->previousMessageOption(), 'previousMessage');
  44.     }
  45.     private function addNextTranslationOption($options)
  46.     {
  47.         return $this->addTranslationOption($options$this->nextMessageOption(), 'nextMessage');
  48.     }
  49.     private function addTranslationOption($options$option$messageMethod)
  50.     {
  51.         if (isset($options[$option])) {
  52.             return $options;
  53.         }
  54.         $message $this->$messageMethod();
  55.         return array_merge($options, [$option => $message]);
  56.     }
  57.     abstract protected function previousMessageOption();
  58.     abstract protected function nextMessageOption();
  59.     private function previousMessage()
  60.     {
  61.         $previousText $this->previousText();
  62.         return $this->buildPreviousMessage($previousText);
  63.     }
  64.     private function nextMessage()
  65.     {
  66.         $nextText $this->nextText();
  67.         return $this->buildNextMessage($nextText);
  68.     }
  69.     private function previousText()
  70.     {
  71.         return $this->translator->trans('previous', [], 'pagerfanta');
  72.     }
  73.     private function nextText()
  74.     {
  75.         return $this->translator->trans('next', [], 'pagerfanta');
  76.     }
  77.     abstract protected function buildPreviousMessage($text);
  78.     abstract protected function buildNextMessage($text);
  79. }