Skip to content

Commit 85635e6

Browse files
author
Slavey Karadzhov
committed
Fixed issues with the convertion from array to flat KV presentation.
Fixes #74.
1 parent 99258f2 commit 85635e6

File tree

3 files changed

+78
-12
lines changed

3 files changed

+78
-12
lines changed

module/Client/Module.php

+1-11
Original file line numberDiff line numberDiff line change
@@ -224,17 +224,7 @@ public function preFinish(MvcEvent $event)
224224
$content = $response->getContent();
225225
$data = json_decode($content, true);
226226
if (isset($data['responseData'])) {
227-
$responseData = $data['responseData'];
228-
$rootKey = key($responseData);
229-
foreach ($responseData[$rootKey] as $k=>$v) {
230-
if (is_scalar($v)) {
231-
$output .= "$k=$v\n";
232-
} elseif (is_array($v)) {
233-
foreach ($v as $k1=>$v1) {
234-
$output .= $k."[".$k1."]=$v1\n";
235-
}
236-
}
237-
}
227+
$output = Utils::array2KV($data['responseData']);
238228
}
239229

240230
$response->setContent($output);

module/Client/src/Client/Utils.php

+35-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
namespace Client;
34

45
use Zend\Stdlib\ArrayUtils;
@@ -10,7 +11,7 @@ abstract class Utils
1011
* This one does not replace dots and spaces in key name with underscores.
1112
*
1213
* @param string $string
13-
* @param array $data
14+
* @param array $data
1415
*/
1516
public static function parseString($string, &$data, $delimiter = '&')
1617
{
@@ -32,4 +33,37 @@ public static function parseString($string, &$data, $delimiter = '&')
3233
}
3334
}
3435
}
36+
37+
/**
38+
* Represents an array as key value pairs.
39+
*
40+
* @param array $items
41+
* @param string $nested
42+
* @param string $prefix
43+
*
44+
* @return string
45+
*/
46+
public static function array2KV(array $items, $nested = false, $prefix = '')
47+
{
48+
$output = '';
49+
foreach ($items as $k => $v) {
50+
if ($nested) {
51+
$k = "[{$k}]";
52+
}
53+
$k = $prefix.$k;
54+
if (is_scalar($v)) {
55+
$output .= "$k=$v\n";
56+
} elseif (is_array($v)) {
57+
foreach ($v as $k1 => $v1) {
58+
if (is_scalar($v1)) {
59+
$output .= $k.'['.$k1."]=$v1\n";
60+
} elseif (is_array($v1)) {
61+
$output .= self::array2KV($v1, true, $k.'['.$k1.']');
62+
}
63+
}
64+
}
65+
}
66+
67+
return $output;
68+
}
3569
}

module/Client/tests/ClientTest/UtilsTest.php

+42
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,46 @@ public function testParseString()
2525
$this->assertEquals($data, $array);
2626
}
2727
}
28+
29+
public function testArrayKV()
30+
{
31+
$jsonData = '{
32+
"responseData":{
33+
"vhostList":[
34+
{"id":"2","name":"gotcms.staging",
35+
"port":"80","status":"Ok","default":"0",
36+
"zendDefined":true,"zendManaged":true,"ssl":false,
37+
"created":"2015-07-15T12:43:28+00:00",
38+
"lastUpdated":"2015-07-23T11:53:18+00:00",
39+
"createdTimestamp":"1436964208",
40+
"lastUpdatedTimestamp":"1437652398",
41+
"servers":[{"id":"0",
42+
"status":"Ok",
43+
"name":"web-staging",
44+
"lastMessage":""}]}],
45+
"total": 1
46+
}}';
47+
$expectedOutput = "vhostList[0][id]=2
48+
vhostList[0][name]=gotcms.staging
49+
vhostList[0][port]=80
50+
vhostList[0][status]=Ok
51+
vhostList[0][default]=0
52+
vhostList[0][zendDefined]=1
53+
vhostList[0][zendManaged]=1
54+
vhostList[0][ssl]=
55+
vhostList[0][created]=2015-07-15T12:43:28+00:00
56+
vhostList[0][lastUpdated]=2015-07-23T11:53:18+00:00
57+
vhostList[0][createdTimestamp]=1436964208
58+
vhostList[0][lastUpdatedTimestamp]=1437652398
59+
vhostList[0][servers][0][id]=0
60+
vhostList[0][servers][0][status]=Ok
61+
vhostList[0][servers][0][name]=web-staging
62+
vhostList[0][servers][0][lastMessage]=
63+
total=1
64+
";
65+
$items = json_decode($jsonData, true);
66+
$output = Utils::array2KV($items['responseData']);
67+
68+
$this->assertEquals($output, $expectedOutput);
69+
}
2870
}

0 commit comments

Comments
 (0)