8000 Allow application/hal+json on incoming requests. by Wilt · Pull Request #50 · zfcampus/zf-content-negotiation · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
This repository was archived by the owner on Jan 21, 2020. It is now read-only.

Allow application/hal+json on incoming requests. #50

Merged
merged 1 commit into from
May 26, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/ContentTypeListener.php
< 8000 td id="diff-8bf1719716d7f97f52ee163b333500644cbd0fe6a1ef6397a1fcbaa6abb33ef3L45" data-line-number="45" class="blob-num blob-num-context js-linkable-line-number">
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Zend\Mvc\MvcEvent;
use ZF\ApiProblem\ApiProblem;
use ZF\ApiProblem\ApiProblemResponse;
use Zend\Http\Header;

class ContentTypeListener
{
Expand Down Expand Up @@ -45,6 +46,7 @@ class ContentTypeListener
*/
public function __invoke(MvcEvent $e)
{
/** @var Request $request */
$request = $e->getRequest();
if (!method_exists($request, 'getHeaders')) {
// Not an HTTP request; nothing to do
Expand All @@ -63,6 +65,7 @@ public function __invoke(MvcEvent $e)

// body parameters:
$bodyParams = [];
/** @var Header\ContentType $contentType */
$contentType = $request->getHeader('Content-type');
switch ($request->getMethod()) {
case $request::METHOD_POST:
Expand Down Expand Up @@ -170,6 +173,15 @@ public function decodeJson($json)
}

$data = json_decode($json, true);

// Decode 'application/hal+json' to 'application/json' by merging _embedded into the array
if(is_array($data) && isset($data['_embedded'])){
foreach($data['_embedded'] as $key => $value){
$data[$key] = $value;
}
unset($data['_embedded']);
}

if (null !== $data) {
return $data;
}
Expand Down
0