Skip to content

Instantly share code, notes, and snippets.

@davidwindell
Created July 17, 2012 17:30
Show Gist options
  • Save davidwindell/3130707 to your computer and use it in GitHub Desktop.
Save davidwindell/3130707 to your computer and use it in GitHub Desktop.
<?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