requestTarget = $request->getRequestTarget(); $target->method = $request->getMethod(); $target->uri = self::clone($request->getUri()); $target->body = self::clone($request->getBody()); $target->parsedBody = self::clone($request->getParsedBody()); $target->queryParams = $request->getQueryParams(); $target->serverParams = $request->getServerParams(); $target->headers = $request->getHeaders(); $target->attributes = array_filter( $request->getAttributes(), static fn(string $name) => in_array($name, self::KEEP_ATTRIBUTE_NAMES, true), ARRAY_FILTER_USE_KEY ); return $target; } public static function buildFromArray(array $data): self { $target = new self(); $target->requestTarget = $data['requestTarget']; $target->method = $data['method']; $target->uri = new Uri($data['uri']); $target->body = new Stream('php://temp', 'w+b'); $target->body->write($data['body']['contents']); $target->parsedBody = $data['parsedBody']; $target->queryParams = $data['queryParams']; $target->serverParams = $data['serverParams']; $target->headers = $data['headers']; $target->attributes = $data['attributes'] ?? []; return $target; } protected static function clone($value) { if (is_object($value)) { return clone $value; } return $value; } protected function __construct() { // avoid creating class instances directly from external } protected function __clone() { // avoid cloning class instances directly from external } public function jsonSerialize(): array { return [ 'class' => self::class, 'requestTarget' => $this->requestTarget, 'method' => $this->method, 'uri' => (string)$this->uri, 'body' => [ 'contents' => (string)$this->body, ], 'parsedBody' => $this->parsedBody, 'queryParams' => $this->queryParams, 'serverParams' => $this->serverParams, 'headers' => $this->headers, 'attributes' => $this->attributes, ]; } /** * Applies instructions to given ServerRequest ("replaying the request"). */ public function applyTo(ServerRequestInterface $request): ServerRequestInterface { $request = $request ->withRequestTarget($this->requestTarget) ->withMethod($this->method) ->withUri($this->uri) ->withBody($this->body) ->withParsedBody($this->parsedBody) ->withQueryParams($this->queryParams); foreach ($this->attributes as $name => $value) { $request = $request->withAttribute($name, $value); } return $request; } public function getRequestTarget(): string { return $this->requestTarget; } public function getMethod(): string { return $this->method; } public function getUri(): UriInterface { return $this->uri; } public function getBody(): StreamInterface { return $this->body; } public function getParsedBody(): ?array { return $this->parsedBody; } public function getQueryParams(): array { return $this->queryParams; } public function getServerParams(): array { return $this->serverParams; } public function getHeaders(): array { return $this->headers; } public function getAttributes(): array { return $this->attributes; } }