From a6ee9e352d6c9efea570c9deba4d648b08982803 Mon Sep 17 00:00:00 2001 From: Mark Story Date: Mon, 26 Apr 2010 23:46:47 -0400 Subject: [PATCH] Expanding doc blocks for methods. Adding ArrayAccess implementation and test cases. --- cake/libs/cake_request.php | 52 +++++++++++++++++++-- cake/tests/cases/libs/cake_request.test.php | 25 ++++++++++ 2 files changed, 74 insertions(+), 3 deletions(-) diff --git a/cake/libs/cake_request.php b/cake/libs/cake_request.php index a68e24cc4..2322a1d52 100644 --- a/cake/libs/cake_request.php +++ b/cake/libs/cake_request.php @@ -17,7 +17,7 @@ * @since CakePHP(tm) v 2.0 * @license MIT License (http://www.opensource.org/licenses/mit-license.php) */ -class CakeRequest { +class CakeRequest implements ArrayAccess { /** * Array of parameters parsed from the url. * @@ -216,7 +216,9 @@ class CakeRequest { /** * Missing method handler, handles wrapping older style isAjax() type methods * - * @return void + * @param string $name The method called + * @param array $params Array of parameters for the method call + * @return mixed */ public function __call($name, $params) { if (strpos($name, 'is') === 0) { @@ -228,7 +230,8 @@ class CakeRequest { /** * Magic get method allows access to parsed routing parameters directly on the object. * - * @return mixed Either the value of the parameter or null + * @param string $name The property being accessed. + * @return mixed Either the value of the parameter or null. */ public function __get($name) { if (isset($this->params[$name])) { @@ -269,4 +272,47 @@ class CakeRequest { return false; } +/** + * Array access read implementation + * + * @param string $name Name of the key being accessed. + * @return mixed + */ + public function offsetGet($name) { + if (isset($this->params[$name])) { + return $this->params[$name]; + } + return null; + } + +/** + * Array access write implementation + * + * @param string $name Name of the key being written + * @param mixed $value The value being written. + * @return void + */ + public function offsetSet($name, $value) { + $this->params[$name] = $value; + } + +/** + * Array access isset() implementation + * + * @param string $name thing to check. + * @return boolean + */ + public function offsetExists($name) { + return isset($this->params[$name]); + } + +/** + * Array access unset() implementation + * + * @param $name Name to unset. + * @return void + */ + public function offsetUnset($name) { + unset($this->params[$name]); + } } \ No newline at end of file diff --git a/cake/tests/cases/libs/cake_request.test.php b/cake/tests/cases/libs/cake_request.test.php index 5c3bcb119..b97fe9e53 100644 --- a/cake/tests/cases/libs/cake_request.test.php +++ b/cake/tests/cases/libs/cake_request.test.php @@ -495,4 +495,29 @@ class CakeRequestTestCase extends CakeTestCase { $this->assertEqual($request->plugin, 'blogs'); $this->assertIdentical($request->banana, null); } + +/** + * test the array access implementation + * + * @return void + */ + function testArrayAccess() { + $request = new CakeRequest(); + $request->params = array('controller' => 'posts', 'action' => 'view', 'plugin' => 'blogs'); + + $this->assertEqual($request['controller'], 'posts'); + + $request['slug'] = 'speedy-slug'; + $this->assertEqual($request->slug, 'speedy-slug'); + $this->assertEqual($request['slug'], 'speedy-slug'); + + $this->assertTrue(isset($request['action'])); + $this->assertFalse(isset($request['wrong-param'])); + + $this->assertTrue(isset($request['plugin'])); + unset($request['plugin']); + $this->assertFalse(isset($request['plugin'])); + $this->assertNull($request['plugin']); + $this->assertNull($request->plugin); + } } \ No newline at end of file