From a119f49bcb371891d567367a716cc8473ba678ba Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Sat, 4 Sep 2010 01:28:29 +0200 Subject: [PATCH] improved class structure --- backend/lib/View/JSON.php | 55 ++++++++++++++++++++++++--------------- 1 file changed, 34 insertions(+), 21 deletions(-) diff --git a/backend/lib/View/JSON.php b/backend/lib/View/JSON.php index 2b77560..47fc786 100644 --- a/backend/lib/View/JSON.php +++ b/backend/lib/View/JSON.php @@ -54,15 +54,8 @@ class JSON extends View { $this->padding = $this->request->getParameter('padding'); } - public function setPadding($padding) { $this->padding = $padding; } - public function addChannel(Model\Channel $channel, array $data = NULL) { - $jsonChannel['uuid'] = (string) $channel->getUuid(); - - - foreach ($channel->getProperties() as $property) { - $jsonChannel[$property->getName()] = $property->getValue(); - } + $jsonChannel = self::convertEntity($channel); if (isset($data)) { $jsonChannel['data'] = self::convertData($data); @@ -72,7 +65,7 @@ class JSON extends View { } public function addAggregator(Model\Aggregator $aggregator, $recursive = FALSE) { - $this->json['groups'][] = self::convertJson($aggregator, $recursive); + $this->json['groups'][] = self::convertAggregator($aggregator, $recursive); } public function addDebug(Util\Debug $debug) { @@ -86,24 +79,39 @@ class JSON extends View { ); } - protected function addException(\Exception $exception) { - $this->json['exception'] = array( + protected function addException(\Exception $exception, $debug = FALSE) { + $exceptionInfo = array( 'type' => get_class($exception), 'message' => $exception->getMessage(), - 'code' => $exception->getCode(), - 'file' => $exception->getFile(), - 'line' => $exception->getLine(), - 'trace' => $exception->getTrace() + 'code' => $exception->getCode() ); + + if ($debug) { + $debugInfo = array('file' => $exception->getFile(), + 'line' => $exception->getLine(), + 'trace' => $exception->getTrace() + ); + + $this->json['exception'] = array_merge($exceptionInfo, $debugInfo); + } + else { + $this->json['exception'] = $exceptionInfo; + } + } + + protected static function convertEntity(Model\Entity $entity) { + $jsonEntity = array(); + $jsonEntity['uuid'] = (string) $entity->getUuid(); + + foreach ($entity->getProperties() as $property) { + $jsonEntity[$property->getName()] = $property->getValue(); + } + + return $jsonEntity; } protected static function convertAggregator(Model\Aggregator $aggregator, $recursive = FALSE) { - $jsonAggregator = array(); - - $jsonAggregator['uuid'] = (string) $aggregator->getUuid(); - $jsonAggregator['name'] = $aggregator->getName(); - $jsonAggregator['description'] = $aggregator->getDescription(); - $jsonAggregator['channels'] = array(); + $jsonAggregator = self::convertEntity($aggregator); foreach ($aggregator->getChannels() as $channel) { $jsonAggregator['channels'][] = (string) $channel->getUuid(); @@ -143,6 +151,11 @@ class JSON extends View { echo $json; } + + /* + * Setter & getter + */ + public function setPadding($padding) { $this->padding = $padding; } } ?> \ No newline at end of file