vendor/friendsofsymfony/rest-bundle/View/View.php line 253

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the FOSRestBundle package.
  4.  *
  5.  * (c) FriendsOfSymfony <http://friendsofsymfony.github.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 FOS\RestBundle\View;
  11. use FOS\RestBundle\Context\Context;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\Templating\TemplateReferenceInterface;
  14. /**
  15.  * Default View implementation.
  16.  *
  17.  * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  18.  * @author Lukas K. Smith <smith@pooteeweet.org>
  19.  *
  20.  * @final since 2.8
  21.  */
  22. class View
  23. {
  24.     /**
  25.      * @var mixed|null
  26.      */
  27.     private $data;
  28.     /**
  29.      * @var int|null
  30.      */
  31.     private $statusCode;
  32.     /**
  33.      * @var mixed|null
  34.      */
  35.     private $templateData = [];
  36.     /**
  37.      * @var TemplateReference|string|null
  38.      */
  39.     private $template;
  40.     /**
  41.      * @var string|null
  42.      */
  43.     private $templateVar;
  44.     /**
  45.      * @var string|null
  46.      */
  47.     private $engine;
  48.     /**
  49.      * @var string|null
  50.      */
  51.     private $format;
  52.     /**
  53.      * @var string|null
  54.      */
  55.     private $location;
  56.     /**
  57.      * @var string|null
  58.      */
  59.     private $route;
  60.     /**
  61.      * @var array|null
  62.      */
  63.     private $routeParameters;
  64.     /**
  65.      * @var Context
  66.      */
  67.     private $context;
  68.     /**
  69.      * @var Response
  70.      */
  71.     private $response;
  72.     /**
  73.      * @param int|null $statusCode
  74.      *
  75.      * @return static
  76.      */
  77.     public static function create($data null$statusCode null, array $headers = [])
  78.     {
  79.         return new static($data$statusCode$headers);
  80.     }
  81.     /**
  82.      * @param string $url
  83.      * @param int    $statusCode
  84.      *
  85.      * @return static
  86.      */
  87.     public static function createRedirect($url$statusCode Response::HTTP_FOUND, array $headers = [])
  88.     {
  89.         $view = static::create(null$statusCode$headers);
  90.         $view->setLocation($url);
  91.         return $view;
  92.     }
  93.     /**
  94.      * @param string $route
  95.      * @param int    $statusCode
  96.      *
  97.      * @return static
  98.      */
  99.     public static function createRouteRedirect(
  100.         $route,
  101.         array $parameters = [],
  102.         $statusCode Response::HTTP_FOUND,
  103.         array $headers = []
  104.     ) {
  105.         $view = static::create(null$statusCode$headers);
  106.         $view->setRoute($route);
  107.         $view->setRouteParameters($parameters);
  108.         return $view;
  109.     }
  110.     /**
  111.      * @param int $statusCode
  112.      */
  113.     public function __construct($data null$statusCode null, array $headers = [])
  114.     {
  115.         $this->setData($data);
  116.         $this->setStatusCode($statusCode);
  117.         $this->setTemplateVar('data'false);
  118.         if (!empty($headers)) {
  119.             $this->getResponse()->headers->replace($headers);
  120.         }
  121.     }
  122.     /**
  123.      * @return View
  124.      */
  125.     public function setData($data)
  126.     {
  127.         $this->data $data;
  128.         return $this;
  129.     }
  130.     /**
  131.      * @deprecated since 2.8
  132.      *
  133.      * @param array|callable $data
  134.      *
  135.      * @return View
  136.      */
  137.     public function setTemplateData($data = [])
  138.     {
  139.         if (=== func_num_args() || func_get_arg(1)) {
  140.             @trigger_error(sprintf('The %s() method is deprecated since FOSRestBundle 2.8.'__METHOD__), E_USER_DEPRECATED);
  141.         }
  142.         $this->templateData $data;
  143.         return $this;
  144.     }
  145.     /**
  146.      * @param string $name
  147.      * @param string $value
  148.      *
  149.      * @return View
  150.      */
  151.     public function setHeader($name$value)
  152.     {
  153.         $this->getResponse()->headers->set($name$value);
  154.         return $this;
  155.     }
  156.     /**
  157.      * @return View
  158.      */
  159.     public function setHeaders(array $headers)
  160.     {
  161.         $this->getResponse()->headers->replace($headers);
  162.         return $this;
  163.     }
  164.     /**
  165.      * @param int|null $code
  166.      *
  167.      * @return View
  168.      */
  169.     public function setStatusCode($code)
  170.     {
  171.         if (null !== $code) {
  172.             $this->statusCode $code;
  173.         }
  174.         return $this;
  175.     }
  176.     /**
  177.      * @return View
  178.      */
  179.     public function setContext(Context $context)
  180.     {
  181.         $this->context $context;
  182.         return $this;
  183.     }
  184.     /**
  185.      * @deprecated since 2.8
  186.      *
  187.      * @param string|TemplateReferenceInterface $template
  188.      *
  189.      * @return View
  190.      *
  191.      * @throws \InvalidArgumentException if the template is neither a string nor an instance of TemplateReferenceInterface
  192.      */
  193.     public function setTemplate($template)
  194.     {
  195.         if (=== func_num_args() || func_get_arg(1)) {
  196.             @trigger_error(sprintf('The %s() method is deprecated since FOSRestBundle 2.8.'__METHOD__), E_USER_DEPRECATED);
  197.         }
  198.         if (!(is_string($template) || $template instanceof TemplateReferenceInterface)) {
  199.             throw new \InvalidArgumentException('The template should be a string or implement TemplateReferenceInterface');
  200.         }
  201.         $this->template $template;
  202.         return $this;
  203.     }
  204.     /**
  205.      * @deprecated since 2.8
  206.      *
  207.      * @param string $templateVar
  208.      *
  209.      * @return View
  210.      */
  211.     public function setTemplateVar($templateVar)
  212.     {
  213.         if (=== func_num_args() || func_get_arg(1)) {
  214.             @trigger_error(sprintf('The %s() method is deprecated since FOSRestBundle 2.8.'__METHOD__), E_USER_DEPRECATED);
  215.         }
  216.         $this->templateVar $templateVar;
  217.         return $this;
  218.     }
  219.     /**
  220.      * @deprecated since 2.8
  221.      *
  222.      * @param string $engine
  223.      *
  224.      * @return View
  225.      */
  226.     public function setEngine($engine)
  227.     {
  228.         @trigger_error(sprintf('The %s() method is deprecated since FOSRestBundle 2.8.'__METHOD__), E_USER_DEPRECATED);
  229.         $this->engine $engine;
  230.         return $this;
  231.     }
  232.     /**
  233.      * @param string $format
  234.      *
  235.      * @return View
  236.      */
  237.     public function setFormat($format)
  238.     {
  239.         $this->format $format;
  240.         return $this;
  241.     }
  242.     /**
  243.      * @param string $location
  244.      *
  245.      * @return View
  246.      */
  247.     public function setLocation($location)
  248.     {
  249.         $this->location $location;
  250.         $this->route null;
  251.         return $this;
  252.     }
  253.     /**
  254.      * Sets the route (implicitly removes the location).
  255.      *
  256.      * @param string $route
  257.      *
  258.      * @return View
  259.      */
  260.     public function setRoute($route)
  261.     {
  262.         $this->route $route;
  263.         $this->location null;
  264.         return $this;
  265.     }
  266.     /**
  267.      * @param array $parameters
  268.      *
  269.      * @return View
  270.      */
  271.     public function setRouteParameters($parameters)
  272.     {
  273.         $this->routeParameters $parameters;
  274.         return $this;
  275.     }
  276.     /**
  277.      * @return View
  278.      */
  279.     public function setResponse(Response $response)
  280.     {
  281.         $this->response $response;
  282.         return $this;
  283.     }
  284.     public function getData()
  285.     {
  286.         return $this->data;
  287.     }
  288.     /**
  289.      * @deprecated since 2.8
  290.      *
  291.      * @return mixed|null
  292.      */
  293.     public function getTemplateData()
  294.     {
  295.         if (=== func_num_args() || func_get_arg(0)) {
  296.             @trigger_error(sprintf('The %s() method is deprecated since FOSRestBundle 2.8.'__METHOD__), E_USER_DEPRECATED);
  297.         }
  298.         return $this->templateData;
  299.     }
  300.     /**
  301.      * @return int|null
  302.      */
  303.     public function getStatusCode()
  304.     {
  305.         return $this->statusCode;
  306.     }
  307.     /**
  308.      * @return array
  309.      */
  310.     public function getHeaders()
  311.     {
  312.         return $this->getResponse()->headers->all();
  313.     }
  314.     /**
  315.      * @deprecated since 2.8
  316.      *
  317.      * @return TemplateReferenceInterface|string|null
  318.      */
  319.     public function getTemplate()
  320.     {
  321.         if (=== func_num_args() || func_get_arg(0)) {
  322.             @trigger_error(sprintf('The %s() method is deprecated since FOSRestBundle 2.8.'__METHOD__), E_USER_DEPRECATED);
  323.         }
  324.         return $this->template;
  325.     }
  326.     /**
  327.      * @deprecated since 2.8
  328.      *
  329.      * @return string|null
  330.      */
  331.     public function getTemplateVar()
  332.     {
  333.         if (=== func_num_args() || func_get_arg(0)) {
  334.             @trigger_error(sprintf('The %s() method is deprecated since FOSRestBundle 2.8.'__METHOD__), E_USER_DEPRECATED);
  335.         }
  336.         return $this->templateVar;
  337.     }
  338.     /**
  339.      * @deprecated since 2.8
  340.      *
  341.      * @return string|null
  342.      */
  343.     public function getEngine()
  344.     {
  345.         @trigger_error(sprintf('The %s() method is deprecated since FOSRestBundle 2.8.'__METHOD__), E_USER_DEPRECATED);
  346.         return $this->engine;
  347.     }
  348.     /**
  349.      * @return string|null
  350.      */
  351.     public function getFormat()
  352.     {
  353.         return $this->format;
  354.     }
  355.     /**
  356.      * @return string|null
  357.      */
  358.     public function getLocation()
  359.     {
  360.         return $this->location;
  361.     }
  362.     /**
  363.      * @return string|null
  364.      */
  365.     public function getRoute()
  366.     {
  367.         return $this->route;
  368.     }
  369.     /**
  370.      * @return array|null
  371.      */
  372.     public function getRouteParameters()
  373.     {
  374.         return $this->routeParameters;
  375.     }
  376.     /**
  377.      * @return Response
  378.      */
  379.     public function getResponse()
  380.     {
  381.         if (null === $this->response) {
  382.             $this->response = new Response();
  383.             if (null !== ($code $this->getStatusCode())) {
  384.                 $this->response->setStatusCode($code);
  385.             }
  386.         }
  387.         return $this->response;
  388.     }
  389.     /**
  390.      * @return Context
  391.      */
  392.     public function getContext()
  393.     {
  394.         if (null === $this->context) {
  395.             $this->context = new Context();
  396.         }
  397.         return $this->context;
  398.     }
  399. }