Adding CakeRequest::data() to make reading/writing data similar to CakeSession.

Tests added.
This commit is contained in:
mark_story 2010-09-12 14:05:57 -04:00
parent 730e373afe
commit 6519de3a2e
2 changed files with 103 additions and 7 deletions

View file

@ -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
*
@ -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.
@ -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
*/
@ -625,6 +629,36 @@ class CakeRequest implements ArrayAccess {
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
*

View file

@ -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 {
@ -1241,6 +1262,47 @@ class CakeRequestTestCase extends CakeTestCase {
$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
*