Skip to content
Permalink
Browse files

Add initial implementation of attributes.

Build out the Psr7\ServerRequestInterface methods for setting
attributes. These methods do not yet have shims for the deprecated
public properties.
  • Loading branch information...
markstory committed Aug 31, 2016
1 parent 6a509da commit 20e8d3cb3e09a9a20ede74527e7afc34e991c178
Showing with 118 additions and 0 deletions.
  1. +60 −0 src/Network/Request.php
  2. +58 −0 tests/TestCase/Network/RequestTest.php
@@ -157,6 +157,13 @@ class Request implements ArrayAccess
*/
protected $_session;
/**
* Store the additional attributes attached to the request.
*
* @var array
*/
protected $attributes = [];
/**
* Wrapper method to create a new request from PHP superglobals.
*
@@ -1420,6 +1427,59 @@ public function withParam($name, $value)
return $copy;
}
/**
* Return an instance with the specified request attribute.
*
* @param string $name The attribute name.
* @param mixed $value The value of the attribute.
* @return static
*/
public function withAttribute($name, $value)
{
$new = clone $this;
$new->attributes[$name] = $value;
return $new;
}
/**
* Return an instance without the specified request attribute.
*
* @param string $name The attribute name.
* @param mixed $value The value of the attribute.
* @return static
*/
public function withoutAttribute($name)
{
$new = clone $this;
unset($new->attributes[$name]);
return $new;
}
/**
* Read an attribute from the request, or get the default
*
* @param string $name The attribute name.
* @param mixed $default The default value if the attribute has not been set.
* @return static
*/
public function getAttribute($name, $default = null)
{
if (array_key_exists($name, $this->attributes)) {
return $this->attributes[$name];
}
return $default;
}
/**
* Get all the attributes in the request.
*
* @return array
*/
public function getAttributes()
{
return $this->attributes;
}
/**
* Array access read implementation
*
@@ -2638,6 +2638,64 @@ public function testWithDataFalseyValues()
$this->assertSame($expected, $result->data());
}
/**
* Test setting attributes.
*
* @return void
*/
public function testWithAttribute()
{
$request = new Request([]);
$this->assertNull($request->getAttribute('key'));
$this->assertSame('default', $request->getAttribute('key', 'default'));
$new = $request->withAttribute('key', 'value');
$this->assertNotEquals($new, $request, 'Should be different');
$this->assertNull($request->getAttribute('key'), 'Old instance not modified');
$this->assertSame('value', $new->getAttribute('key'));
$update = $new->withAttribute('key', ['complex']);
$this->assertNotEquals($update, $new, 'Should be different');
$this->assertSame(['complex'], $update->getAttribute('key'));
}
/**
* Test getting all attributes.
*
* @return void
*/
public function testGetAttributes()
{
$request = new Request([]);
$new = $request->withAttribute('key', 'value')
->withAttribute('nully', null)
->withAttribute('falsey', false);
$this->assertFalse($new->getAttribute('falsey'));
$this->assertNull($new->getAttribute('nully'));
$expected = [
'key' => 'value',
'nully' => null,
'falsey' => false
];
$this->assertEquals($expected, $new->getAttributes());
}
/**
* Test unsetting attributes.
*
* @return void
*/
public function testWithoutAttribute()
{
$request = new Request([]);
$new = $request->withAttribute('key', 'value');
$update = $request->withoutAttribute('key');
$this->assertNotEquals($update, $new, 'Should be different');
$this->assertNull($update->getAttribute('key'));
}
/**
* loadEnvironment method
*

0 comments on commit 20e8d3c

Please sign in to comment.
You can’t perform that action at this time.