From 40fab8135a7c4f1fdda3dd21b71ba354ffca6690 Mon Sep 17 00:00:00 2001 From: mark_story Date: Sat, 30 Apr 2011 13:05:10 -0400 Subject: [PATCH] Adding ability to pass params to the decoding function. --- lib/Cake/Network/CakeRequest.php | 20 +++++++++++--- .../tests/Case/Network/CakeRequestTest.php | 27 +++++++++++++++++++ 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/lib/Cake/Network/CakeRequest.php b/lib/Cake/Network/CakeRequest.php index 7dea69cce..e03257fd9 100644 --- a/lib/Cake/Network/CakeRequest.php +++ b/lib/Cake/Network/CakeRequest.php @@ -676,15 +676,29 @@ class CakeRequest implements ArrayAccess { /** * Read data from `php://stdin`. Useful when interacting with XML or JSON * request body content. + * + * Getting input with a decoding function: + * + * `$this->request->input('json_decode');` + * + * Getting input using a decoding function, and additional params: + * + * `$this->request->input('Xml::build', array('return' => 'DOMDocument'));` + * + * Any additional parameters are applied to the callback in the order they are given. * * @param string $callback A decoding callback that will convert the string data to another - * representation. Leave empty to access the raw input data. + * representation. Leave empty to access the raw input data. You can also + * supply additional parameters for the decoding callback using var args, see above. * @return The decoded/processed request data. */ public function input($callback = null) { $input = $this->_readStdin(); - if ($callback) { - return call_user_func($callback, $input); + $args = func_get_args(); + if (!empty($args)) { + $callback = array_shift($args); + array_unshift($args, $input); + return call_user_func_array($callback, $args); } return $input; } diff --git a/lib/Cake/tests/Case/Network/CakeRequestTest.php b/lib/Cake/tests/Case/Network/CakeRequestTest.php index d0cdaf719..addb2594a 100644 --- a/lib/Cake/tests/Case/Network/CakeRequestTest.php +++ b/lib/Cake/tests/Case/Network/CakeRequestTest.php @@ -18,6 +18,7 @@ */ App::uses('Dispatcher', 'Routing'); +App::uses('Xml', 'Utility'); App::uses('CakeRequest', 'Network'); class CakeRequestTestCase extends CakeTestCase { @@ -1462,6 +1463,32 @@ class CakeRequestTestCase extends CakeTestCase { $result = $request->input('json_decode'); $this->assertEquals(array('name' => 'value'), (array)$result); } + +/** + * Test input() decoding with additional arguments. + * + * @return void + */ + function testInputDecodeExtraParams() { + $xml = << + + Test + +XML; + + $request = $this->getMock('CakeRequest', array('_readStdin')); + $request->expects($this->once())->method('_readStdin') + ->will($this->returnValue($xml)); + + $result = $request->input('Xml::build', array('return' => 'domdocument')); + $this->assertInstanceOf('DOMDocument', $result); + $this->assertEquals( + 'Test', + $result->getElementsByTagName('title')->item(0)->childNodes->item(0)->wholeText + ); + } + /** * loadEnvironment method *