juggl/juggl/services/jsonBuilder.inc.php

37 lines
936 B
PHP
Raw Normal View History

2020-01-10 22:33:46 +01:00
<?php
class JsonBuilder {
function __construct () {
$this->jsonData = array();
}
function getJson () {
return json_encode($this->jsonData, JSON_FORCE_OBJECT);
}
function getArray () {
return $this->jsonData;
}
function addCategories (array $categories) {
$columns = array("id" => "",
"name" => "");
foreach ($categories as $category) {
$this->jsonData['categories'] = array();
$this->jsonData['categories'][] = $this->createJsonArray($category, $columns);
}
return $this;
}
private function createJsonArray (array $data, array $columns) {
$jsonArray = array();
foreach ($columns as $key => $column) {
if ($column === "") {
$column = $key;
}
$jsonArray[$key] = $data[$column];
}
return $jsonArray;
}
}
?>