vendor/netbull/mobile-detect-bundle/src/Helper/DeviceView.php line 100

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. namespace SunCat\MobileDetectBundle\Helper;
  11. use Datetime;
  12. use Exception;
  13. use Symfony\Component\HttpFoundation\Cookie;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\RequestStack;
  16. use Symfony\Component\HttpFoundation\Response;
  17. /**
  18.  * DeviceView
  19.  *
  20.  * @author suncat2000 <nikolay.kotovsky@gmail.com>
  21.  */
  22. class DeviceView
  23. {
  24.     const VIEW_MOBILE 'mobile';
  25.     const VIEW_TABLET 'tablet';
  26.     const VIEW_FULL 'full';
  27.     const VIEW_NOT_MOBILE 'not_mobile';
  28.     const COOKIE_KEY_DEFAULT 'device_view';
  29.     const COOKIE_PATH_DEFAULT '/';
  30.     const COOKIE_DOMAIN_DEFAULT '';
  31.     const COOKIE_SECURE_DEFAULT false;
  32.     const COOKIE_HTTP_ONLY_DEFAULT true;
  33.     const COOKIE_EXPIRE_DATETIME_MODIFIER_DEFAULT '1 month';
  34.     const SWITCH_PARAM_DEFAULT 'device_view';
  35.     /**
  36.      * @var Request
  37.      */
  38.     protected $request;
  39.     /**
  40.      * @var string|null
  41.      */
  42.     protected $requestedViewType;
  43.     /**
  44.      * @var string|null
  45.      */
  46.     protected $viewType;
  47.     /**
  48.      * @var string
  49.      */
  50.     protected $cookieKey self::COOKIE_KEY_DEFAULT;
  51.     /**
  52.      * @var string
  53.      */
  54.     protected $cookiePath self::COOKIE_PATH_DEFAULT;
  55.     /**
  56.      * @var string
  57.      */
  58.     protected $cookieDomain self::COOKIE_DOMAIN_DEFAULT;
  59.     /**
  60.      * @var bool
  61.      */
  62.     protected $cookieSecure self::COOKIE_SECURE_DEFAULT;
  63.     /**
  64.      * @var bool
  65.      */
  66.     protected $cookieHttpOnly self::COOKIE_HTTP_ONLY_DEFAULT;
  67.     /**
  68.      * @var string
  69.      */
  70.     protected $cookieExpireDatetimeModifier self::COOKIE_EXPIRE_DATETIME_MODIFIER_DEFAULT;
  71.     /**
  72.      * @var string
  73.      */
  74.     protected $switchParam self::SWITCH_PARAM_DEFAULT;
  75.     /**
  76.      * @var array
  77.      */
  78.     protected $redirectConfig;
  79.     /**
  80.      * DeviceView constructor.
  81.      * @param RequestStack|null $requestStack
  82.      */
  83.     public function __construct(RequestStack $requestStack null)
  84.     {
  85.         if (!$requestStack || !$this->request $requestStack->getMasterRequest()) {
  86.             $this->viewType self::VIEW_NOT_MOBILE;
  87.             return;
  88.         }
  89.         if ($this->request->query->has($this->switchParam)) {
  90.             $this->viewType $this->request->query->get($this->switchParam);
  91.         } elseif ($this->request->cookies->has($this->cookieKey)) {
  92.             $this->viewType $this->request->cookies->get($this->cookieKey);
  93.         }
  94.         $this->requestedViewType $this->viewType;
  95.     }
  96.     /**
  97.      * Gets the view type for a device.
  98.      *
  99.      * @return string|null
  100.      */
  101.     public function getViewType(): ?string
  102.     {
  103.         return $this->viewType;
  104.     }
  105.     /**
  106.      * Gets the view type that has explicitly been requested either by switch param, or by cookie.
  107.      *
  108.      * @return string|null The requested view type or null if no view type has been explicitly requested.
  109.      */
  110.     public function getRequestedViewType(): ?string
  111.     {
  112.         return $this->requestedViewType;
  113.     }
  114.     /**
  115.      * Is the device in full view.
  116.      *
  117.      * @return boolean
  118.      */
  119.     public function isFullView(): bool
  120.     {
  121.         return $this->viewType === self::VIEW_FULL;
  122.     }
  123.     /**
  124.      * Is the device a tablet view type.
  125.      *
  126.      * @return boolean
  127.      */
  128.     public function isTabletView(): bool
  129.     {
  130.         return $this->viewType === self::VIEW_TABLET;
  131.     }
  132.     /**
  133.      * Is the device a mobile view type.
  134.      *
  135.      * @return boolean
  136.      */
  137.     public function isMobileView(): bool
  138.     {
  139.         return $this->viewType === self::VIEW_MOBILE;
  140.     }
  141.     /**
  142.      * Is not the device a mobile view type (PC, Mac, etc.).
  143.      *
  144.      * @return boolean
  145.      */
  146.     public function isNotMobileView(): bool
  147.     {
  148.         return $this->viewType === self::VIEW_NOT_MOBILE;
  149.     }
  150.     /**
  151.      * Has the Request the switch param in the query string (GET header).
  152.      *
  153.      * @return boolean
  154.      */
  155.     public function hasSwitchParam(): bool
  156.     {
  157.         return $this->request && $this->request->query->has($this->switchParam);
  158.     }
  159.     /**
  160.      * Sets the view type.
  161.      *
  162.      * @param string $view
  163.      */
  164.     public function setView(string $view)
  165.     {
  166.         $this->viewType $view;
  167.     }
  168.     /**
  169.      * Sets the full (desktop) view type.
  170.      */
  171.     public function setFullView()
  172.     {
  173.         $this->viewType self::VIEW_FULL;
  174.     }
  175.     /**
  176.      * Sets the tablet view type.
  177.      */
  178.     public function setTabletView()
  179.     {
  180.         $this->viewType self::VIEW_TABLET;
  181.     }
  182.     /**
  183.      * Sets the mobile view type.
  184.      */
  185.     public function setMobileView()
  186.     {
  187.         $this->viewType self::VIEW_MOBILE;
  188.     }
  189.     /**
  190.      * Sets the not mobile view type.
  191.      */
  192.     public function setNotMobileView()
  193.     {
  194.         $this->viewType self::VIEW_NOT_MOBILE;
  195.     }
  196.     /**
  197.      * Gets the switch param value from the query string (GET header).
  198.      *
  199.      * @return string|null
  200.      */
  201.     public function getSwitchParamValue(): ?string
  202.     {
  203.         if (!$this->request) {
  204.             return null;
  205.         }
  206.         return $this->request->query->get($this->switchParamself::VIEW_FULL);
  207.     }
  208.     /**
  209.      * Getter of RedirectConfig.
  210.      *
  211.      * @return array
  212.      */
  213.     public function getRedirectConfig(): array
  214.     {
  215.         return $this->redirectConfig;
  216.     }
  217.     /**
  218.      * Setter of RedirectConfig.
  219.      *
  220.      * @param array $redirectConfig
  221.      */
  222.     public function setRedirectConfig(array $redirectConfig)
  223.     {
  224.         $this->redirectConfig $redirectConfig;
  225.     }
  226.     /**
  227.      * Gets the RedirectResponse by switch param value.
  228.      *
  229.      * @param string $redirectUrl
  230.      *
  231.      * @return RedirectResponseWithCookie
  232.      */
  233.     public function getRedirectResponseBySwitchParam(string $redirectUrl): RedirectResponseWithCookie
  234.     {
  235.         switch ($this->getSwitchParamValue()) {
  236.             case self::VIEW_MOBILE:
  237.                 $viewType self::VIEW_MOBILE;
  238.                 break;
  239.             case self::VIEW_TABLET:
  240.                 $viewType self::VIEW_TABLET;
  241.                 if (isset($this->redirectConfig['detect_tablet_as_mobile']) && $this->redirectConfig['detect_tablet_as_mobile'] === true) {
  242.                     $viewType self::VIEW_MOBILE;
  243.                 }
  244.                 break;
  245.             default:
  246.                 $viewType self::VIEW_FULL;
  247.         }
  248.         return new RedirectResponseWithCookie(
  249.             $redirectUrl,
  250.             $this->createCookie($viewType),
  251.             $this->getStatusCode($viewType)
  252.         );
  253.     }
  254.     /**
  255.      * Modifies the Response for the specified device view.
  256.      *
  257.      * @param string $view The device view for which the response should be modified.
  258.      * @param Response $response
  259.      *
  260.      * @return Response
  261.      */
  262.     public function modifyResponse(string $viewResponse $response): Response
  263.     {
  264.         $response->headers->setCookie($this->createCookie($view));
  265.         return $response;
  266.     }
  267.     /**
  268.      * Gets the RedirectResponse for the specified device view.
  269.      *
  270.      * @param string $view The device view for which we want the RedirectResponse.
  271.      * @param string $host Uri host
  272.      * @param int $statusCode Status code
  273.      *
  274.      * @return RedirectResponseWithCookie
  275.      */
  276.     public function getRedirectResponse(string $viewstring $hostint $statusCode): RedirectResponseWithCookie
  277.     {
  278.         return new RedirectResponseWithCookie($host$this->createCookie($view), $statusCode);
  279.     }
  280.     /**
  281.      * Setter of CookieKey
  282.      *
  283.      * @param string $cookieKey
  284.      */
  285.     public function setCookieKey(string $cookieKey)
  286.     {
  287.         $this->cookieKey $cookieKey;
  288.     }
  289.     /**
  290.      * Getter of CookieKey
  291.      *
  292.      * @return string
  293.      */
  294.     public function getCookieKey(): string
  295.     {
  296.         return $this->cookieKey;
  297.     }
  298.     /**
  299.      * Getter of CookiePath.
  300.      *
  301.      * @return string
  302.      */
  303.     public function getCookiePath(): string
  304.     {
  305.         return $this->cookiePath;
  306.     }
  307.     /**
  308.      * Setter of CookiePath.
  309.      *
  310.      * @param string $cookiePath
  311.      */
  312.     public function setCookiePath(string $cookiePath)
  313.     {
  314.         $this->cookiePath $cookiePath;
  315.     }
  316.     /**
  317.      * Getter of CookieDomain.
  318.      *
  319.      * @return string
  320.      */
  321.     public function getCookieDomain(): string
  322.     {
  323.         return $this->cookieDomain;
  324.     }
  325.     /**
  326.      * Setter of CookieDomain.
  327.      *
  328.      * @param string $cookieDomain
  329.      */
  330.     public function setCookieDomain(string $cookieDomain)
  331.     {
  332.         $this->cookieDomain $cookieDomain;
  333.     }
  334.     /**
  335.      * Is the cookie secure.
  336.      *
  337.      * @return bool
  338.      */
  339.     public function isCookieSecure(): bool
  340.     {
  341.         return $this->cookieSecure;
  342.     }
  343.     /**
  344.      * Setter of CookieSecure.
  345.      *
  346.      * @param bool $cookieSecure
  347.      */
  348.     public function setCookieSecure(bool $cookieSecure)
  349.     {
  350.         $this->cookieSecure $cookieSecure;
  351.     }
  352.     /**
  353.      * Is the cookie http only.
  354.      *
  355.      * @return bool
  356.      */
  357.     public function isCookieHttpOnly(): bool
  358.     {
  359.         return $this->cookieHttpOnly;
  360.     }
  361.     /**
  362.      * Setter of CookieHttpOnly.
  363.      *
  364.      * @param bool $cookieHttpOnly
  365.      */
  366.     public function setCookieHttpOnly(bool $cookieHttpOnly)
  367.     {
  368.         $this->cookieHttpOnly $cookieHttpOnly;
  369.     }
  370.     /**
  371.      * Setter of SwitchParam.
  372.      *
  373.      * @param string $switchParam
  374.      */
  375.     public function setSwitchParam(string $switchParam)
  376.     {
  377.         $this->switchParam $switchParam;
  378.     }
  379.     /**
  380.      * Getter of SwitchParam
  381.      *
  382.      * @return string
  383.      */
  384.     public function getSwitchParam(): string
  385.     {
  386.         return $this->switchParam;
  387.     }
  388.     /**
  389.      * @param string $cookieExpireDatetimeModifier
  390.      */
  391.     public function setCookieExpireDatetimeModifier($cookieExpireDatetimeModifier)
  392.     {
  393.         $this->cookieExpireDatetimeModifier $cookieExpireDatetimeModifier;
  394.     }
  395.     /**
  396.      * @return string
  397.      */
  398.     public function getCookieExpireDatetimeModifier(): string
  399.     {
  400.         return $this->cookieExpireDatetimeModifier;
  401.     }
  402.     /**
  403.      * Create the Cookie object
  404.      *
  405.      * @param string $value
  406.      *
  407.      * @return Cookie
  408.      */
  409.     protected function createCookie(string $value): Cookie
  410.     {
  411.         try {
  412.             $expire = new Datetime($this->getCookieExpireDatetimeModifier());
  413.         } catch (Exception $e) {
  414.             $expire = new Datetime(self::COOKIE_EXPIRE_DATETIME_MODIFIER_DEFAULT);
  415.         }
  416.         return new Cookie(
  417.             $this->getCookieKey(),
  418.             $value,
  419.             $expire,
  420.             $this->getCookiePath(),
  421.             $this->getCookieDomain(),
  422.             $this->isCookieSecure(),
  423.             $this->isCookieHttpOnly(),
  424.             false,
  425.             Cookie::SAMESITE_LAX
  426.         );
  427.     }
  428.     /**
  429.      * @param string $view
  430.      *
  431.      * @return integer
  432.      */
  433.     protected function getStatusCode(string $view): int
  434.     {
  435.         if (isset($this->redirectConfig[$view]['status_code'])) {
  436.             return $this->redirectConfig[$view]['status_code'];
  437.         }
  438.         return 302;
  439.     }
  440. }