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