Skip to content

Commit

Permalink
Add getAttributes() and removeAttribute() to Request
Browse files Browse the repository at this point in the history
  • Loading branch information
trowski committed Sep 12, 2019
1 parent 60eaeb2 commit a97d546
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
8 changes: 8 additions & 0 deletions docs/classes/request.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ If `$cookie` value is not valid, [`\Error`](http://php.net/manual/en/class.error

Removes a cookie from the request.

## `getAttributes(): array`

Returns an array of all the attributes stored in the request's mutable local storage.

## `hasAttribute(string $name): bool`

Check whether an attribute with the given name exists in the request's mutable local storage.
Expand All @@ -148,6 +152,10 @@ Assign a variable to the request's mutable local storage.
{:.note}
> Name of the attribute should be namespaced with a vendor and package namespace, like classes.
## `removeAttribute(string $name): void`

Removes a variable from the request's mutable local storage.

## `getTrailers(): Trailers`

Allows access to the [`Trailers`](trailers.md) of a request.
Expand Down
26 changes: 26 additions & 0 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,14 @@ private function setHeadersFromCookies(): void
$this->setHeader("cookie", $values);
}

/**
* @return mixed[] An array of all request attributes in the request's mutable local storage, indexed by name.
*/
public function getAttributes(): array
{
return $this->attributes;
}

/**
* Check whether a variable with the given name exists in the request's mutable local storage.
*
Expand All @@ -378,6 +386,8 @@ public function hasAttribute(string $name): bool
* @param string $name Name of the attribute, should be namespaced with a vendor and package namespace like classes.
*
* @return mixed
*
* @throws MissingAttributeError If an attribute with the given name does not exist.
*/
public function getAttribute(string $name)
{
Expand Down Expand Up @@ -409,6 +419,22 @@ public function setAttribute(string $name, $value): void
$this->attributes[$name] = $value;
}

/**
* Remove an attribute from the request's mutable local storage.
*
* @param string $name Name of the attribute, should be namespaced with a vendor and package namespace like classes.
*
* @throws MissingAttributeError If an attribute with the given name does not exist.
*/
public function removeAttribute(string $name): void
{
if (!$this->hasAttribute($name)) {
throw new MissingAttributeError("The requested attribute '{$name}' does not exist");
}

unset($this->attributes[$name]);
}

/**
* @return Trailers|null
*/
Expand Down
7 changes: 7 additions & 0 deletions test/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,13 @@ public function testGetAttribute(): void
$request->setAttribute('foo', 'bar');
$this->assertSame('bar', $request->getAttribute('foo'));

$request->setAttribute('bar', 'baz');
$this->assertSame('baz', $request->getAttribute('bar'));

$this->assertSame(['foo' => 'bar', 'bar' => 'baz'], $request->getAttributes());

$request->removeAttribute('bar');

$this->expectException(MissingAttributeError::class);
$request->getAttribute('bar');
}
Expand Down

0 comments on commit a97d546

Please sign in to comment.