vendor/tattali/mobile-detect-bundle/src/Helper/DeviceView.php line 108

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the MobileDetectBundle.
  4.  *
  5.  * (c) Nikolay Ivlev <nikolay.kotovsky@gmail.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace MobileDetectBundle\Helper;
  12. use Symfony\Component\HttpFoundation\Cookie;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\RequestStack;
  15. use Symfony\Component\HttpFoundation\Response;
  16. /**
  17.  * @author suncat2000 <nikolay.kotovsky@gmail.com>
  18.  */
  19. class DeviceView
  20. {
  21.     public const VIEW_MOBILE 'mobile';
  22.     public const VIEW_TABLET 'tablet';
  23.     public const VIEW_FULL 'full';
  24.     public const VIEW_NOT_MOBILE 'not_mobile';
  25.     public const COOKIE_KEY_DEFAULT 'device_view';
  26.     public const COOKIE_PATH_DEFAULT '/';
  27.     public const COOKIE_DOMAIN_DEFAULT '';
  28.     public const COOKIE_SECURE_DEFAULT false;
  29.     public const COOKIE_HTTP_ONLY_DEFAULT true;
  30.     public const COOKIE_RAW_DEFAULT false;
  31.     public const COOKIE_SAMESITE_DEFAULT Cookie::SAMESITE_LAX;
  32.     public const COOKIE_EXPIRE_DATETIME_MODIFIER_DEFAULT '1 month';
  33.     public const SWITCH_PARAM_DEFAULT 'device_view';
  34.     /**
  35.      * @var Request
  36.      */
  37.     protected $request;
  38.     /**
  39.      * @var string
  40.      */
  41.     protected $requestedViewType;
  42.     /**
  43.      * @var string
  44.      */
  45.     protected $viewType;
  46.     /**
  47.      * @var string
  48.      */
  49.     protected $cookieKey self::COOKIE_KEY_DEFAULT;
  50.     /**
  51.      * @var string
  52.      */
  53.     protected $cookiePath self::COOKIE_PATH_DEFAULT;
  54.     /**
  55.      * @var string
  56.      */
  57.     protected $cookieDomain self::COOKIE_DOMAIN_DEFAULT;
  58.     /**
  59.      * @var bool
  60.      */
  61.     protected $cookieSecure self::COOKIE_SECURE_DEFAULT;
  62.     /**
  63.      * @var bool
  64.      */
  65.     protected $cookieHttpOnly self::COOKIE_HTTP_ONLY_DEFAULT;
  66.     /**
  67.      * @var bool
  68.      */
  69.     protected $cookieRaw self::COOKIE_RAW_DEFAULT;
  70.     /**
  71.      * @var string|null
  72.      */
  73.     protected $cookieSameSite self::COOKIE_SAMESITE_DEFAULT;
  74.     /**
  75.      * @var string
  76.      */
  77.     protected $cookieExpireDatetimeModifier self::COOKIE_EXPIRE_DATETIME_MODIFIER_DEFAULT;
  78.     /**
  79.      * @var string
  80.      */
  81.     protected $switchParam self::SWITCH_PARAM_DEFAULT;
  82.     /**
  83.      * @var array
  84.      */
  85.     protected $redirectConfig = [];
  86.     public function __construct(RequestStack $requestStack null)
  87.     {
  88.         if (!$requestStack || !$this->request $requestStack->getMainRequest()) {
  89.             $this->viewType self::VIEW_NOT_MOBILE;
  90.             return;
  91.         }
  92.         if ($this->request->query->has($this->switchParam)) {
  93.             $this->viewType $this->request->query->get($this->switchParam);
  94.         } elseif ($this->request->cookies->has($this->cookieKey)) {
  95.             $this->viewType $this->request->cookies->get($this->cookieKey);
  96.         }
  97.         $this->requestedViewType $this->viewType;
  98.     }
  99.     /**
  100.      * Gets the view type for a device.
  101.      */
  102.     public function getViewType(): ?string
  103.     {
  104.         return $this->viewType;
  105.     }
  106.     /**
  107.      * Gets the view type that has explicitly been requested either by switch param, or by cookie.
  108.      *
  109.      * @return string the requested view type or null if no view type has been explicitly requested
  110.      */
  111.     public function getRequestedViewType(): ?string
  112.     {
  113.         return $this->requestedViewType;
  114.     }
  115.     /**
  116.      * Is the device in full view.
  117.      */
  118.     public function isFullView(): bool
  119.     {
  120.         return self::VIEW_FULL === $this->viewType;
  121.     }
  122.     public function isTabletView(): bool
  123.     {
  124.         return self::VIEW_TABLET === $this->viewType;
  125.     }
  126.     public function isMobileView(): bool
  127.     {
  128.         return self::VIEW_MOBILE === $this->viewType;
  129.     }
  130.     /**
  131.      * Is not the device a mobile view type (PC, Mac, etc.).
  132.      */
  133.     public function isNotMobileView(): bool
  134.     {
  135.         return self::VIEW_NOT_MOBILE === $this->viewType;
  136.     }
  137.     /**
  138.      * Has the Request the switch param in the query string (GET header).
  139.      */
  140.     public function hasSwitchParam(): bool
  141.     {
  142.         return $this->request && $this->request->query->has($this->switchParam);
  143.     }
  144.     public function setView(string $view): void
  145.     {
  146.         $this->viewType $view;
  147.     }
  148.     /**
  149.      * Sets the full (desktop) view type.
  150.      */
  151.     public function setFullView(): void
  152.     {
  153.         $this->viewType self::VIEW_FULL;
  154.     }
  155.     public function setTabletView(): void
  156.     {
  157.         $this->viewType self::VIEW_TABLET;
  158.     }
  159.     public function setMobileView(): void
  160.     {
  161.         $this->viewType self::VIEW_MOBILE;
  162.     }
  163.     public function setNotMobileView(): void
  164.     {
  165.         $this->viewType self::VIEW_NOT_MOBILE;
  166.     }
  167.     public function getRedirectConfig(): array
  168.     {
  169.         return $this->redirectConfig;
  170.     }
  171.     public function setRedirectConfig(array $redirectConfig): void
  172.     {
  173.         $this->redirectConfig $redirectConfig;
  174.     }
  175.     public function getRedirectResponseBySwitchParam(string $redirectUrl): RedirectResponseWithCookie
  176.     {
  177.         switch ($this->getSwitchParamValue()) {
  178.             case self::VIEW_MOBILE:
  179.                 $viewType self::VIEW_MOBILE;
  180.                 break;
  181.             case self::VIEW_TABLET:
  182.                 $viewType self::VIEW_TABLET;
  183.                 if (isset($this->getRedirectConfig()['detect_tablet_as_mobile']) && true === $this->getRedirectConfig()['detect_tablet_as_mobile']) {
  184.                     $viewType self::VIEW_MOBILE;
  185.                 }
  186.                 break;
  187.             default:
  188.                 $viewType self::VIEW_FULL;
  189.         }
  190.         return new RedirectResponseWithCookie(
  191.             $redirectUrl,
  192.             $this->getStatusCode($viewType),
  193.             $this->createCookie($viewType)
  194.         );
  195.     }
  196.     /**
  197.      * Gets the switch param value from the query string (GET header).
  198.      */
  199.     public function getSwitchParamValue(): ?string
  200.     {
  201.         if (!$this->request) {
  202.             return null;
  203.         }
  204.         return $this->request->query->get($this->switchParamself::VIEW_FULL);
  205.     }
  206.     public function getCookieExpireDatetimeModifier(): string
  207.     {
  208.         return $this->cookieExpireDatetimeModifier;
  209.     }
  210.     public function setCookieExpireDatetimeModifier(string $cookieExpireDatetimeModifier): void
  211.     {
  212.         $this->cookieExpireDatetimeModifier $cookieExpireDatetimeModifier;
  213.     }
  214.     public function getCookieKey(): string
  215.     {
  216.         return $this->cookieKey;
  217.     }
  218.     public function setCookieKey(string $cookieKey): void
  219.     {
  220.         $this->cookieKey $cookieKey;
  221.     }
  222.     public function getCookiePath(): string
  223.     {
  224.         return $this->cookiePath;
  225.     }
  226.     public function setCookiePath(string $cookiePath): void
  227.     {
  228.         $this->cookiePath $cookiePath;
  229.     }
  230.     public function getCookieDomain(): string
  231.     {
  232.         return $this->cookieDomain;
  233.     }
  234.     public function setCookieDomain(string $cookieDomain): void
  235.     {
  236.         $this->cookieDomain $cookieDomain;
  237.     }
  238.     public function isCookieSecure(): bool
  239.     {
  240.         return $this->cookieSecure;
  241.     }
  242.     public function setCookieSecure(bool $cookieSecure): void
  243.     {
  244.         $this->cookieSecure $cookieSecure;
  245.     }
  246.     public function isCookieHttpOnly(): bool
  247.     {
  248.         return $this->cookieHttpOnly;
  249.     }
  250.     public function setCookieHttpOnly(bool $cookieHttpOnly): void
  251.     {
  252.         $this->cookieHttpOnly $cookieHttpOnly;
  253.     }
  254.     public function isCookieRaw(): bool
  255.     {
  256.         return $this->cookieRaw;
  257.     }
  258.     public function setCookieRaw(bool $cookieRaw false): void
  259.     {
  260.         $this->cookieRaw $cookieRaw;
  261.     }
  262.     public function getCookieSameSite(): ?string
  263.     {
  264.         return $this->cookieSameSite;
  265.     }
  266.     public function setCookieSameSite(?string $cookieSameSite Cookie::SAMESITE_LAX): void
  267.     {
  268.         $this->cookieSameSite $cookieSameSite;
  269.     }
  270.     /**
  271.      * Modifies the Response for the specified device view.
  272.      *
  273.      * @param string $view the device view for which the response should be modified
  274.      */
  275.     public function modifyResponse(string $viewResponse $response): Response
  276.     {
  277.         $response->headers->setCookie($this->createCookie($view));
  278.         return $response;
  279.     }
  280.     /**
  281.      * Gets the RedirectResponse for the specified device view.
  282.      *
  283.      * @param string $view       The device view for which we want the RedirectResponse
  284.      * @param string $host       Uri host
  285.      * @param int    $statusCode Status code
  286.      */
  287.     public function getRedirectResponse(string $viewstring $hostint $statusCode): RedirectResponseWithCookie
  288.     {
  289.         return new RedirectResponseWithCookie($host$statusCode$this->createCookie($view));
  290.     }
  291.     public function getSwitchParam(): string
  292.     {
  293.         return $this->switchParam;
  294.     }
  295.     public function setSwitchParam(string $switchParam): void
  296.     {
  297.         $this->switchParam $switchParam;
  298.     }
  299.     protected function getStatusCode(string $view): int
  300.     {
  301.         if (isset($this->getRedirectConfig()[$view]['status_code'])) {
  302.             return $this->getRedirectConfig()[$view]['status_code'];
  303.         }
  304.         return Response::HTTP_FOUND;
  305.     }
  306.     /**
  307.      * Create the Cookie object.
  308.      */
  309.     protected function createCookie(string $value): Cookie
  310.     {
  311.         try {
  312.             $expire = new \DateTime($this->getCookieExpireDatetimeModifier());
  313.         } catch (\Exception $e) {
  314.             $expire = new \DateTime(self::COOKIE_EXPIRE_DATETIME_MODIFIER_DEFAULT);
  315.         }
  316.         return Cookie::create(
  317.             $this->getCookieKey(),
  318.             $value,
  319.             $expire,
  320.             $this->getCookiePath(),
  321.             $this->getCookieDomain(),
  322.             $this->isCookieSecure(),
  323.             $this->isCookieHttpOnly(),
  324.             $this->isCookieRaw(),
  325.             $this->getCookieSameSite()
  326.         );
  327.     }
  328. }