Skip to content
Permalink
Browse files

Implement getData()

Add a getter for reading data and fully deprecate data().
  • Loading branch information...
markstory committed Nov 11, 2016
1 parent cf2a2f9 commit e7c246950e11417852ddc4237b870d487011f7a6
Showing with 41 additions and 9 deletions.
  1. +32 −3 src/Http/ServerRequest.php
  2. +9 −6 tests/TestCase/Network/RequestTest.php
@@ -1429,15 +1429,13 @@ public function getQuery($name, $default = null)
* $request->data('Post.title', 'New post!');
* ```
*
* As of 3.4.0, the setter mode of this method is *deprecated*.
* Use `withData` instead.
*
* You can write to any value, even paths/keys that do not exist, and the arrays
* will be created for you.
*
* @param string|null $name Dot separated name of the value to read/write
* @param mixed ...$args The data to set (deprecated)
* @return mixed|$this Either the value being read, or this so you can chain consecutive writes.
* @deprecated 3.4.0 Use withData() and getData() or getParsedBody() instead.
*/
public function data($name = null, ...$args)
{
@@ -1453,6 +1451,37 @@ public function data($name = null, ...$args)
return $this->data;
}
/**
* Provides a safe accessor for request data. Allows
* you to use Hash::get() compatible paths.
*
* ### Reading values.
*
* ```
* // get all data
* $request->getData();
*
* // Read a specific field.
* $request->data('Post.title');
*
* // With a default value.
* $request->data('Post.not there', 'default value);
* ```
*
* When reading values you will get `null` for keys/values that do not exist.
*
* @param string $name|null Dot separated name of the value to read. Or null to read all data.
* @param mixed $default The default data.
* @return mixed The value being read.
*/
public function getData($name = null, $default = null)
{
if ($name === null) {
return $this->data;
}
return Hash::get($this->data, $name, $default);
}
/**
* Safely access the values in $this->params.
*
@@ -2527,14 +2527,17 @@ public function testDataReading()
]
];
$request = new Request(compact('post'));
$result = $request->data('Model');
$this->assertEquals($post['Model'], $result);
$this->assertEquals($post['Model'], $request->data('Model'));
$this->assertEquals($post['Model'], $request->getData('Model'));
$result = $request->data();
$this->assertEquals($post, $result);
$this->assertEquals($post, $request->data());
$this->assertEquals($post, $request->getData());
$result = $request->data('Model.imaginary');
$this->assertNull($result);
$this->assertNull($request->data('Model.imaginary'));
$this->assertNull($request->getData('Model.imaginary'));
$this->assertSame('value', $request->getData('Model.field', 'default'));
$this->assertSame('default', $request->getData('Model.imaginary', 'default'));
}
/**

0 comments on commit e7c2469

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