You are here

class ServerRequest in Zircon Profile 8

Same name in this branch
  1. 8 vendor/zendframework/zend-diactoros/src/ServerRequest.php \Zend\Diactoros\ServerRequest
  2. 8 vendor/symfony/psr-http-message-bridge/Tests/Fixtures/ServerRequest.php \Symfony\Bridge\PsrHttpMessage\Tests\Fixtures\ServerRequest
Same name and namespace in other branches
  1. 8.0 vendor/zendframework/zend-diactoros/src/ServerRequest.php \Zend\Diactoros\ServerRequest

Server-side HTTP request

Extends the Request definition to add methods for accessing incoming data, specifically server parameters, cookies, matched path parameters, query string arguments, body parameters, and upload file information.

"Attributes" are discovered via decomposing the request (and usually specifically the URI path), and typically will be injected by the application.

Requests are considered immutable; all methods that might change state are implemented such that they retain the internal state of the current message and return a new instance that contains the changed state.

Hierarchy

Expanded class hierarchy of ServerRequest

1 file declares its use of ServerRequest
DiactorosFactory.php in vendor/symfony/psr-http-message-bridge/Factory/DiactorosFactory.php

File

vendor/zendframework/zend-diactoros/src/ServerRequest.php, line 31

Namespace

Zend\Diactoros
View source
class ServerRequest implements ServerRequestInterface {
  use MessageTrait, RequestTrait;

  /**
   * @var array
   */
  private $attributes = [];

  /**
   * @var array
   */
  private $cookieParams = [];

  /**
   * @var null|array|object
   */
  private $parsedBody;

  /**
   * @var array
   */
  private $queryParams = [];

  /**
   * @var array
   */
  private $serverParams;

  /**
   * @var array
   */
  private $uploadedFiles;

  /**
   * @param array $serverParams Server parameters, typically from $_SERVER
   * @param array $uploadedFiles Upload file information, a tree of UploadedFiles
   * @param null|string $uri URI for the request, if any.
   * @param null|string $method HTTP method for the request, if any.
   * @param string|resource|StreamInterface $body Message body, if any.
   * @param array $headers Headers for the message, if any.
   * @throws InvalidArgumentException for any invalid value.
   */
  public function __construct(array $serverParams = [], array $uploadedFiles = [], $uri = null, $method = null, $body = 'php://input', array $headers = []) {
    $this
      ->validateUploadedFiles($uploadedFiles);
    $body = $this
      ->getStream($body);
    $this
      ->initialize($uri, $method, $body, $headers);
    $this->serverParams = $serverParams;
    $this->uploadedFiles = $uploadedFiles;
  }

  /**
   * {@inheritdoc}
   */
  public function getServerParams() {
    return $this->serverParams;
  }

  /**
   * {@inheritdoc}
   */
  public function getUploadedFiles() {
    return $this->uploadedFiles;
  }

  /**
   * {@inheritdoc}
   */
  public function withUploadedFiles(array $uploadedFiles) {
    $this
      ->validateUploadedFiles($uploadedFiles);
    $new = clone $this;
    $new->uploadedFiles = $uploadedFiles;
    return $new;
  }

  /**
   * {@inheritdoc}
   */
  public function getCookieParams() {
    return $this->cookieParams;
  }

  /**
   * {@inheritdoc}
   */
  public function withCookieParams(array $cookies) {
    $new = clone $this;
    $new->cookieParams = $cookies;
    return $new;
  }

  /**
   * {@inheritdoc}
   */
  public function getQueryParams() {
    return $this->queryParams;
  }

  /**
   * {@inheritdoc}
   */
  public function withQueryParams(array $query) {
    $new = clone $this;
    $new->queryParams = $query;
    return $new;
  }

  /**
   * {@inheritdoc}
   */
  public function getParsedBody() {
    return $this->parsedBody;
  }

  /**
   * {@inheritdoc}
   */
  public function withParsedBody($data) {
    $new = clone $this;
    $new->parsedBody = $data;
    return $new;
  }

  /**
   * {@inheritdoc}
   */
  public function getAttributes() {
    return $this->attributes;
  }

  /**
   * {@inheritdoc}
   */
  public function getAttribute($attribute, $default = null) {
    if (!array_key_exists($attribute, $this->attributes)) {
      return $default;
    }
    return $this->attributes[$attribute];
  }

  /**
   * {@inheritdoc}
   */
  public function withAttribute($attribute, $value) {
    $new = clone $this;
    $new->attributes[$attribute] = $value;
    return $new;
  }

  /**
   * {@inheritdoc}
   */
  public function withoutAttribute($attribute) {
    if (!isset($this->attributes[$attribute])) {
      return clone $this;
    }
    $new = clone $this;
    unset($new->attributes[$attribute]);
    return $new;
  }

  /**
   * Proxy to receive the request method.
   *
   * This overrides the parent functionality to ensure the method is never
   * empty; if no method is present, it returns 'GET'.
   *
   * @return string
   */
  public function getMethod() {
    if (empty($this->method)) {
      return 'GET';
    }
    return $this->method;
  }

  /**
   * Set the request method.
   *
   * Unlike the regular Request implementation, the server-side
   * normalizes the method to uppercase to ensure consistency
   * and make checking the method simpler.
   *
   * This methods returns a new instance.
   *
   * @param string $method
   * @return self
   */
  public function withMethod($method) {
    $this
      ->validateMethod($method);
    $new = clone $this;
    $new->method = $method;
    return $new;
  }

  /**
   * Set the body stream
   *
   * @param string|resource|StreamInterface $stream
   * @return StreamInterface
   */
  private function getStream($stream) {
    if ($stream === 'php://input') {
      return new PhpInputStream();
    }
    if (!is_string($stream) && !is_resource($stream) && !$stream instanceof StreamInterface) {
      throw new InvalidArgumentException('Stream must be a string stream resource identifier, ' . 'an actual stream resource, ' . 'or a Psr\\Http\\Message\\StreamInterface implementation');
    }
    if (!$stream instanceof StreamInterface) {
      return new Stream($stream, 'r');
    }
    return $stream;
  }

  /**
   * Recursively validate the structure in an uploaded files array.
   *
   * @param array $uploadedFiles
   * @throws InvalidArgumentException if any leaf is not an UploadedFileInterface instance.
   */
  private function validateUploadedFiles(array $uploadedFiles) {
    foreach ($uploadedFiles as $file) {
      if (is_array($file)) {
        $this
          ->validateUploadedFiles($file);
        continue;
      }
      if (!$file instanceof UploadedFileInterface) {
        throw new InvalidArgumentException('Invalid leaf in uploaded files structure');
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MessageTrait::$headerNames protected property Map of normalized header name to original name used to register header.
MessageTrait::$headers protected property List of all registered headers, as key => array of values.
MessageTrait::$protocol private property
MessageTrait::$stream private property
MessageTrait::arrayContainsOnlyStrings private function Test that an array contains only strings
MessageTrait::assertValidHeaderValue private static function Assert that the provided header values are valid.
MessageTrait::filterHeaders private function Filter a set of headers to ensure they are in the correct internal format.
MessageTrait::filterStringValue private static function Test if a value is a string
MessageTrait::getBody public function Gets the body of the message.
MessageTrait::getHeader public function Retrieves a message header value by the given case-insensitive name. 1
MessageTrait::getHeaderLine public function Retrieves a comma-separated string of the values for a single header.
MessageTrait::getHeaders public function Retrieves all message headers. 1
MessageTrait::getProtocolVersion public function Retrieves the HTTP protocol version as a string.
MessageTrait::hasHeader public function Checks if a header exists by the given case-insensitive name.
MessageTrait::withAddedHeader public function Return an instance with the specified header appended with the given value.
MessageTrait::withBody public function Return an instance with the specified message body.
MessageTrait::withHeader public function Return an instance with the provided header, replacing any existing values of any headers with the same case-insensitive name.
MessageTrait::withoutHeader public function Return an instance without the specified header.
MessageTrait::withProtocolVersion public function Return an instance with the specified HTTP protocol version.
RequestInterface::getRequestTarget public function Retrieves the message's request target. 2
RequestInterface::getUri public function Retrieves the URI instance. 2
RequestInterface::withRequestTarget public function Return an instance with the specific request-target. 2
RequestInterface::withUri public function Returns an instance with the provided URI. 2
ServerRequest::$attributes private property
ServerRequest::$cookieParams private property
ServerRequest::$parsedBody private property
ServerRequest::$queryParams private property
ServerRequest::$serverParams private property
ServerRequest::$uploadedFiles private property
ServerRequest::getAttribute public function Retrieve a single derived request attribute. Overrides ServerRequestInterface::getAttribute
ServerRequest::getAttributes public function Retrieve attributes derived from the request. Overrides ServerRequestInterface::getAttributes
ServerRequest::getCookieParams public function Retrieve cookies. Overrides ServerRequestInterface::getCookieParams
ServerRequest::getMethod public function Proxy to receive the request method. Overrides RequestInterface::getMethod
ServerRequest::getParsedBody public function Retrieve any parameters provided in the request body. Overrides ServerRequestInterface::getParsedBody
ServerRequest::getQueryParams public function Retrieve query string arguments. Overrides ServerRequestInterface::getQueryParams
ServerRequest::getServerParams public function Retrieve server parameters. Overrides ServerRequestInterface::getServerParams
ServerRequest::getStream private function Set the body stream
ServerRequest::getUploadedFiles public function Retrieve normalized file upload data. Overrides ServerRequestInterface::getUploadedFiles
ServerRequest::validateUploadedFiles private function Recursively validate the structure in an uploaded files array.
ServerRequest::withAttribute public function Return an instance with the specified derived request attribute. Overrides ServerRequestInterface::withAttribute
ServerRequest::withCookieParams public function Return an instance with the specified cookies. Overrides ServerRequestInterface::withCookieParams
ServerRequest::withMethod public function Set the request method. Overrides RequestInterface::withMethod
ServerRequest::withoutAttribute public function Return an instance that removes the specified derived request attribute. Overrides ServerRequestInterface::withoutAttribute
ServerRequest::withParsedBody public function Return an instance with the specified body parameters. Overrides ServerRequestInterface::withParsedBody
ServerRequest::withQueryParams public function Return an instance with the specified query string arguments. Overrides ServerRequestInterface::withQueryParams
ServerRequest::withUploadedFiles public function Create a new instance with the specified uploaded files. Overrides ServerRequestInterface::withUploadedFiles
ServerRequest::__construct public function