public function testArrayKV() { $jsonData = '{ "responseData":{ "vhostList":[ {"id":"2","name":"gotcms.staging", "port":"80","status":"Ok","default":"0", "zendDefined":true,"zendManaged":true,"ssl":false, "created":"2015-07-15T12:43:28+00:00", "lastUpdated":"2015-07-23T11:53:18+00:00", "createdTimestamp":"1436964208", "lastUpdatedTimestamp":"1437652398", "servers":[{"id":"0", "status":"Ok", "name":"web-staging", "lastMessage":""}]}], "total": 1 }}'; $expectedOutput = "vhostList[0][id]=2\nvhostList[0][name]=gotcms.staging\nvhostList[0][port]=80\nvhostList[0][status]=Ok\nvhostList[0][default]=0\nvhostList[0][zendDefined]=1\nvhostList[0][zendManaged]=1\nvhostList[0][ssl]=\nvhostList[0][created]=2015-07-15T12:43:28+00:00\nvhostList[0][lastUpdated]=2015-07-23T11:53:18+00:00\nvhostList[0][createdTimestamp]=1436964208\nvhostList[0][lastUpdatedTimestamp]=1437652398\nvhostList[0][servers][0][id]=0\nvhostList[0][servers][0][status]=Ok\nvhostList[0][servers][0][name]=web-staging\nvhostList[0][servers][0][lastMessage]=\ntotal=1\n"; $items = json_decode($jsonData, true); $output = Utils::array2KV($items['responseData']); $this->assertEquals($output, $expectedOutput); }
/** * * @param MvcEvent $event */ public function preFinish(MvcEvent $event) { $response = $event->getResponse(); if ($response instanceof HttpResponse) { $response->setContent($response->getBody()); } $match = $event->getRouteMatch(); if ($match) { $outputFormat = $match->getParam('output-format'); if ($outputFormat != "kv") { return; } $output = ""; $content = $response->getContent(); $data = json_decode($content, true); if (isset($data['responseData'])) { $output = Utils::array2KV($data['responseData']); } $response->setContent($output); } }