. */ namespace Volkszaehler\View; use Volkszaehler\Interpreter; use Volkszaehler\View\HTTP; use Volkszaehler\Util; use Volkszaehler\Model; /** * JSON view * * @package default * @author Steffen Vogel */ class JSON extends View { /** * @var array holds the JSON data in an array */ protected $json; /** * @var string padding function name or NULL if disabled */ protected $padding = FALSE; /** * Constructor * * @param HTTP\Request $request * @param HTTP\Response $response */ public function __construct(HTTP\Request $request, HTTP\Response $response) { parent::__construct($request, $response); $this->json = new Util\JSON(); $this->json['version'] = VZ_VERSION; $this->response->setHeader('Content-type', 'application/json'); $this->setPadding($request->getParameter('padding')); } /** * Add object to output * * @param mixed $data */ public function add($data) { if ($data instanceof Interpreter\Interpreter || $data instanceof Interpreter\AggregatorInterpreter) { $this->addData($data); } elseif ($data instanceof Model\Entity) { $this->addEntity($data); } elseif ($data instanceof \Exception) { $this->addException($data); } elseif ($data instanceof Util\Debug) { $this->addDebug($data); } elseif ($data instanceof Util\JSON || is_array($data)) { $this->addArray($data); } elseif (isset($data)) { // ignores NULL data throw new \Exception('Can\'t show ' . get_class($data)); } } /** * Process, encode and print output to stdout */ protected function render() { $json = $this->json->encode((Util\Debug::isActivated()) ? JSON_PRETTY : 0); if ($this->padding) { $json = 'if (' . $this->padding . ') { ' . $this->padding . '(' . $json . '); }'; } echo $json; } /** * Add Entity to output queue * * @param Model\Entity $entity */ protected function addEntity(Model\Entity $entity) { if ($entity instanceof Model\Aggregator) { $this->json['entity'] = self::convertAggregator($entity); } else { $this->json['entity'] = self::convertEntity($entity); } } /** * Converts entity to array for json_encode() * * @param Model\Entity $entity * @return array */ protected static function convertEntity(Model\Entity $entity) { $jsonEntity = array(); $jsonEntity['uuid'] = (string) $entity->getUuid(); $jsonEntity['type'] = $entity->getType(); foreach ($entity->getProperties() as $key => $value) { $jsonEntity[$key] = $value; } return $jsonEntity; } /** * Converts aggregator to array for json_encode * * @param Model\Aggregator $aggregator * @return array */ protected static function convertAggregator(Model\Aggregator $aggregator) { $jsonAggregator = self::convertEntity($aggregator); foreach ($aggregator->getChildren() as $entity) { if ($entity instanceof Model\Channel) { $jsonAggregator['children'][] = self::convertEntity($entity); } elseif ($entity instanceof Model\Aggregator) { $jsonAggregator['children'][] = self::convertAggregator($entity); } } return $jsonAggregator; } /** * Add debugging information include queries and messages to output queue * * @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) ); } $this->json['debug'] = $jsonDebug; } /** * Add exception to output queue * * @param \Exception $exception * @param boolean $debug */ protected function addException(\Exception $exception) { $exceptionInfo = array( 'message' => $exception->getMessage(), 'type' => get_class($exception), 'code' => $exception->getCode() ); if (Util\Debug::isActivated()) { $debugInfo = array( 'file' => $exception->getFile(), 'line' => $exception->getLine(), 'backtrace' => $exception->getTrace() ); $this->json['exception'] = array_merge($exceptionInfo, $debugInfo); } else { $this->json['exception'] = $exceptionInfo; } } /** * Add data to output queue * * @param $interpreter */ protected function addData($interpreter) { $this->json['data']['uuid'] = $interpreter->getEntity()->getUuid(); $data = $interpreter->processData( $this->request->getParameter('tuples'), $this->request->getParameter('group'), function($tuple) { return array( $tuple[0], View::formatNumber($tuple[1]), $tuple[2] ); } ); $this->json['data']['min'] = $interpreter->getMin(); $this->json['data']['max'] = $interpreter->getMax(); $this->json['data']['average'] = View::formatNumber($interpreter->getAverage()); if ($interpreter instanceof Interpreter\MeterInterpreter) { $this->json['data']['consumption'] = View::formatNumber($interpreter->getConsumption()); } $this->json['data']['count'] = $interpreter->getTupleCount(); if (count($data) > 0) $this->json['data']['tuples'] = $data; } /** * Insert array in output * * @todo fix workaround for public entities */ protected function addArray($data) { foreach ($data as $index => $value) { if ($value instanceof Model\Entity) { $this->json['entities'][] = self::convertEntity($value); } else { $this->json[$index] = $value; } } } /* * Setter & getter */ public function setPadding($padding) { $this->padding = $padding; } } ?>