improved output processing of generic arrays

fixed bug for sowing public entities
This commit is contained in:
Steffen Vogel 2011-03-07 16:12:08 +01:00
parent 469a930caf
commit a0d3f0d29a
3 changed files with 53 additions and 39 deletions

View file

@ -26,6 +26,7 @@ namespace Volkszaehler\Controller;
use Volkszaehler\Definition;
use Volkszaehler\Util;
use Volkszaehler\Model;
use Doctrine\ORM;
/**
* Entity controller
@ -61,7 +62,7 @@ class EntityController extends Controller {
}
}
else { // get public entities
return $this->filter(array('public' => TRUE));
return array('entities' => $this->filter(array('public' => TRUE)));
}
}

View file

@ -80,7 +80,7 @@ class JSON extends View {
$this->addDebug($data);
}
elseif ($data instanceof Util\JSON || is_array($data)) {
$this->addArray($data);
$this->addArray($data, $this->json);
}
elseif (isset($data)) { // ignores NULL data
throw new \Exception('Can\'t show ' . get_class($data));
@ -159,21 +159,9 @@ class JSON extends View {
* @param Util\Debug $debug
*/
protected function addDebug(Util\Debug $debug) {
$queries = $debug->getQueries();
$messages = $debug->getMessages();
$jsonDebug['time'] = $debug->getExecutionTime();
if (count($messages) > 0) {
$jsonDebug['messages'] = $messages;
}
if (count($queries) > 0) {
$jsonDebug['database'] = array(
'driver' => Util\Configuration::read('db.driver'),
'queries' => array_values($queries)
);
}
$jsonDebug['messages'] = $debug->getMessages();
$jsonDebug['queries'] = array_values($debug->getQueries());
$this->json['debug'] = $jsonDebug;
}
@ -238,17 +226,18 @@ class JSON extends View {
}
/**
* Insert array in output
*
* @todo fix workaround for public entities
*/
protected function addArray($data) {
protected function addArray($data, &$refNode) {
foreach ($data as $index => $value) {
if ($value instanceof Model\Entity) {
$this->json['entities'][] = self::convertEntity($value);
if ($value instanceof Util\JSON || is_array($value)) {
$this->addArray($value, $refNode[$index]);
}
elseif ($value instanceof Model\Entity) {
$refNode[$index] = self::convertEntity($value);
}
else {
$this->json[$index] = $value;
$refNode[$index] = $value;
}
}
}

View file

@ -80,10 +80,12 @@ class XML extends View {
elseif ($data instanceof Util\Debug) {
$this->addDebug($data);
}
elseif (is_array($data)) {
$this->xmlRoot->appendChild($this->convertArray($data));
elseif (is_array($data) || $data instanceof Util\JSON) {
foreach($data as $key => $value) {
$this->xmlRoot->appendChild($this->convertArray($value, $key));
}
}
else {
elseif (isset($data)) { // ignores NULL data
throw new \Exception('Can\'t show ' . get_class($data));
}
}
@ -163,13 +165,14 @@ class XML extends View {
protected function addDebug(Util\Debug $debug) {
$xmlDebug = $this->xmlDoc->createElement('debug');
$xmlDebug->appendChild($this->xmlDoc->createElement('time', $debug->getExecutionTime()));
$xmlDebug->appendChild($this->convertArray($debug->getMessages(), 'messages', 'message'));
$xmlDatabase = $this->xmlDoc->createElement('database');
$xmlDatabase->setAttribute('driver', Util\Configuration::read('db.driver'));
$xmlDatabase->appendChild($this->convertArray($debug->getQueries(), 'queries', 'query'));
$xmlDebug->appendChild($xmlDatabase);
$xmlMessages = $this->xmlDoc->createElement('messages');
foreach ($debug->getMessages() as $message) {
$xmlMessages->appendChild($this->convertMessage($message));
}
$xmlDebug->appendChild($xmlMessages);
$xmlDebug->appendChild($this->convertArray($debug->getQueries(), 'queries', 'query'));
$this->xmlRoot->appendChild($xmlDebug);
}
@ -194,6 +197,27 @@ class XML extends View {
$this->xmlRoot->appendChild($xmlException);
}
/**
* Converts message to DOMElement
*
* @param array $message
* @return DOMElement
*/
protected function convertMessage($message) {
$xmlMessage = $this->xmlDoc->createElement('message');
$xmlMessage->appendChild($this->xmlDoc->createElement('message', $message['message']));
if (Util\Debug::isActivated()) {
$xmlMessage->appendChild($this->xmlDoc->createElement('file', $message['file']));
$xmlMessage->appendChild($this->xmlDoc->createElement('line', $message['line']));
$xmlMessage->appendChild($this->convertArray($message['args'], 'args', 'arg'));
$xmlMessage->appendChild($this->convertTrace($message['trace']));
}
return $xmlMessage;
}
/**
* Add data to output queue
@ -233,7 +257,7 @@ class XML extends View {
* @param array the input array
* @return DOMElement
*/
protected function convertArray(array $array, $identifierPlural = 'array', $identifierSingular = 'entry') {
protected function convertArray($array, $identifierPlural = 'array', $identifierSingular = 'entry') {
$xmlArray = $this->xmlDoc->createElement($identifierPlural);
foreach ($array as $key => $value) {
@ -241,7 +265,11 @@ class XML extends View {
$key = $identifierSingular;
}
if (is_array($value)) {
if (is_null($value)) {
$value = 'null';
}
if (is_array($value) || $value instanceof Util\JSON || $value instanceof \stdClass) {
$xmlArray->appendChild($this->convertArray($value, $key));
}
elseif (is_numeric($value)) {
@ -251,7 +279,7 @@ class XML extends View {
$xmlArray->appendChild($this->xmlDoc->createElement($key, $value));
}
else { // TODO required?
$xmlArray->appendChild($this->xmlDoc->createElement($key, 'object'));
$xmlArray->appendChild($this->xmlDoc->createElement($key, 'object:' . get_class($value)));
}
}
@ -275,11 +303,7 @@ class XML extends View {
foreach ($trace as $key => $value) {
switch ($key) {
case 'args':
$xmlArgs = $this->xmlDoc->createElement($key);
$xmlTrace->appendChild($xmlArgs);
foreach ($value as $arg) {
$xmlArgs->appendChild($this->xmlDoc->createElement('arg', (is_scalar($value)) ? $value : 'object'));
}
$xmlTrace->appendChild($this->convertArray($value, 'args', 'arg'));
break;
case 'type':