arrel/json.php

35 lines
595 B
PHP
Raw Normal View History

<?php
class Json implements JsonSerializable
{
function __construct(
private mixed $struct
) {
$this->struct = $struct;
}
public static function new(mixed $struct): Json
{
return new Json($struct);
}
public static function error(mixed $msg): Json
{
return new Json([
'type' => 'error',
'msg' => $msg,
]);
}
public function jsonSerialize(): array
{
return $this->struct;
}
public function die()
{
print(json_encode($this->struct));
die();
}
}