Skip to content
Permalink
Browse files

Implement setter for param() that supports immutable objects.

Add `withParam()` that implements the immutable object pattern that PSR7
uses. This deprecates the setter mode of param() as the request becomes
start to implement more of the immutable patterns.
  • Loading branch information...
markstory committed Aug 26, 2016
1 parent ce6cf6a commit 2150a249fb8caafd882667ea1af00c643f81b702
Showing with 46 additions and 0 deletions.
  1. +21 −0 src/Network/Request.php
  2. +25 −0 tests/TestCase/Network/RequestTest.php
@@ -1218,6 +1218,9 @@ public function data($name = null)
/**
* Safely access the values in $this->params.
*
* As of 3.4.0, the setter mode of this method is *deprecated*.
* Use `withParam` instead.
*
* @param string $name The name of the parameter to get.
* @return mixed|$this The value of the provided parameter. Will
* return false if the parameter doesn't exist or is falsey.
@@ -1395,6 +1398,24 @@ public function withData($name, $value)
return $copy;
}
/**
* Update the request with a new routing parameter
*
* Returns an updated request object. This method returns
* a *new* request object and does not mutate the request in-place.
*
* @param string $name The dot separated path to insert $value at.
* @param mixed $value The value to insert into the the request parameters.
* @return self
*/
public function withParam($name, $value)
{
$copy = clone $this;
$copy->params = Hash::insert($copy->params, $name, $value);
return $copy;
}
/**
* Array access read implementation
*
@@ -2537,6 +2537,31 @@ public function testMethodOverrideEmptyData()
$this->assertEmpty($request->data);
}
/**
* Test updating params in a psr7 fashion.
*
* @return void
*/
public function testWithParam()
{
$request = new Request([
'params' => ['controller' => 'Articles']
]);
$result = $request->withParam('action', 'view');
$this->assertNotSame($result, $request, 'New instance should be made');
$this->assertFalse($request->param('action'), 'No side-effect on original');
$this->assertSame('view', $result->param('action'));
$result = $request->withParam('action', 'index')
->withParam('plugin', 'DebugKit')
->withParam('prefix', 'Admin');
$this->assertNotSame($result, $request, 'New instance should be made');
$this->assertFalse($request->param('action'), 'No side-effect on original');
$this->assertSame('index', $result->param('action'));
$this->assertSame('DebugKit', $result->param('plugin'));
$this->assertSame('Admin', $result->param('prefix'));
}
/**
* Test updating POST data in a psr7 fashion.
*

0 comments on commit 2150a24

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