| [01a620f] | 1 | <?php
|
|---|
| 2 | namespace GuzzleHttp\Psr7;
|
|---|
| 3 |
|
|---|
| 4 | use Psr\Http\Message\StreamInterface;
|
|---|
| 5 |
|
|---|
| 6 | /**
|
|---|
| 7 | * Trait implementing functionality common to requests and responses.
|
|---|
| 8 | */
|
|---|
| 9 | trait MessageTrait
|
|---|
| 10 | {
|
|---|
| 11 | /** @var array Cached HTTP header collection with lowercase key to values */
|
|---|
| 12 | private $headers = [];
|
|---|
| 13 |
|
|---|
| 14 | /** @var array Actual key to list of values per header. */
|
|---|
| 15 | private $headerLines = [];
|
|---|
| 16 |
|
|---|
| 17 | /** @var string */
|
|---|
| 18 | private $protocol = '1.1';
|
|---|
| 19 |
|
|---|
| 20 | /** @var StreamInterface */
|
|---|
| 21 | private $stream;
|
|---|
| 22 |
|
|---|
| 23 | public function getProtocolVersion()
|
|---|
| 24 | {
|
|---|
| 25 | return $this->protocol;
|
|---|
| 26 | }
|
|---|
| 27 |
|
|---|
| 28 | public function withProtocolVersion($version)
|
|---|
| 29 | {
|
|---|
| 30 | if ($this->protocol === $version) {
|
|---|
| 31 | return $this;
|
|---|
| 32 | }
|
|---|
| 33 |
|
|---|
| 34 | $new = clone $this;
|
|---|
| 35 | $new->protocol = $version;
|
|---|
| 36 | return $new;
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 | public function getHeaders()
|
|---|
| 40 | {
|
|---|
| 41 | return $this->headerLines;
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | public function hasHeader($header)
|
|---|
| 45 | {
|
|---|
| 46 | return isset($this->headers[strtolower($header)]);
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | public function getHeader($header)
|
|---|
| 50 | {
|
|---|
| 51 | $name = strtolower($header);
|
|---|
| 52 | return isset($this->headers[$name]) ? $this->headers[$name] : [];
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | public function getHeaderLine($header)
|
|---|
| 56 | {
|
|---|
| 57 | return implode(', ', $this->getHeader($header));
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | public function withHeader($header, $value)
|
|---|
| 61 | {
|
|---|
| 62 | $new = clone $this;
|
|---|
| 63 | $header = trim($header);
|
|---|
| 64 | $name = strtolower($header);
|
|---|
| 65 |
|
|---|
| 66 | if (!is_array($value)) {
|
|---|
| 67 | $new->headers[$name] = [trim($value)];
|
|---|
| 68 | } else {
|
|---|
| 69 | $new->headers[$name] = $value;
|
|---|
| 70 | foreach ($new->headers[$name] as &$v) {
|
|---|
| 71 | $v = trim($v);
|
|---|
| 72 | }
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | // Remove the header lines.
|
|---|
| 76 | foreach (array_keys($new->headerLines) as $key) {
|
|---|
| 77 | if (strtolower($key) === $name) {
|
|---|
| 78 | unset($new->headerLines[$key]);
|
|---|
| 79 | }
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | // Add the header line.
|
|---|
| 83 | $new->headerLines[$header] = $new->headers[$name];
|
|---|
| 84 |
|
|---|
| 85 | return $new;
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | public function withAddedHeader($header, $value)
|
|---|
| 89 | {
|
|---|
| 90 | if (!$this->hasHeader($header)) {
|
|---|
| 91 | return $this->withHeader($header, $value);
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | $new = clone $this;
|
|---|
| 95 | $new->headers[strtolower($header)][] = $value;
|
|---|
| 96 | $new->headerLines[$header][] = $value;
|
|---|
| 97 | return $new;
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | public function withoutHeader($header)
|
|---|
| 101 | {
|
|---|
| 102 | if (!$this->hasHeader($header)) {
|
|---|
| 103 | return $this;
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | $new = clone $this;
|
|---|
| 107 | $name = strtolower($header);
|
|---|
| 108 | unset($new->headers[$name]);
|
|---|
| 109 |
|
|---|
| 110 | foreach (array_keys($new->headerLines) as $key) {
|
|---|
| 111 | if (strtolower($key) === $name) {
|
|---|
| 112 | unset($new->headerLines[$key]);
|
|---|
| 113 | }
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | return $new;
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | public function getBody()
|
|---|
| 120 | {
|
|---|
| 121 | if (!$this->stream) {
|
|---|
| 122 | $this->stream = stream_for('');
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | return $this->stream;
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | public function withBody(StreamInterface $body)
|
|---|
| 129 | {
|
|---|
| 130 | if ($body === $this->stream) {
|
|---|
| 131 | return $this;
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | $new = clone $this;
|
|---|
| 135 | $new->stream = $body;
|
|---|
| 136 | return $new;
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | private function setHeaders(array $headers)
|
|---|
| 140 | {
|
|---|
| 141 | $this->headerLines = $this->headers = [];
|
|---|
| 142 | foreach ($headers as $header => $value) {
|
|---|
| 143 | $header = trim($header);
|
|---|
| 144 | $name = strtolower($header);
|
|---|
| 145 | if (!is_array($value)) {
|
|---|
| 146 | $value = trim($value);
|
|---|
| 147 | $this->headers[$name][] = $value;
|
|---|
| 148 | $this->headerLines[$header][] = $value;
|
|---|
| 149 | } else {
|
|---|
| 150 | foreach ($value as $v) {
|
|---|
| 151 | $v = trim($v);
|
|---|
| 152 | $this->headers[$name][] = $v;
|
|---|
| 153 | $this->headerLines[$header][] = $v;
|
|---|
| 154 | }
|
|---|
| 155 | }
|
|---|
| 156 | }
|
|---|
| 157 | }
|
|---|
| 158 | }
|
|---|