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.     const VIEW_MOBILE 'mobile';
  22.     const VIEW_TABLET 'tablet';
  23.     const VIEW_FULL 'full';
  24.     const VIEW_NOT_MOBILE 'not_mobile';
  25.     const COOKIE_KEY_DEFAULT 'device_view';
  26.     const COOKIE_PATH_DEFAULT '/';
  27.     const COOKIE_DOMAIN_DEFAULT '';
  28.     const COOKIE_SECURE_DEFAULT false;
  29.     const COOKIE_HTTP_ONLY_DEFAULT true;
  30.     const COOKIE_RAW_DEFAULT false;
  31.     const COOKIE_SAMESITE_DEFAULT null;
  32.     const COOKIE_EXPIRE_DATETIME_MODIFIER_DEFAULT '1 month';
  33.     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->getMasterRequest()) {
  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.     /**
  123.      * Is the device a tablet view type.
  124.      */
  125.     public function isTabletView(): bool
  126.     {
  127.         return self::VIEW_TABLET === $this->viewType;
  128.     }
  129.     /**
  130.      * Is the device a mobile view type.
  131.      */
  132.     public function isMobileView(): bool
  133.     {
  134.         return self::VIEW_MOBILE === $this->viewType;
  135.     }
  136.     /**
  137.      * Is not the device a mobile view type (PC, Mac, etc.).
  138.      */
  139.     public function isNotMobileView(): bool
  140.     {
  141.         return self::VIEW_NOT_MOBILE === $this->viewType;
  142.     }
  143.     /**
  144.      * Has the Request the switch param in the query string (GET header).
  145.      */
  146.     public function hasSwitchParam(): bool
  147.     {
  148.         return $this->request && $this->request->query->has($this->switchParam);
  149.     }
  150.     public function setView(string $view): void
  151.     {
  152.         $this->viewType $view;
  153.     }
  154.     /**
  155.      * Sets the full (desktop) view type.
  156.      */
  157.     public function setFullView(): void
  158.     {
  159.         $this->viewType self::VIEW_FULL;
  160.     }
  161.     /**
  162.      * Sets the tablet view type.
  163.      */
  164.     public function setTabletView(): void
  165.     {
  166.         $this->viewType self::VIEW_TABLET;
  167.     }
  168.     /**
  169.      * Sets the mobile view type.
  170.      */
  171.     public function setMobileView(): void
  172.     {
  173.         $this->viewType self::VIEW_MOBILE;
  174.     }
  175.     /**
  176.      * Sets the not mobile view type.
  177.      */
  178.     public function setNotMobileView(): void
  179.     {
  180.         $this->viewType self::VIEW_NOT_MOBILE;
  181.     }
  182.     /**
  183.      * Gets the switch param value from the query string (GET header).
  184.      */
  185.     public function getSwitchParamValue(): ?string
  186.     {
  187.         if (!$this->request) {
  188.             return null;
  189.         }
  190.         return $this->request->query->get($this->switchParamself::VIEW_FULL);
  191.     }
  192.     /**
  193.      * Getter of RedirectConfig.
  194.      */
  195.     public function getRedirectConfig(): array
  196.     {
  197.         return $this->redirectConfig;
  198.     }
  199.     /**
  200.      * Setter of RedirectConfig.
  201.      */
  202.     public function setRedirectConfig(array $redirectConfig): void
  203.     {
  204.         $this->redirectConfig $redirectConfig;
  205.     }
  206.     /**
  207.      * Gets the RedirectResponse by switch param value.
  208.      */
  209.     public function getRedirectResponseBySwitchParam(string $redirectUrl): RedirectResponseWithCookie
  210.     {
  211.         switch ($this->getSwitchParamValue()) {
  212.             case self::VIEW_MOBILE:
  213.                 $viewType self::VIEW_MOBILE;
  214.                 break;
  215.             case self::VIEW_TABLET:
  216.                 $viewType self::VIEW_TABLET;
  217.                 if (isset($this->redirectConfig['detect_tablet_as_mobile']) && true === $this->redirectConfig['detect_tablet_as_mobile']) {
  218.                     $viewType self::VIEW_MOBILE;
  219.                 }
  220.                 break;
  221.             default:
  222.                 $viewType self::VIEW_FULL;
  223.         }
  224.         return new RedirectResponseWithCookie($redirectUrl$this->getStatusCode($viewType), $this->createCookie($viewType));
  225.     }
  226.     /**
  227.      * Modifies the Response for the specified device view.
  228.      *
  229.      * @param string $view the device view for which the response should be modified
  230.      */
  231.     public function modifyResponse(string $viewResponse $response): Response
  232.     {
  233.         $response->headers->setCookie($this->createCookie($view));
  234.         return $response;
  235.     }
  236.     /**
  237.      * Gets the RedirectResponse for the specified device view.
  238.      *
  239.      * @param string $view       the device view for which we want the RedirectResponse
  240.      * @param string $host       Uri host
  241.      * @param int    $statusCode Status code
  242.      */
  243.     public function getRedirectResponse(string $viewstring $hostint $statusCode): RedirectResponseWithCookie
  244.     {
  245.         return new RedirectResponseWithCookie($host$statusCode$this->createCookie($view));
  246.     }
  247.     /**
  248.      * Setter of CookieKey.
  249.      */
  250.     public function setCookieKey(string $cookieKey): void
  251.     {
  252.         $this->cookieKey $cookieKey;
  253.     }
  254.     /**
  255.      * Getter of CookieKey.
  256.      */
  257.     public function getCookieKey(): string
  258.     {
  259.         return $this->cookieKey;
  260.     }
  261.     /**
  262.      * Getter of CookiePath.
  263.      */
  264.     public function getCookiePath(): string
  265.     {
  266.         return $this->cookiePath;
  267.     }
  268.     /**
  269.      * Setter of CookiePath.
  270.      */
  271.     public function setCookiePath(string $cookiePath): void
  272.     {
  273.         $this->cookiePath $cookiePath;
  274.     }
  275.     /**
  276.      * Getter of CookieDomain.
  277.      */
  278.     public function getCookieDomain(): string
  279.     {
  280.         return $this->cookieDomain;
  281.     }
  282.     /**
  283.      * Setter of CookieDomain.
  284.      */
  285.     public function setCookieDomain(string $cookieDomain): void
  286.     {
  287.         $this->cookieDomain $cookieDomain;
  288.     }
  289.     /**
  290.      * Is the cookie secure.
  291.      */
  292.     public function isCookieSecure(): bool
  293.     {
  294.         return $this->cookieSecure;
  295.     }
  296.     /**
  297.      * Setter of CookieSecure.
  298.      */
  299.     public function setCookieSecure(bool $cookieSecure)
  300.     {
  301.         $this->cookieSecure $cookieSecure;
  302.     }
  303.     /**
  304.      * Is the cookie http only.
  305.      */
  306.     public function isCookieHttpOnly(): bool
  307.     {
  308.         return $this->cookieHttpOnly;
  309.     }
  310.     /**
  311.      * Setter of CookieHttpOnly.
  312.      */
  313.     public function setCookieHttpOnly(bool $cookieHttpOnly): void
  314.     {
  315.         $this->cookieHttpOnly $cookieHttpOnly;
  316.     }
  317.     /**
  318.      * Is the cookie raw.
  319.      */
  320.     public function isCookieRaw(): bool
  321.     {
  322.         return $this->cookieRaw;
  323.     }
  324.     /**
  325.      * Setter of CookieRaw.
  326.      */
  327.     public function setCookieRaw(bool $cookieRaw): void
  328.     {
  329.         $this->cookieRaw $cookieRaw;
  330.     }
  331.     /**
  332.      * Getter of CookieSamesite.
  333.      */
  334.     public function getCookieSamesite(): ?string
  335.     {
  336.         return $this->cookieSamesite;
  337.     }
  338.     /**
  339.      * Setter of CookieSamesite.
  340.      */
  341.     public function setCookieSamesite(?string $cookieSamesite): void
  342.     {
  343.         $this->cookieSamesite $cookieSamesite;
  344.     }
  345.     /**
  346.      * Setter of SwitchParam.
  347.      */
  348.     public function setSwitchParam(string $switchParam): void
  349.     {
  350.         $this->switchParam $switchParam;
  351.     }
  352.     /**
  353.      * Getter of SwitchParam.
  354.      */
  355.     public function getSwitchParam(): string
  356.     {
  357.         return $this->switchParam;
  358.     }
  359.     public function setCookieExpireDatetimeModifier(string $cookieExpireDatetimeModifier): void
  360.     {
  361.         $this->cookieExpireDatetimeModifier $cookieExpireDatetimeModifier;
  362.     }
  363.     public function getCookieExpireDatetimeModifier(): string
  364.     {
  365.         return $this->cookieExpireDatetimeModifier;
  366.     }
  367.     /**
  368.      * Create the Cookie object.
  369.      */
  370.     protected function createCookie(string $value): Cookie
  371.     {
  372.         try {
  373.             $expire = new \Datetime($this->getCookieExpireDatetimeModifier());
  374.         } catch (\Exception $e) {
  375.             $expire = new \Datetime(self::COOKIE_EXPIRE_DATETIME_MODIFIER_DEFAULT);
  376.         }
  377.         return new Cookie(
  378.             $this->getCookieKey(),
  379.             $value,
  380.             $expire,
  381.             $this->getCookiePath(),
  382.             $this->getCookieDomain(),
  383.             $this->isCookieSecure(),
  384.             $this->isCookieHttpOnly(),
  385.             $this->isCookieRaw(),
  386.             $this->getCookieSamesite()
  387.         );
  388.     }
  389.     protected function getStatusCode(string $view): int
  390.     {
  391.         if (isset($this->redirectConfig[$view]['status_code'])) {
  392.             return $this->redirectConfig[$view]['status_code'];
  393.         }
  394.         return Response::HTTP_FOUND;
  395.     }
  396. }