[ Index ] |
PHP Cross Reference of phpBB-3.2.11-deutsch |
[Summary view] [Print] [Text view]
1 <?php 2 namespace GuzzleHttp\Message; 3 4 use GuzzleHttp\Stream\StreamInterface; 5 6 abstract class AbstractMessage implements MessageInterface 7 { 8 /** @var array HTTP header collection */ 9 private $headers = []; 10 11 /** @var array mapping a lowercase header name to its name over the wire */ 12 private $headerNames = []; 13 14 /** @var StreamInterface Message body */ 15 private $body; 16 17 /** @var string HTTP protocol version of the message */ 18 private $protocolVersion = '1.1'; 19 20 public function __toString() 21 { 22 return static::getStartLineAndHeaders($this) 23 . "\r\n\r\n" . $this->getBody(); 24 } 25 26 public function getProtocolVersion() 27 { 28 return $this->protocolVersion; 29 } 30 31 public function getBody() 32 { 33 return $this->body; 34 } 35 36 public function setBody(StreamInterface $body = null) 37 { 38 if ($body === null) { 39 // Setting a null body will remove the body of the request 40 $this->removeHeader('Content-Length'); 41 $this->removeHeader('Transfer-Encoding'); 42 } 43 44 $this->body = $body; 45 } 46 47 public function addHeader($header, $value) 48 { 49 if (is_array($value)) { 50 $current = array_merge($this->getHeaderAsArray($header), $value); 51 } else { 52 $current = $this->getHeaderAsArray($header); 53 $current[] = (string) $value; 54 } 55 56 $this->setHeader($header, $current); 57 } 58 59 public function addHeaders(array $headers) 60 { 61 foreach ($headers as $name => $header) { 62 $this->addHeader($name, $header); 63 } 64 } 65 66 public function getHeader($header) 67 { 68 $name = strtolower($header); 69 return isset($this->headers[$name]) 70 ? implode(', ', $this->headers[$name]) 71 : ''; 72 } 73 74 public function getHeaderAsArray($header) 75 { 76 $name = strtolower($header); 77 return isset($this->headers[$name]) ? $this->headers[$name] : []; 78 } 79 80 public function getHeaders() 81 { 82 $headers = []; 83 foreach ($this->headers as $name => $values) { 84 $headers[$this->headerNames[$name]] = $values; 85 } 86 87 return $headers; 88 } 89 90 public function setHeader($header, $value) 91 { 92 $header = trim($header); 93 $name = strtolower($header); 94 $this->headerNames[$name] = $header; 95 96 if (is_array($value)) { 97 foreach ($value as &$v) { 98 $v = trim($v); 99 } 100 $this->headers[$name] = $value; 101 } else { 102 $this->headers[$name] = [trim($value)]; 103 } 104 } 105 106 public function setHeaders(array $headers) 107 { 108 $this->headers = $this->headerNames = []; 109 foreach ($headers as $key => $value) { 110 $this->addHeader($key, $value); 111 } 112 } 113 114 public function hasHeader($header) 115 { 116 return isset($this->headers[strtolower($header)]); 117 } 118 119 public function removeHeader($header) 120 { 121 $name = strtolower($header); 122 unset($this->headers[$name], $this->headerNames[$name]); 123 } 124 125 /** 126 * Parse an array of header values containing ";" separated data into an 127 * array of associative arrays representing the header key value pair 128 * data of the header. When a parameter does not contain a value, but just 129 * contains a key, this function will inject a key with a '' string value. 130 * 131 * @param MessageInterface $message That contains the header 132 * @param string $header Header to retrieve from the message 133 * 134 * @return array Returns the parsed header values. 135 */ 136 public static function parseHeader(MessageInterface $message, $header) 137 { 138 static $trimmed = "\"' \n\t\r"; 139 $params = $matches = []; 140 141 foreach (self::normalizeHeader($message, $header) as $val) { 142 $part = []; 143 foreach (preg_split('/;(?=([^"]*"[^"]*")*[^"]*$)/', $val) as $kvp) { 144 if (preg_match_all('/<[^>]+>|[^=]+/', $kvp, $matches)) { 145 $m = $matches[0]; 146 if (isset($m[1])) { 147 $part[trim($m[0], $trimmed)] = trim($m[1], $trimmed); 148 } else { 149 $part[] = trim($m[0], $trimmed); 150 } 151 } 152 } 153 if ($part) { 154 $params[] = $part; 155 } 156 } 157 158 return $params; 159 } 160 161 /** 162 * Converts an array of header values that may contain comma separated 163 * headers into an array of headers with no comma separated values. 164 * 165 * @param MessageInterface $message That contains the header 166 * @param string $header Header to retrieve from the message 167 * 168 * @return array Returns the normalized header field values. 169 */ 170 public static function normalizeHeader(MessageInterface $message, $header) 171 { 172 $h = $message->getHeaderAsArray($header); 173 for ($i = 0, $total = count($h); $i < $total; $i++) { 174 if (strpos($h[$i], ',') === false) { 175 continue; 176 } 177 foreach (preg_split('/,(?=([^"]*"[^"]*")*[^"]*$)/', $h[$i]) as $v) { 178 $h[] = trim($v); 179 } 180 unset($h[$i]); 181 } 182 183 return $h; 184 } 185 186 /** 187 * Gets the start-line and headers of a message as a string 188 * 189 * @param MessageInterface $message 190 * 191 * @return string 192 */ 193 public static function getStartLineAndHeaders(MessageInterface $message) 194 { 195 return static::getStartLine($message) 196 . self::getHeadersAsString($message); 197 } 198 199 /** 200 * Gets the headers of a message as a string 201 * 202 * @param MessageInterface $message 203 * 204 * @return string 205 */ 206 public static function getHeadersAsString(MessageInterface $message) 207 { 208 $result = ''; 209 foreach ($message->getHeaders() as $name => $values) { 210 $result .= "\r\n{$name}: " . implode(', ', $values); 211 } 212 213 return $result; 214 } 215 216 /** 217 * Gets the start line of a message 218 * 219 * @param MessageInterface $message 220 * 221 * @return string 222 * @throws \InvalidArgumentException 223 */ 224 public static function getStartLine(MessageInterface $message) 225 { 226 if ($message instanceof RequestInterface) { 227 return trim($message->getMethod() . ' ' 228 . $message->getResource()) 229 . ' HTTP/' . $message->getProtocolVersion(); 230 } elseif ($message instanceof ResponseInterface) { 231 return 'HTTP/' . $message->getProtocolVersion() . ' ' 232 . $message->getStatusCode() . ' ' 233 . $message->getReasonPhrase(); 234 } else { 235 throw new \InvalidArgumentException('Unknown message type'); 236 } 237 } 238 239 /** 240 * Accepts and modifies the options provided to the message in the 241 * constructor. 242 * 243 * Can be overridden in subclasses as necessary. 244 * 245 * @param array $options Options array passed by reference. 246 */ 247 protected function handleOptions(array &$options) 248 { 249 if (isset($options['protocol_version'])) { 250 $this->protocolVersion = $options['protocol_version']; 251 } 252 } 253 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Nov 11 20:33:01 2020 | Cross-referenced by PHPXref 0.7.1 |