2020-11-08 15:21:51 +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 addRecords(array $records)
|
|
|
|
{
|
2020-11-09 14:22:34 +01:00
|
|
|
if ($records === null) return;
|
|
|
|
|
2020-11-08 15:21:51 +01:00
|
|
|
$columns = array(
|
|
|
|
"record_id" => "",
|
|
|
|
"start_time" => "",
|
|
|
|
"end_time" => "",
|
|
|
|
"duration" => "",
|
|
|
|
"user_id" => "",
|
|
|
|
"project_id" => "",
|
|
|
|
"start_device_id" => ""
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->jsonData['records'] = array();
|
|
|
|
foreach ($records as $record) {
|
|
|
|
$this->jsonData['records'][] = $this->createJsonArray($record, $columns);
|
|
|
|
}
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
function addProjects(array $projects)
|
|
|
|
{
|
2020-11-09 14:22:34 +01:00
|
|
|
if ($projects === null) return;
|
|
|
|
|
2020-11-08 15:21:51 +01:00
|
|
|
$columns = array(
|
|
|
|
"project_id" => "",
|
|
|
|
"name" => "",
|
|
|
|
"user_id" => "",
|
|
|
|
"start_date" => "",
|
|
|
|
"duration" => "",
|
|
|
|
"record_count" => ""
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->jsonData['projects'] = array();
|
|
|
|
foreach ($projects as $project) {
|
|
|
|
$this->jsonData['projects'][] = $this->createJsonArray($project, $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;
|
|
|
|
}
|
|
|
|
}
|