updated JSON view
This commit is contained in:
parent
15d543e223
commit
57302a09ce
1 changed files with 63 additions and 53 deletions
|
@ -23,7 +23,6 @@
|
|||
|
||||
namespace Volkszaehler\View;
|
||||
|
||||
use Doctrine\ORM\Query\AST\Functions;
|
||||
use Volkszaehler\Interpreter;
|
||||
use Volkszaehler\View\HTTP;
|
||||
use Volkszaehler\Util;
|
||||
|
@ -47,21 +46,24 @@ class JSON extends View {
|
|||
protected $padding = FALSE;
|
||||
|
||||
/**
|
||||
* constructor
|
||||
* 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['source'] = 'volkszaehler.org';
|
||||
$this->json['version'] = VZ_VERSION;
|
||||
$this->json['component'] = 'backend';
|
||||
|
||||
$this->setPadding($request->getParameter('padding'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Process, encode and print output
|
||||
*
|
||||
* @return string the output
|
||||
*/
|
||||
protected function render() {
|
||||
$json = $this->json->encode((Util\Debug::isActivated()) ? JSON_PRETTY : 0);
|
||||
|
@ -75,9 +77,9 @@ class JSON extends View {
|
|||
}
|
||||
|
||||
/**
|
||||
* Add channel to output queue
|
||||
* Add Entity to output queue
|
||||
*
|
||||
* @param Model\Channel $channel
|
||||
* @param Model\Entity $entity
|
||||
*/
|
||||
protected function addEntity(Model\Entity $entity) {
|
||||
if ($entity instanceof Model\Aggregator) {
|
||||
|
@ -88,6 +90,46 @@ class JSON extends View {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*
|
||||
|
@ -148,17 +190,22 @@ class JSON extends View {
|
|||
$data = $interpreter->getValues($this->request->getParameter('tuples'), $this->request->getParameter('group'));
|
||||
|
||||
$this->json['data'] = array(
|
||||
'uuid' => $interpreter->getUuid(),
|
||||
'count' => count($data),
|
||||
'first' => (isset($data[0][0])) ? $data[0][0] : NULL,
|
||||
'last' => (isset($data[count($data)-1][0])) ? $data[count($data)-1][0] : NULL,
|
||||
'min' => $interpreter->getMin(),
|
||||
'max' => $interpreter->getMax(),
|
||||
'average' => $interpreter->getAverage(),
|
||||
'tuples' => $data
|
||||
'uuid' => $interpreter->getUuid(),
|
||||
'count' => count($data),
|
||||
'first' => (isset($data[0][0])) ? $data[0][0] : NULL,
|
||||
'last' => (isset($data[count($data)-1][0])) ? $data[count($data)-1][0] : NULL,
|
||||
'min' => $interpreter->getMin(),
|
||||
'max' => $interpreter->getMax(),
|
||||
'average' => $interpreter->getAverage(),
|
||||
'tuples' => $data
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert array in output
|
||||
*
|
||||
* @todo required?
|
||||
*/
|
||||
protected function addArray($data) {
|
||||
foreach ($data as $index => $value) {
|
||||
$this->json[$index] = $value;
|
||||
|
@ -166,46 +213,8 @@ class JSON extends View {
|
|||
}
|
||||
|
||||
/**
|
||||
* Converts entity to array for json_encode()
|
||||
*
|
||||
* @param Model\Entity $entity
|
||||
* @return array
|
||||
* Overloaded to support arrays
|
||||
*/
|
||||
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
|
||||
* @param boolean $recursive
|
||||
* @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;
|
||||
}
|
||||
|
||||
public function add($data) {
|
||||
if ($data instanceof Util\JSON || is_array($data)) {
|
||||
$this->addArray($data);
|
||||
|
@ -218,6 +227,7 @@ class JSON extends View {
|
|||
/*
|
||||
* Setter & getter
|
||||
*/
|
||||
|
||||
public function setPadding($padding) { $this->padding = $padding; }
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue