Created
August 2, 2012 13:38
-
-
Save stefankleff/3237158 to your computer and use it in GitHub Desktop.
Json aware POST/PUT processing
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
/** | |
* Process post data and call create | |
* | |
* @param Request $request | |
*/ | |
public function processPostData(Request $request) | |
{ | |
$contentType = $request->getHeaders('Content-Type')->getFieldValue(); | |
switch(true) { | |
case strstr($contentType, 'application/json'): | |
return $this->create(Json::decode($request->getContent(), Json::TYPE_ARRAY)); | |
break; | |
default: | |
return $this->create($request->getPost()->toArray()); | |
break; | |
} | |
} | |
/** | |
* Process put data and call update | |
* | |
* @param Request $request | |
* @param $routeMatch | |
*/ | |
public function processPutData(Request $request, $routeMatch) | |
{ | |
if (null === $id = $routeMatch->getParam('id')) { | |
if (!($id = $request->getQuery()->get('id', false))) { | |
throw new \DomainException('Missing identifier'); | |
} | |
} | |
$content = $request->getContent(); | |
$contentType = $request->getHeaders('Content-Type')->getFieldValue(); | |
switch(true) { | |
case strstr($contentType, 'application/json'): | |
$parsedParams = Json::decode($content, Json::TYPE_ARRAY); | |
break; | |
default: | |
parse_str($content, $parsedParams); | |
break; | |
} | |
return $this->update($id, $parsedParams); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment