Created
July 17, 2012 17:30
-
-
Save davidwindell/3130707 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Process input data and return as array based on content type | |
* support types json/xml/urlencoded form data | |
* | |
* @param Request $request | |
* @return array | |
*/ | |
public function processInput(Request $request) | |
{ | |
$contentType = $request->getHeaders()->get('Content-Type')->getFieldValue(); | |
$rawBody = $request->getContent(); | |
if (!$rawBody) { | |
throw new \DomainException('Empty data provided'); | |
} | |
// process input data based upon content type headers | |
switch ($contentType) { | |
case 'application/json': | |
return Json::decode($rawBody, Json::TYPE_ARRAY); | |
break; | |
case 'application/xml': | |
$xml = new Xml(); | |
return $xml->fromString($rawBody); | |
break; | |
default: | |
if ($request->getMethod() == 'post') { | |
return $request->getPost()->toArray(); | |
} else { | |
$content = $request->getContent(); | |
parse_str($content, $parsedParams); | |
return $parsedParams; | |
} | |
break; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment