Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / vendor / symfony / browser-kit / Response.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\BrowserKit;
13
14 /**
15  * @author Fabien Potencier <fabien@symfony.com>
16  */
17 class Response
18 {
19     protected $content;
20     protected $status;
21     protected $headers;
22
23     /**
24      * The headers array is a set of key/value pairs. If a header is present multiple times
25      * then the value is an array of all the values.
26      *
27      * @param string $content The content of the response
28      * @param int    $status  The response status code
29      * @param array  $headers An array of headers
30      */
31     public function __construct($content = '', $status = 200, array $headers = array())
32     {
33         $this->content = $content;
34         $this->status = $status;
35         $this->headers = $headers;
36     }
37
38     /**
39      * Converts the response object to string containing all headers and the response content.
40      *
41      * @return string The response with headers and content
42      */
43     public function __toString()
44     {
45         $headers = '';
46         foreach ($this->headers as $name => $value) {
47             if (is_string($value)) {
48                 $headers .= $this->buildHeader($name, $value);
49             } else {
50                 foreach ($value as $headerValue) {
51                     $headers .= $this->buildHeader($name, $headerValue);
52                 }
53             }
54         }
55
56         return $headers."\n".$this->content;
57     }
58
59     /**
60      * Returns the build header line.
61      *
62      * @param string $name  The header name
63      * @param string $value The header value
64      *
65      * @return string The built header line
66      */
67     protected function buildHeader($name, $value)
68     {
69         return sprintf("%s: %s\n", $name, $value);
70     }
71
72     /**
73      * Gets the response content.
74      *
75      * @return string The response content
76      */
77     public function getContent()
78     {
79         return $this->content;
80     }
81
82     /**
83      * Gets the response status code.
84      *
85      * @return int The response status code
86      */
87     public function getStatus()
88     {
89         return $this->status;
90     }
91
92     /**
93      * Gets the response headers.
94      *
95      * @return array The response headers
96      */
97     public function getHeaders()
98     {
99         return $this->headers;
100     }
101
102     /**
103      * Gets a response header.
104      *
105      * @param string $header The header name
106      * @param bool   $first  Whether to return the first value or all header values
107      *
108      * @return string|array The first header value if $first is true, an array of values otherwise
109      */
110     public function getHeader($header, $first = true)
111     {
112         $normalizedHeader = str_replace('-', '_', strtolower($header));
113         foreach ($this->headers as $key => $value) {
114             if (str_replace('-', '_', strtolower($key)) === $normalizedHeader) {
115                 if ($first) {
116                     return is_array($value) ? (count($value) ? $value[0] : '') : $value;
117                 }
118
119                 return is_array($value) ? $value : array($value);
120             }
121         }
122
123         return $first ? null : array();
124     }
125 }