/** * Get parameters * * This method returns an array of the parameters * passed. * * Is this similar to getRequest ? I have to check tmrw. * @todo ^ * @return Mixed An array or a string of parameters */ public function getParams() { $params = $this->request; /** * This certainly isn't a pure approach however it is a very * practical approach that will suit most people most of the times. * * Unhappy? Remove me. */ $input = file_get_contents("php://input"); parse_str($input, $puts); $inputFormat = $this->inputFormat; if (empty($inputFormat)) { $xmlJsonMatch = preg_grep('/\\<|\\{/i', array_keys($puts)); $inputFormat = $this->getFormat(); } if (empty($puts) && !empty($inputFormat) || !empty($xmlJsonMatch)) { /* attempt to parse the input */ $requestBody = Frapi_Input_RequestBodyParser::parse($inputFormat, $input); if (!empty($requestBody)) { $rootElement = array_keys($requestBody); // flatten the first element of the requestbody into the array // if it is itself an array and the only element // this handles a root element in the request body. if (count($requestBody) == 1 && is_array($requestBody[$rootElement[0]])) { $params[$rootElement[0]] = true; $requestBody = $requestBody[$rootElement[0]]; } $params = array_merge($params, $requestBody); } } else { if (!empty($puts)) { foreach ($puts as $put => $val) { $params[$put] = $val; } } } $this->request = $params; return $this->request; }
/** * parses the request and sets request parameters for later use * * @param array $params request params collected so far * @return Frapi_Controller_Main */ public function setParams(array $params) { // read the raw input to get the request body $input = file_get_contents("php://input"); parse_str($input, $puts); $inputFormat = $this->inputFormat; if (empty($inputFormat)) { $xmlJsonMatch = preg_grep('/\\<|\\{/i', array_keys($puts)); $inputFormat = $this->getFormat(); } /** * When doing parse_str("{json:string}") it creates an array like: * array( * "{json:string}" => "" * ) * * If args are also present along with the body, they are in the array * before the body. * * Checks if the last argument is an empty string, this + inputForm is * indicative of the body needing parsing. */ if (end($puts) == '' && !empty($inputFormat) || !empty($xmlJsonMatch)) { /* attempt to parse the input */ reset($puts); $requestBody = Frapi_Input_RequestBodyParser::parse($inputFormat, $input); if (!empty($requestBody)) { $rootElement = array_keys($requestBody); // flatten the first element of the requestbody into the array // if it is itself an array and the only element // this handles a root element in the request body. if (count($requestBody) == 1 && is_array($requestBody[$rootElement[0]])) { $params[$rootElement[0]] = true; $requestBody = $requestBody[$rootElement[0]]; } $params = array_merge($params, $requestBody); } } else { if (!empty($puts)) { foreach ($puts as $put => $val) { $params[$put] = $val; } } } // set the total request params; combines derived request params and parsed params $this->params = $params; return $this; }
/** * Get parameters * * This method returns an array of the parameters * passed. * * Is this similar to getRequest ? I have to check tmrw. * @todo ^ * @return Mixed An array or a string of parameters */ public function getParams() { $params = $this->request; /** * This certainly isn't a pure approach however it is a very * practical approach that will suit most people most of the times. * * Unhappy? Remove me. */ $input = file_get_contents("php://input"); parse_str($input, $puts); $inputFormat = $this->inputFormat; if (empty($inputFormat)) { $xmlJsonMatch = preg_grep('/\\<|\\{/i', array_keys($puts)); $inputFormat = $this->getFormat(); } /** * When doing parse_str("{json:string}") it creates an array like: * array( * "{json:string}" => "" * ) * * If args are also present along with the body, they are in the array * before the body. * * Checks if the last argument is an empty string, this + inputForm is * indicative of the body needing parsing. */ if (end($puts) == '' && !empty($inputFormat) || !empty($xmlJsonMatch)) { /* attempt to parse the input */ reset($puts); $requestBody = Frapi_Input_RequestBodyParser::parse($inputFormat, $input); if (!empty($requestBody)) { $rootElement = array_keys($requestBody); // flatten the first element of the requestbody into the array // if it is itself an array and the only element // this handles a root element in the request body. if (count($requestBody) == 1 && is_array($requestBody[$rootElement[0]])) { $params[$rootElement[0]] = true; $requestBody = $requestBody[$rootElement[0]]; } $params = array_merge($params, $requestBody); } } else { if (!empty($puts)) { foreach ($puts as $put => $val) { $params[$put] = $val; } } } $this->request = $params; return $this->request; }
function testEmpty() { $this->assertNull(Frapi_Input_RequestBodyParser::parse('json', '')); }