|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Deviantintegral\Har\Adapter\Psr7; |
| 6 | + |
| 7 | +use Deviantintegral\Har\Cookie; |
| 8 | +use Deviantintegral\Har\Params; |
| 9 | +use Deviantintegral\Har\PostData; |
| 10 | +use Psr\Http\Message\ServerRequestInterface; |
| 11 | +use Psr\Http\Message\StreamInterface; |
| 12 | +use Psr\Http\Message\UriInterface; |
| 13 | + |
| 14 | +/** |
| 15 | + * Adapts PSR-7 ServerRequests. |
| 16 | + * |
| 17 | + * Only features supported by HAR spec are implemented. Unsupported features |
| 18 | + * will throw a \LogicException if they involve possible data loss. |
| 19 | + */ |
| 20 | +class ServerRequest extends Request implements ServerRequestInterface |
| 21 | +{ |
| 22 | + public function getServerParams(): array |
| 23 | + { |
| 24 | + return []; |
| 25 | + } |
| 26 | + |
| 27 | + public function getCookieParams(): array |
| 28 | + { |
| 29 | + $request = $this->getHarRequest(); |
| 30 | + $cookieParams = []; |
| 31 | + foreach ($request->getCookies() as $cookie) { |
| 32 | + $cookieParams[$cookie->getName()] = $cookie->getValue(); |
| 33 | + } |
| 34 | + |
| 35 | + return $cookieParams; |
| 36 | + } |
| 37 | + |
| 38 | + public function withCookieParams(array $cookies): ServerRequestInterface |
| 39 | + { |
| 40 | + $request = clone $this->getHarRequest(); |
| 41 | + $harCookies = []; |
| 42 | + foreach ($cookies as $name => $value) { |
| 43 | + $harCookies[] = (new Cookie()) |
| 44 | + ->setName($name) |
| 45 | + ->setValue($value); |
| 46 | + } |
| 47 | + $request->setCookies($harCookies); |
| 48 | + |
| 49 | + return new static($request); |
| 50 | + } |
| 51 | + |
| 52 | + public function getQueryParams(): array |
| 53 | + { |
| 54 | + $request = $this->getHarRequest(); |
| 55 | + $queryParams = []; |
| 56 | + foreach ($request->getQueryString() as $param) { |
| 57 | + $queryParams[$param->getName()] = $param->getValue(); |
| 58 | + } |
| 59 | + |
| 60 | + return $queryParams; |
| 61 | + } |
| 62 | + |
| 63 | + public function withQueryParams(array $query): ServerRequestInterface |
| 64 | + { |
| 65 | + $request = clone $this->getHarRequest(); |
| 66 | + $harParams = []; |
| 67 | + foreach ($query as $name => $value) { |
| 68 | + $harParams[] = (new Params()) |
| 69 | + ->setName($name) |
| 70 | + ->setValue((string) $value); |
| 71 | + } |
| 72 | + $request->setQueryString($harParams); |
| 73 | + |
| 74 | + return new static($request); |
| 75 | + } |
| 76 | + |
| 77 | + public function getUploadedFiles(): array |
| 78 | + { |
| 79 | + return []; |
| 80 | + } |
| 81 | + |
| 82 | + public function withUploadedFiles(array $uploadedFiles): ServerRequestInterface |
| 83 | + { |
| 84 | + throw new \LogicException('Uploaded files are not supported.'); |
| 85 | + } |
| 86 | + |
| 87 | + public function getParsedBody() |
| 88 | + { |
| 89 | + $request = $this->getHarRequest(); |
| 90 | + |
| 91 | + if (!$request->hasPostData()) { |
| 92 | + return null; |
| 93 | + } |
| 94 | + |
| 95 | + $postData = $request->getPostData(); |
| 96 | + if ($postData->hasParams()) { |
| 97 | + $parsedBody = []; |
| 98 | + foreach ($postData->getParams() as $param) { |
| 99 | + $parsedBody[$param->getName()] = $param->getValue(); |
| 100 | + } |
| 101 | + |
| 102 | + return $parsedBody; |
| 103 | + } |
| 104 | + |
| 105 | + if ($postData->hasText()) { |
| 106 | + // Try to parse as form data if content type suggests it |
| 107 | + $contentType = $postData->getMimeType(); |
| 108 | + if ('application/x-www-form-urlencoded' === $contentType) { |
| 109 | + $parsedBody = []; |
| 110 | + parse_str($postData->getText(), $parsedBody); |
| 111 | + |
| 112 | + return $parsedBody; |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + return null; |
| 117 | + } |
| 118 | + |
| 119 | + public function withParsedBody($data): ServerRequestInterface |
| 120 | + { |
| 121 | + if (!\is_array($data) && !\is_object($data) && null !== $data) { |
| 122 | + throw new \InvalidArgumentException('Parsed body must be an array, object, or null.'); |
| 123 | + } |
| 124 | + |
| 125 | + $request = clone $this->getHarRequest(); |
| 126 | + |
| 127 | + if (\is_array($data) || \is_object($data)) { |
| 128 | + $postData = new PostData(); |
| 129 | + $harParams = []; |
| 130 | + foreach ($data as $name => $value) { |
| 131 | + $harParams[] = (new Params()) |
| 132 | + ->setName($name) |
| 133 | + ->setValue((string) $value); |
| 134 | + } |
| 135 | + $postData->setParams($harParams); |
| 136 | + $request->setPostData($postData); |
| 137 | + } elseif (null === $data) { |
| 138 | + // Clear post data |
| 139 | + $request->setPostData(new PostData()); |
| 140 | + } |
| 141 | + |
| 142 | + return new static($request); |
| 143 | + } |
| 144 | + |
| 145 | + public function getAttributes(): array |
| 146 | + { |
| 147 | + return []; |
| 148 | + } |
| 149 | + |
| 150 | + public function getAttribute($name, $default = null) |
| 151 | + { |
| 152 | + return $default; |
| 153 | + } |
| 154 | + |
| 155 | + public function withAttribute($name, $value): ServerRequestInterface |
| 156 | + { |
| 157 | + throw new \LogicException('Attributes are not supported.'); |
| 158 | + } |
| 159 | + |
| 160 | + public function withoutAttribute($name): ServerRequestInterface |
| 161 | + { |
| 162 | + // Attributes are not part of HAR spec, return unchanged clone |
| 163 | + return new static($this->getHarRequest()); |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * Override parent methods to return ServerRequestInterface. |
| 168 | + */ |
| 169 | + public function withMethod($method): ServerRequestInterface |
| 170 | + { |
| 171 | + $parent = parent::withMethod($method); |
| 172 | + |
| 173 | + return new static($parent->getHarRequest()); |
| 174 | + } |
| 175 | + |
| 176 | + public function withUri(UriInterface $uri, $preserveHost = false): ServerRequestInterface |
| 177 | + { |
| 178 | + $parent = parent::withUri($uri, $preserveHost); |
| 179 | + |
| 180 | + return new static($parent->getHarRequest()); |
| 181 | + } |
| 182 | + |
| 183 | + public function withRequestTarget($requestTarget): ServerRequestInterface |
| 184 | + { |
| 185 | + $parent = parent::withRequestTarget($requestTarget); |
| 186 | + |
| 187 | + return new static($parent->getHarRequest()); |
| 188 | + } |
| 189 | + |
| 190 | + public function withBody(StreamInterface $body): ServerRequestInterface |
| 191 | + { |
| 192 | + $parent = parent::withBody($body); |
| 193 | + |
| 194 | + return new static($parent->getHarRequest()); |
| 195 | + } |
| 196 | + |
| 197 | + public function withHeader($name, $value): ServerRequestInterface |
| 198 | + { |
| 199 | + $parent = parent::withHeader($name, $value); |
| 200 | + |
| 201 | + return new static($parent->getHarRequest()); |
| 202 | + } |
| 203 | + |
| 204 | + public function withAddedHeader($name, $value): ServerRequestInterface |
| 205 | + { |
| 206 | + $parent = parent::withAddedHeader($name, $value); |
| 207 | + |
| 208 | + return new static($parent->getHarRequest()); |
| 209 | + } |
| 210 | + |
| 211 | + public function withoutHeader($name): ServerRequestInterface |
| 212 | + { |
| 213 | + $parent = parent::withoutHeader($name); |
| 214 | + |
| 215 | + return new static($parent->getHarRequest()); |
| 216 | + } |
| 217 | + |
| 218 | + public function withProtocolVersion($version): ServerRequestInterface |
| 219 | + { |
| 220 | + $parent = parent::withProtocolVersion($version); |
| 221 | + |
| 222 | + return new static($parent->getHarRequest()); |
| 223 | + } |
| 224 | +} |
0 commit comments