ResponseBuilder.php
149 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaHttp\Message\Builder; |
| 4 | |
| 5 | use AmeliaVendor\Psr\Http\Message\ResponseInterface; |
| 6 | |
| 7 | /** |
| 8 | * Fills response object with values. |
| 9 | */ |
| 10 | class ResponseBuilder |
| 11 | { |
| 12 | /** |
| 13 | * The response to be built. |
| 14 | * |
| 15 | * @var ResponseInterface |
| 16 | */ |
| 17 | protected $response; |
| 18 | |
| 19 | /** |
| 20 | * Create builder for the given response. |
| 21 | * |
| 22 | * @param ResponseInterface $response |
| 23 | */ |
| 24 | public function __construct(ResponseInterface $response) |
| 25 | { |
| 26 | $this->response = $response; |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Return response. |
| 31 | * |
| 32 | * @return ResponseInterface |
| 33 | */ |
| 34 | public function getResponse() |
| 35 | { |
| 36 | return $this->response; |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Add headers represented by an array of header lines. |
| 41 | * |
| 42 | * @param string[] $headers Response headers as array of header lines. |
| 43 | * |
| 44 | * @return $this |
| 45 | * |
| 46 | * @throws \UnexpectedValueException For invalid header values. |
| 47 | * @throws \InvalidArgumentException For invalid status code arguments. |
| 48 | */ |
| 49 | public function setHeadersFromArray(array $headers) |
| 50 | { |
| 51 | $status = array_shift($headers); |
| 52 | $this->setStatus($status); |
| 53 | |
| 54 | foreach ($headers as $headerLine) { |
| 55 | $headerLine = trim($headerLine); |
| 56 | if ('' === $headerLine) { |
| 57 | continue; |
| 58 | } |
| 59 | |
| 60 | $this->addHeader($headerLine); |
| 61 | } |
| 62 | |
| 63 | return $this; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Add headers represented by a single string. |
| 68 | * |
| 69 | * @param string $headers Response headers as single string. |
| 70 | * |
| 71 | * @return $this |
| 72 | * |
| 73 | * @throws \InvalidArgumentException if $headers is not a string on object with __toString() |
| 74 | * @throws \UnexpectedValueException For invalid header values. |
| 75 | */ |
| 76 | public function setHeadersFromString($headers) |
| 77 | { |
| 78 | if (!(is_string($headers) |
| 79 | || (is_object($headers) && method_exists($headers, '__toString'))) |
| 80 | ) { |
| 81 | throw new \InvalidArgumentException( |
| 82 | sprintf( |
| 83 | '%s expects parameter 1 to be a string, %s given', |
| 84 | __METHOD__, |
| 85 | is_object($headers) ? get_class($headers) : gettype($headers) |
| 86 | ) |
| 87 | ); |
| 88 | } |
| 89 | |
| 90 | $this->setHeadersFromArray(explode("\r\n", $headers)); |
| 91 | |
| 92 | return $this; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Set response status from a status string. |
| 97 | * |
| 98 | * @param string $statusLine Response status as a string. |
| 99 | * |
| 100 | * @return $this |
| 101 | * |
| 102 | * @throws \InvalidArgumentException For invalid status line. |
| 103 | */ |
| 104 | public function setStatus($statusLine) |
| 105 | { |
| 106 | $parts = explode(' ', $statusLine, 3); |
| 107 | if (count($parts) < 2 || 0 !== strpos(strtolower($parts[0]), 'http/')) { |
| 108 | throw new \InvalidArgumentException( |
| 109 | sprintf('"%s" is not a valid HTTP status line', $statusLine) |
| 110 | ); |
| 111 | } |
| 112 | |
| 113 | $reasonPhrase = count($parts) > 2 ? $parts[2] : ''; |
| 114 | $this->response = $this->response |
| 115 | ->withStatus((int) $parts[1], $reasonPhrase) |
| 116 | ->withProtocolVersion(substr($parts[0], 5)); |
| 117 | |
| 118 | return $this; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Add header represented by a string. |
| 123 | * |
| 124 | * @param string $headerLine Response header as a string. |
| 125 | * |
| 126 | * @return $this |
| 127 | * |
| 128 | * @throws \InvalidArgumentException For invalid header names or values. |
| 129 | */ |
| 130 | public function addHeader($headerLine) |
| 131 | { |
| 132 | $parts = explode(':', $headerLine, 2); |
| 133 | if (2 !== count($parts)) { |
| 134 | throw new \InvalidArgumentException( |
| 135 | sprintf('"%s" is not a valid HTTP header line', $headerLine) |
| 136 | ); |
| 137 | } |
| 138 | $name = trim($parts[0]); |
| 139 | $value = trim($parts[1]); |
| 140 | if ($this->response->hasHeader($name)) { |
| 141 | $this->response = $this->response->withAddedHeader($name, $value); |
| 142 | } else { |
| 143 | $this->response = $this->response->withHeader($name, $value); |
| 144 | } |
| 145 | |
| 146 | return $this; |
| 147 | } |
| 148 | } |
| 149 |