vendor/terminal42/contao-changelanguage/src/Navigation/NavigationItem.php line 143

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Terminal42\ChangeLanguage\Navigation;
  4. use Contao\PageModel;
  5. use Symfony\Component\Routing\Exception\ExceptionInterface;
  6. use Terminal42\ChangeLanguage\Language;
  7. class NavigationItem
  8. {
  9.     private PageModel $rootPage;
  10.     private ?PageModel $targetPage null;
  11.     private string $linkLabel;
  12.     private ?string $ariaLabel null;
  13.     private ?bool $newWindow null;
  14.     private bool $isDirectFallback false;
  15.     private bool $isCurrentPage false;
  16.     public function __construct(PageModel $rootPagestring $label null)
  17.     {
  18.         if ('root' !== $rootPage->type) {
  19.             throw new \RuntimeException(sprintf('Page ID "%s" has type "%s" but should be "root"'$rootPage->id$rootPage->type));
  20.         }
  21.         $this->rootPage $rootPage->loadDetails();
  22.         $this->linkLabel $label;
  23.         if (null === $label) {
  24.             $this->linkLabel strtoupper($this->getLanguageTag());
  25.         }
  26.     }
  27.     public function hasTargetPage(): bool
  28.     {
  29.         return $this->targetPage instanceof PageModel;
  30.     }
  31.     public function isDirectFallback(): bool
  32.     {
  33.         return $this->isDirectFallback;
  34.     }
  35.     public function setIsDirectFallback($isDirectFallback): void
  36.     {
  37.         $this->isDirectFallback = (bool) $isDirectFallback;
  38.     }
  39.     public function isCurrentPage(): bool
  40.     {
  41.         return $this->isCurrentPage;
  42.     }
  43.     public function getRootPage(): PageModel
  44.     {
  45.         return $this->rootPage;
  46.     }
  47.     public function getLabel(): string
  48.     {
  49.         return $this->linkLabel;
  50.     }
  51.     public function setLabel(string $label): void
  52.     {
  53.         $this->linkLabel $label;
  54.     }
  55.     public function getAriaLabel(): ?string
  56.     {
  57.         return $this->ariaLabel;
  58.     }
  59.     public function setAriaLabel(?string $ariaLabel): void
  60.     {
  61.         $this->ariaLabel $ariaLabel;
  62.     }
  63.     public function getTargetPage(): ?PageModel
  64.     {
  65.         return $this->targetPage;
  66.     }
  67.     public function setTargetPage(PageModel $targetPagebool $isDirectFallbackbool $isCurrentPage false): void
  68.     {
  69.         $this->targetPage $targetPage->loadDetails();
  70.         $this->isDirectFallback $isDirectFallback;
  71.         $this->isCurrentPage $isCurrentPage;
  72.     }
  73.     public function setIsCurrentPage(bool $isCurrentPage): void
  74.     {
  75.         $this->isCurrentPage $isCurrentPage;
  76.     }
  77.     public function isNewWindow(): ?bool
  78.     {
  79.         if (null === $this->newWindow) {
  80.             $targetPage $this->targetPage ?: $this->rootPage;
  81.             return 'redirect' === $targetPage->type && $targetPage->target;
  82.         }
  83.         return $this->newWindow;
  84.     }
  85.     public function setNewWindow(?bool $newWindow): void
  86.     {
  87.         $this->newWindow $newWindow;
  88.     }
  89.     public function getNormalizedLanguage(): string
  90.     {
  91.         return strtolower($this->getLocaleId());
  92.     }
  93.     /**
  94.      * Returns the language formatted as IETF Language Tag (BCP 47)
  95.      * Example: en, en-US, de-CH.
  96.      *
  97.      * @see http://www.w3.org/International/articles/language-tags/
  98.      */
  99.     public function getLanguageTag(): string
  100.     {
  101.         return Language::toLanguageTag($this->rootPage->language);
  102.     }
  103.     /**
  104.      * Returns the language formatted as ICU Locale ID
  105.      * Example: en, en_US, de_CH.
  106.      *
  107.      * @see http://userguide.icu-project.org/locale
  108.      */
  109.     public function getLocaleId(): string
  110.     {
  111.         return Language::toLocaleID($this->rootPage->language);
  112.     }
  113.     /**
  114.      * @throws ExceptionInterface
  115.      */
  116.     public function getHref(UrlParameterBag $urlParameterBagbool $catch false): string
  117.     {
  118.         $targetPage $this->targetPage ?: $this->rootPage;
  119.         if ('root' === $targetPage->type) {
  120.             $targetPage PageModel::findFirstPublishedRegularByPid($targetPage->id) ?: $targetPage;
  121.         }
  122.         try {
  123.             $href $targetPage->getAbsoluteUrl($urlParameterBag->generateParameters());
  124.         } catch (ExceptionInterface $e) {
  125.             if (!$catch) {
  126.                 throw $e;
  127.             }
  128.             $this->targetPage null;
  129.             $this->isDirectFallback false;
  130.             $this->isCurrentPage false;
  131.             return $this->getHref($urlParameterBag);
  132.         }
  133.         if (null !== ($queryString $urlParameterBag->generateQueryString())) {
  134.             $href .= '?'.$queryString;
  135.         }
  136.         return $href;
  137.     }
  138.     public function getTitle(): string
  139.     {
  140.         return $this->targetPage->title ?? $this->rootPage->title;
  141.     }
  142.     public function getPageTitle(): string
  143.     {
  144.         return $this->targetPage->pageTitle ?? $this->rootPage->pageTitle;
  145.     }
  146. }