From 2a49fc9e43dc465634b6d281a8e15ec46d5b00a6 Mon Sep 17 00:00:00 2001 From: Benjamin Morel Date: Wed, 11 Nov 2020 14:45:53 +0100 Subject: [PATCH] Support for request attributes --- src/Request.php | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/src/Request.php b/src/Request.php index fd21aea..6ec5aaf 100644 --- a/src/Request.php +++ b/src/Request.php @@ -110,6 +110,11 @@ final class Request extends Message */ private string $clientIp = '0.0.0.0'; + /** + * @var array + */ + private array $attributes = []; + /** * Returns a Request object representing the current request. * @@ -1179,6 +1184,42 @@ public function getAcceptLanguage() : array return $this->parseQualityValues($this->getHeader('Accept-Language')); } + /** + * @return array + */ + public function getAttributes(): array + { + return $this->attributes; + } + + public function getAttribute(string $name, $default = null) + { + if (array_key_exists($name, $this->attributes)) { + return $this->attributes[$name]; + } + + return $default; + } + + /** + * Returns a copy of this request with the given attribute set. + * + * This instance is immutable and unaffected by this method call. + * + * @param string $name The attribute name. + * @param mixed $value The attribute value. + * + * @return Request The updated request. + */ + public function withAttribute(string $name, $value): Request + { + $that = clone $this; + + $that->attributes[$name] = $value; + + return $that; + } + /** * Parses quality values as defined per RFC 7231 ยง 5.3.1. *