e949/api/_types.php

96 lines
2.1 KiB
PHP

<?php // Necessary functions, types and other stuff
// Includes
if ($IS_FRONTEND) {
require_once("api/_errorslist.php");
require_once("api/_json.php");
} else {
require_once("_errorslist.php");
require_once("_json.php");
}
final class ErrorT {
private int $Code;
private string $Name;
private string $Description;
// Ctor
public function __construct(int $code = -1, string $name = "", string $desc = "") {
if ($code === -1 && empty($name))
JSON_ReturnError(code: E_UNS_INTERNAL, desc: "cant construct ErrorT without at least error code or name");
else if ($code === -1)
$code = Errors_ResolveCodeByName($name);
else if (empty($name))
$name = Errors_ResolveNameByCode($code);
$this->Code = $code;
$this->Name = $name;
$this->Description = $desc;
}
// Getter for error code
public function GetCode (): int {
return $this->Code;
}
// Getter for error name
public function GetName (): string {
return $this->Name;
}
// Getter for error description
public function GetDescription (): string {
return $this->Description;
}
// Stringify error
public function Stringify (): string {
if (isset($this->Description))
return "error " . $this->Name . " (" . strval($this->Code) . "): " . $this->Description;
else
return "error " . $this->Name . " (" . strval($this->Code) . ")";
}
}
// Return type of API method
final class ReturnT {
private ErrorT $ErrorObj;
private $Data;
// Ctor
public function __construct($data = null, int $err_code = 0, string $err_name = "", string $err_desc = "") {
$this->ErrorObj = new ErrorT($err_code, $err_name, $err_desc);
$this->Data = $data;
}
// Setter/getter for data
public function SetData ($d) {
$this->Data = $d;
}
public function GetData () {
return $this->Data;
}
// Get string representation of error
public function GetError (): string {
return $this->ErrorObj->Stringify();
}
// Is there any error
public function IsError (): bool {
return $this->ErrorObj->GetCode() !== E_NOERROR;
}
// Throw JSON error
function ThrowJSONError () {
JSON_ReturnError(
$this->ErrorObj->GetCode(),
$this->ErrorObj->GetName(),
$this->ErrorObj->Stringify()
);
}
}
?>