Skip to content

Commit

Permalink
Support for request attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
BenMorel committed Nov 11, 2020
1 parent 21f9104 commit 2a49fc9
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ final class Request extends Message
*/
private string $clientIp = '0.0.0.0';

/**
* @var array<string, mixed>
*/
private array $attributes = [];

/**
* Returns a Request object representing the current request.
*
Expand Down Expand Up @@ -1179,6 +1184,42 @@ public function getAcceptLanguage() : array
return $this->parseQualityValues($this->getHeader('Accept-Language'));
}

/**
* @return array<string, mixed>
*/
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.
*
Expand Down

0 comments on commit 2a49fc9

Please sign in to comment.