37 lines
936 B
PHP
37 lines
936 B
PHP
|
<?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;
|
||
|
}
|
||
|
}
|
||
|
?>
|