comparison vendor/symfony/browser-kit/Response.php @ 0:4c8ae668cc8c

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