class ServerRequest in Zircon Profile 8
Same name in this branch
- 8 vendor/zendframework/zend-diactoros/src/ServerRequest.php \Zend\Diactoros\ServerRequest
- 8 vendor/symfony/psr-http-message-bridge/Tests/Fixtures/ServerRequest.php \Symfony\Bridge\PsrHttpMessage\Tests\Fixtures\ServerRequest
Same name and namespace in other branches
- 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
- class \Zend\Diactoros\ServerRequest implements ServerRequestInterface uses MessageTrait
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\DiactorosView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
MessageTrait:: |
protected | property | Map of normalized header name to original name used to register header. | |
MessageTrait:: |
protected | property | List of all registered headers, as key => array of values. | |
MessageTrait:: |
private | property | ||
MessageTrait:: |
private | property | ||
MessageTrait:: |
private | function | Test that an array contains only strings | |
MessageTrait:: |
private static | function | Assert that the provided header values are valid. | |
MessageTrait:: |
private | function | Filter a set of headers to ensure they are in the correct internal format. | |
MessageTrait:: |
private static | function | Test if a value is a string | |
MessageTrait:: |
public | function | Gets the body of the message. | |
MessageTrait:: |
public | function | Retrieves a message header value by the given case-insensitive name. | 1 |
MessageTrait:: |
public | function | Retrieves a comma-separated string of the values for a single header. | |
MessageTrait:: |
public | function | Retrieves all message headers. | 1 |
MessageTrait:: |
public | function | Retrieves the HTTP protocol version as a string. | |
MessageTrait:: |
public | function | Checks if a header exists by the given case-insensitive name. | |
MessageTrait:: |
public | function | Return an instance with the specified header appended with the given value. | |
MessageTrait:: |
public | function | Return an instance with the specified message body. | |
MessageTrait:: |
public | function | Return an instance with the provided header, replacing any existing values of any headers with the same case-insensitive name. | |
MessageTrait:: |
public | function | Return an instance without the specified header. | |
MessageTrait:: |
public | function | Return an instance with the specified HTTP protocol version. | |
RequestInterface:: |
public | function | Retrieves the message's request target. | 2 |
RequestInterface:: |
public | function | Retrieves the URI instance. | 2 |
RequestInterface:: |
public | function | Return an instance with the specific request-target. | 2 |
RequestInterface:: |
public | function | Returns an instance with the provided URI. | 2 |
ServerRequest:: |
private | property | ||
ServerRequest:: |
private | property | ||
ServerRequest:: |
private | property | ||
ServerRequest:: |
private | property | ||
ServerRequest:: |
private | property | ||
ServerRequest:: |
private | property | ||
ServerRequest:: |
public | function |
Retrieve a single derived request attribute. Overrides ServerRequestInterface:: |
|
ServerRequest:: |
public | function |
Retrieve attributes derived from the request. Overrides ServerRequestInterface:: |
|
ServerRequest:: |
public | function |
Retrieve cookies. Overrides ServerRequestInterface:: |
|
ServerRequest:: |
public | function |
Proxy to receive the request method. Overrides RequestInterface:: |
|
ServerRequest:: |
public | function |
Retrieve any parameters provided in the request body. Overrides ServerRequestInterface:: |
|
ServerRequest:: |
public | function |
Retrieve query string arguments. Overrides ServerRequestInterface:: |
|
ServerRequest:: |
public | function |
Retrieve server parameters. Overrides ServerRequestInterface:: |
|
ServerRequest:: |
private | function | Set the body stream | |
ServerRequest:: |
public | function |
Retrieve normalized file upload data. Overrides ServerRequestInterface:: |
|
ServerRequest:: |
private | function | Recursively validate the structure in an uploaded files array. | |
ServerRequest:: |
public | function |
Return an instance with the specified derived request attribute. Overrides ServerRequestInterface:: |
|
ServerRequest:: |
public | function |
Return an instance with the specified cookies. Overrides ServerRequestInterface:: |
|
ServerRequest:: |
public | function |
Set the request method. Overrides RequestInterface:: |
|
ServerRequest:: |
public | function |
Return an instance that removes the specified derived request attribute. Overrides ServerRequestInterface:: |
|
ServerRequest:: |
public | function |
Return an instance with the specified body parameters. Overrides ServerRequestInterface:: |
|
ServerRequest:: |
public | function |
Return an instance with the specified query string arguments. Overrides ServerRequestInterface:: |
|
ServerRequest:: |
public | function |
Create a new instance with the specified uploaded files. Overrides ServerRequestInterface:: |
|
ServerRequest:: |
public | function |