Skip to content
Extraits de code Groupes Projets
Valider d9852b2a rédigé par mattpiwik's avatar mattpiwik
Parcourir les fichiers

Refs #1403

Added support for JSON/Serialized PHP export of Multi dimensional arrays in API responses, used by Alert plugin in the UI (json)

git-svn-id: http://dev.piwik.org/svn/trunk@2514 59fd770c-687e-43c8-a1e3-f5a4ff64c105
parent f3cac5d4
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
Affichage de avec 172 ajouts et 4 suppressions
......@@ -157,8 +157,10 @@ class Piwik_API_DocumentationGenerator
// Kindly force some known generic parameters to appear in the final list
// the parameter 'format' can be set to all API methods (used in tests)
// the parameter 'hideIdSubDatable' is used for integration tests only
// the parameter 'serialize' sets php outputs human readable, used in integration tests and debug
$aParameters['format'] = false;
$aParameters['hideIdSubDatable'] = false;
$aParameters['serialize'] = false;
$moduleName = Piwik_API_Proxy::getInstance()->getModuleNameFromClassName($class);
$urlExample = '?module=API&method='.$moduleName.'.'.$methodName.'&';
......
......@@ -283,12 +283,69 @@ class Piwik_API_ResponseBuilder
{
return serialize($array);
}
return $array;
}
else
$multiDimensional = $this->handleMultiDimensionalArray($array);
if($multiDimensional !== false)
{
$dataTable = new Piwik_DataTable();
$dataTable->addRowsFromSimpleArray($array);
return $this->getRenderedDataTable($dataTable);
return $multiDimensional;
}
$dataTable = new Piwik_DataTable();
$dataTable->addRowsFromSimpleArray($array);
return $this->getRenderedDataTable($dataTable);
}
/**
* Is this a multi dimensional array?
* Multi dim arrays are not supported by the Datatable renderer.
* We manually render these.
*
* array(
* array(
* 1,
* 2 => array( 1,
* 2
* )
* ),
* array( 2,
* 3
* )
* );
*
* @return String or false if it isn't a multidim array
*/
protected function handleMultiDimensionalArray($array)
{
$first = reset($array);
foreach($array as $first)
{
if(is_array($first))
{
foreach($first as $key => $value)
{
// Yes, this is a multi dim array
if(is_array($value))
{
switch($this->outputFormat)
{
case 'json':
@header( "Content-Type: application/json" );
return json_encode($array);
break;
case 'php':
if($this->caseRendererPHPSerialize( $defaultSerialize = 0))
{
return serialize($array);
}
return $array;
default:
break;
}
}
}
}
}
return false;
}
}
......@@ -153,6 +153,24 @@ class Piwik_ExampleAPI_API
{
return "Check http://en.wikipedia.org/wiki/The_Answer_to_Life,_the_Universe,_and_Everything";
}
/**
* Returns a Multidimensional Array
* Only supported in JSON
*
* @return array
*/
public function getMultiArray()
{
$return = array(
'Limitation' => array(
"Multi dimensional arrays is only supported by format=JSON",
"Known limitation"
),
'Second Dimension' => array( true, false, 1, 0, 152, 'test', array( 42 => 'end') ),
);
return $return;
}
}
/**
......
Error: Data structure returned is not convertible in the requested format. Try to call this method with the parameters '&format=original&serialize=1'; you will get the original php data structure serialized. The data structure looks like this:
$data = array (
'Limitation' =>
array (
0 => 'Multi dimensional arrays is only supported by format=JSON',
1 => 'Known limitation',
),
'Second Dimension' =>
array (
0 => true,
1 => false,
2 => 1,
3 => 0,
4 => 152,
5 => 'test',
6 =>
array (
42 => 'end',
),
),
);
\ No newline at end of file
Data structure returned is not convertible in the requested format. Try to call this method with the parameters '&amp;format=original&amp;serialize=1'; you will get the original php data structure serialized. The data structure looks like this: <br />
$data = array (<br />
'Limitation' =&gt; <br />
array (<br />
0 =&gt; 'Multi dimensional arrays is only supported by format=JSON',<br />
1 =&gt; 'Known limitation',<br />
),<br />
'Second Dimension' =&gt; <br />
array (<br />
0 =&gt; true,<br />
1 =&gt; false,<br />
2 =&gt; 1,<br />
3 =&gt; 0,<br />
4 =&gt; 152,<br />
5 =&gt; 'test',<br />
6 =&gt; <br />
array (<br />
42 =&gt; 'end',<br />
),<br />
),<br />
);
\ No newline at end of file
{"Limitation":["Multi dimensional arrays is only supported by format=JSON","Known limitation"],"Second Dimension":[true,false,1,0,152,"test",{"42":"end"}]}
\ No newline at end of file
a:2:{s:10:"Limitation";a:2:{i:0;s:57:"Multi dimensional arrays is only supported by format=JSON";i:1;s:16:"Known limitation";}s:16:"Second Dimension";a:7:{i:0;b:1;i:1;b:0;i:2;i:1;i:3;i:0;i:4;i:152;i:5;s:4:"test";i:6;a:1:{i:42;s:3:"end";}}}
\ No newline at end of file
Error: Data structure returned is not convertible in the requested format. Try to call this method with the parameters '&amp;format=original&amp;serialize=1'; you will get the original php data structure serialized. The data structure looks like this:
$data = array (
'Limitation' =&gt;
array (
0 =&gt; 'Multi dimensional arrays is only supported by format=JSON',
1 =&gt; 'Known limitation',
),
'Second Dimension' =&gt;
array (
0 =&gt; true,
1 =&gt; false,
2 =&gt; 1,
3 =&gt; 0,
4 =&gt; 152,
5 =&gt; 'test',
6 =&gt;
array (
42 =&gt; 'end',
),
),
);
\ No newline at end of file
<?xml version="1.0" encoding="utf-8" ?>
<result>
<error message=" Data structure returned is not convertible in the requested format. Try to call this method with the parameters '&amp;format=original&amp;serialize=1'; you will get the original php data structure serialized. The data structure looks like this:
$data = array (
'Limitation' =&gt;
array (
0 =&gt; 'Multi dimensional arrays is only supported by format=JSON',
1 =&gt; 'Known limitation',
),
'Second Dimension' =&gt;
array (
0 =&gt; true,
1 =&gt; false,
2 =&gt; 1,
3 =&gt; 0,
4 =&gt; 152,
5 =&gt; 'test',
6 =&gt;
array (
42 =&gt; 'end',
),
),
); " />
</result>
\ No newline at end of file
......@@ -124,6 +124,7 @@ abstract class Test_Integration extends Test_Database
'Live',
'SEO',
'ExampleAPI',
'Pdfexport',
);
/**
......@@ -259,6 +260,7 @@ abstract class Test_Integration extends Test_Database
{
$parametersToSet['format'] = $format;
$parametersToSet['hideIdSubDatable'] = 1;
$parametersToSet['serialize'] = 1;
$exampleUrl = $apiMetadata->getExampleUrl($class, $methodName, $parametersToSet);
if($exampleUrl === false)
{
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter