mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
Adding ability to pass params to the decoding function.
This commit is contained in:
parent
975f74bb44
commit
40fab8135a
2 changed files with 44 additions and 3 deletions
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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 = <<<XML
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<post>
|
||||
<title id="title">Test</title>
|
||||
</post>
|
||||
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
|
||||
*
|
||||
|
|
Loading…
Reference in a new issue