Skip to content

Commit

Permalink
Return ->data in Request::data(), restore Hash::get(), add test for d…
Browse files Browse the repository at this point in the history
…ata() call with null
  • Loading branch information
bcrowe committed May 26, 2014
1 parent 1eb08ad commit 47bebf4
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
5 changes: 4 additions & 1 deletion src/Network/Request.php
Expand Up @@ -966,7 +966,10 @@ public function data($name = null) {
$this->data = Hash::insert($this->data, $name, $args[1]);
return $this;
}
return Hash::get($this->data, $name);
if ($name !== null) {
return Hash::get($this->data, $name);
}
return $this->data;
}

/**
Expand Down
17 changes: 7 additions & 10 deletions src/Utility/Hash.php
Expand Up @@ -32,13 +32,13 @@ class Hash {
* but is faster for simple read operations.
*
* @param array $data Array of data to operate on.
* @param string|array|null $path The path being searched for. Either a dot
* @param string|array $path The path being searched for. Either a dot
* separated string, or an array of path segments, or null.
* @param mixed $default The return value when the path does not exist
* @return mixed The value fetched from the array, or null.
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash::get
*/
public static function get(array $data, $path = null, $default = null) {
public static function get(array $data, $path, $default = null) {
if (empty($data)) {
return $default;
}
Expand All @@ -54,14 +54,11 @@ public static function get(array $data, $path = null, $default = null) {
} else {
$parts = $path;
}

if ($path !== null) {
foreach ($parts as $key) {
if (is_array($data) && isset($data[$key])) {
$data =& $data[$key];
} else {
return $default;
}
foreach ($parts as $key) {
if (is_array($data) && isset($data[$key])) {
$data =& $data[$key];
} else {
return $default;
}
}
return $data;
Expand Down
3 changes: 3 additions & 0 deletions tests/TestCase/Network/RequestTest.php
Expand Up @@ -1991,6 +1991,9 @@ public function testDataReading() {
$result = $request->data('Model');
$this->assertEquals($post['Model'], $result);

$result = $request->data();
$this->assertEquals($post, $result);

$result = $request->data('Model.imaginary');
$this->assertNull($result);
}
Expand Down

0 comments on commit 47bebf4

Please sign in to comment.