Umbau der Statistik Ausgabe (each enfernt)
Optimierungen
This commit is contained in:
@@ -5,27 +5,27 @@ class API
|
||||
{
|
||||
// Startzeit
|
||||
private $startTime = 0;
|
||||
|
||||
|
||||
// Statuscode
|
||||
private $status = 200;
|
||||
|
||||
|
||||
// Daten
|
||||
private $dataArray = array();
|
||||
|
||||
|
||||
// Fehler
|
||||
private $error = array();
|
||||
|
||||
|
||||
/**
|
||||
* Konstrukter, welcher die Startzeit für die Lauftzeit festlegt.
|
||||
*
|
||||
* <code>$api = new API;</code>
|
||||
*/
|
||||
|
||||
|
||||
function __construct()
|
||||
{
|
||||
$this->startTime = microtime(true);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Fügt ein Element hinzu.
|
||||
*
|
||||
@@ -35,20 +35,20 @@ class API
|
||||
* @param string|array|bool $value Wert
|
||||
* @return bool
|
||||
*/
|
||||
|
||||
|
||||
public function addData($key, $value)
|
||||
{
|
||||
if (!empty($this->error))
|
||||
return false;
|
||||
|
||||
|
||||
$dummyStatus = $this->status;
|
||||
$this->status = 500;
|
||||
|
||||
|
||||
if (($key = trim($key)) == '')
|
||||
return false;
|
||||
|
||||
|
||||
$splits = explode('.', $key);
|
||||
|
||||
|
||||
switch (count($splits))
|
||||
{
|
||||
case 1:
|
||||
@@ -69,12 +69,12 @@ class API
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
$this->status = $dummyStatus;
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Setzt den Statuscode.
|
||||
*
|
||||
@@ -83,17 +83,17 @@ class API
|
||||
* @param int $status Statuscode
|
||||
* @return bool
|
||||
*/
|
||||
|
||||
|
||||
public function setStatus($status)
|
||||
{
|
||||
if (!is_int($status))
|
||||
return false;
|
||||
|
||||
|
||||
$this->status = $status;
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Setzt eine Fehlermeldung.
|
||||
*
|
||||
@@ -104,26 +104,26 @@ class API
|
||||
* @param int $status Statuscode
|
||||
* @return bool
|
||||
*/
|
||||
|
||||
|
||||
public function setError($type, $message, $status = 500)
|
||||
{
|
||||
if (($type = trim($type)) == '')
|
||||
return false;
|
||||
|
||||
|
||||
if (($message = trim($message)) == '')
|
||||
return false;
|
||||
|
||||
|
||||
if (!is_int($status))
|
||||
return false;
|
||||
|
||||
|
||||
header('HTTP/1.0 '.$status);
|
||||
|
||||
|
||||
$this->error = array('type' => $type, 'message' => $message);
|
||||
$this->status = $status;
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Erzeugt das fertige JSON und gibt dieses anschließend aus.
|
||||
*
|
||||
@@ -132,19 +132,18 @@ class API
|
||||
* @param bool $prettyPrint Eingerückte Ausgabe des JSON
|
||||
* @return bool Ausgabe erfolgt mit "echo".
|
||||
*/
|
||||
|
||||
|
||||
public function display($prettyPrint = false)
|
||||
{
|
||||
header('Content-Type: application/json');
|
||||
|
||||
|
||||
$executionTime = microtime(true) - $this->startTime;
|
||||
|
||||
|
||||
if (empty($this->error))
|
||||
echo json_encode(array('data' => $this->dataArray, 'executionTime' => $executionTime, 'status' => $this->status), ($prettyPrint != false) ? JSON_PRETTY_PRINT : 0);
|
||||
else
|
||||
echo json_encode(array('error' => $this->error, 'executionTime' => $executionTime, 'status' => $this->status), ($prettyPrint != false) ? JSON_PRETTY_PRINT : 0);
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
?>
|
||||
Reference in New Issue
Block a user