Skip to content

Commit

Permalink
Adding CakeRequest::data() to make reading/writing data similar to Ca…
Browse files Browse the repository at this point in the history
…keSession.

Tests added.
  • Loading branch information
markstory committed Sep 12, 2010
1 parent 730e373 commit 6519de3
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 7 deletions.
48 changes: 41 additions & 7 deletions cake/libs/cake_request.php
@@ -1,11 +1,6 @@
<?php
/**
* A class that helps wrap Request information and particulars about a single request.
* Provides methods commonly used to introspect on the request headers and request body.
*
* Has both an Array and Object interface. You can access framework parameters using indexes:
*
* `$request['controller']` or `$request->controller`.
* CakeRequest
*
* PHP 5
*
Expand All @@ -24,6 +19,15 @@
*/
App::import('Core', 'Set');

/**
* A class that helps wrap Request information and particulars about a single request.
* Provides methods commonly used to introspect on the request headers and request body.
*
* Has both an Array and Object interface. You can access framework parameters using indexes:
*
* `$request['controller']` or `$request->controller`.
*
*/
class CakeRequest implements ArrayAccess {
/**
* Array of parameters parsed from the url.
Expand Down Expand Up @@ -105,7 +109,7 @@ class CakeRequest implements ArrayAccess {
/**
* Constructor
*
* @param string $url Url string to use
* @param string $url Trimmed url string to use. Should not contain the application base path.
* @param boolean $parseEnvironment Set to false to not auto parse the environment. ie. GET, POST and FILES.
* @return void
*/
Expand Down Expand Up @@ -625,6 +629,36 @@ public function accepts($type = null) {
return in_array($type, $acceptTypes);
}

/**
* Provides a read/write accessor for `$this->data`. Allows you
* to use a syntax similar to `CakeSession` for reading post data.
*
* ## Reading values.
*
* `$request->data('Post.title');`
*
* When reading values you will get `null` for keys/values that do not exist.
*
* ## Writing values
*
* `$request->data('Post.title', 'New post!');`
*
* You can write to any value, even paths/keys that do not exist, and the arrays
* will be created for you.
*
* @param string $name Dot separated name of the value to read/write
* @param mixed $value Value to write to the data array.
* @return mixed Either the value being read, or this so you can chain consecutive writes.
*/
public function data($name) {
$args = func_get_args();
if (count($args) == 2) {
$this->data = Set::insert($this->data, $name, $args[1]);
return $this;
}
return Set::classicExtract($this->data, $name);
}

/**
* Array access read implementation
*
Expand Down
62 changes: 62 additions & 0 deletions cake/tests/cases/libs/cake_request.test.php
@@ -1,4 +1,25 @@
<?php
/**
* CakeRequest Test case file.
*
* PHP 5
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @package cake
* @subpackage cake.tests.cases.libs
* @since CakePHP(tm) v 2.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
if (!class_exists('dispatcher')) {
require CAKE . 'dispatcher.php';
}
App::import('Core', 'CakeRequest');

class CakeRequestTestCase extends CakeTestCase {
Expand Down Expand Up @@ -1241,6 +1262,47 @@ function testBasePathInjection() {
$this->assertEqual($request->base, $expected);
}

/**
* test the data() method reading
*
* @return void
*/
function testDataReading() {
$_POST['data'] = array(
'Model' => array(
'field' => 'value'
)
);
$request = new CakeRequest('posts/index');
$result = $request->data('Model');
$this->assertEquals($_POST['data']['Model'], $result);

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

/**
* test writing with data()
*
* @return void
*/
function testDataWriting() {
$_POST['data'] = array(
'Model' => array(
'field' => 'value'
)
);
$request = new CakeRequest('posts/index');
$result = $request->data('Model.new_value', 'new value');
$this->assertSame($result, $request, 'Return was not $this');

$this->assertEquals($request->data['Model']['new_value'], 'new value');

$request->data('Post.title', 'New post')->data('Comment.1.author', 'Mark');
$this->assertEquals($request->data['Post']['title'], 'New post');
$this->assertEquals($request->data['Comment']['1']['author'], 'Mark');
}

/**
* backupEnvironment method
*
Expand Down

0 comments on commit 6519de3

Please sign in to comment.