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