From 103bbbc3751b8162a28d140661cca050532764b7 Mon Sep 17 00:00:00 2001 From: mark_story Date: Sun, 3 Feb 2013 22:09:52 -0500 Subject: [PATCH] Add CakeRequest::param() This method gives a read accessor to the data in $request->params. It removes the need to use isset() and empty(). --- lib/Cake/Network/CakeRequest.php | 14 +++++++++++++ .../Test/Case/Network/CakeRequestTest.php | 20 +++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/lib/Cake/Network/CakeRequest.php b/lib/Cake/Network/CakeRequest.php index 59b2ee2a9..d84b369bf 100644 --- a/lib/Cake/Network/CakeRequest.php +++ b/lib/Cake/Network/CakeRequest.php @@ -805,6 +805,20 @@ class CakeRequest implements ArrayAccess { return Hash::get($this->data, $name); } +/** + * Safely access the values in $this->params. + * + * @param string $name The name of the parameter to get. + * @return mixed The value of the provided parameter. Will + * return false if the parameter doesn't exist or is falsey. + */ + public function param($name) { + if (!isset($this->params[$name])) { + return false; + } + return $this->params[$name]; + } + /** * Read data from `php://input`. Useful when interacting with XML or JSON * request body content. diff --git a/lib/Cake/Test/Case/Network/CakeRequestTest.php b/lib/Cake/Test/Case/Network/CakeRequestTest.php index b851f0bf8..2f7d97a74 100644 --- a/lib/Cake/Test/Case/Network/CakeRequestTest.php +++ b/lib/Cake/Test/Case/Network/CakeRequestTest.php @@ -1768,6 +1768,26 @@ class CakeRequestTest extends CakeTestCase { $this->assertNull($result); } +/** + * Test using param() + * + * @return void + */ + public function testReadingParams() { + $request = new CakeRequest(); + $request->addParams(array( + 'controller' => 'posts', + 'admin' => true, + 'truthy' => 1, + 'zero' => '0', + )); + $this->assertFalse($request->param('not_set')); + $this->assertTrue($request->param('admin')); + $this->assertEquals(1, $request->param('truthy')); + $this->assertEquals('posts', $request->param('controller')); + $this->assertEquals('0', $request->param('zero')); + } + /** * test the data() method reading *