78 lines
1.6 KiB
PHP
78 lines
1.6 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace Lucent\Record;
|
||
|
|
|
||
|
|
use JsonSerializable;
|
||
|
|
use stdClass;
|
||
|
|
|
||
|
|
class Record implements JsonSerializable
|
||
|
|
{
|
||
|
|
|
||
|
|
|
||
|
|
function __construct(
|
||
|
|
public string $id,
|
||
|
|
public System $_sys,
|
||
|
|
public RecordData $data,
|
||
|
|
public ?File $_file = null,
|
||
|
|
)
|
||
|
|
{
|
||
|
|
}
|
||
|
|
|
||
|
|
public function toArray(): array
|
||
|
|
{
|
||
|
|
return \json_decode(\json_encode($this), true);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function toDB(): array
|
||
|
|
{
|
||
|
|
return [
|
||
|
|
"id" => $this->id,
|
||
|
|
"_sys" => json_encode($this->_sys),
|
||
|
|
"_file" => json_encode($this->_file),
|
||
|
|
"data" => json_encode($this->data),
|
||
|
|
];
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function fromDB(stdClass $data): Record
|
||
|
|
{
|
||
|
|
|
||
|
|
$file = json_decode($data->_file, true);
|
||
|
|
if (!empty($file)) {
|
||
|
|
|
||
|
|
$file = new File(...$file);
|
||
|
|
} else {
|
||
|
|
$file = null;
|
||
|
|
}
|
||
|
|
|
||
|
|
return new Record(
|
||
|
|
id: $data->id,
|
||
|
|
_sys: System::fromArray(json_decode($data->_sys, true)),
|
||
|
|
data: new RecordData(json_decode($data->data, true)),
|
||
|
|
_file: $file,
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
public function jsonSerialize(): static
|
||
|
|
{
|
||
|
|
return $this;
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function fromArray(array $data): Record
|
||
|
|
{
|
||
|
|
|
||
|
|
$file = null;
|
||
|
|
if (!empty($data["_file"])) {
|
||
|
|
$file = File::fromArray($data["_file"]);
|
||
|
|
}
|
||
|
|
|
||
|
|
return new Record(
|
||
|
|
id: $data["id"],
|
||
|
|
_sys: System::fromArray($data["_sys"]),
|
||
|
|
data: new RecordData($data["data"]),
|
||
|
|
_file: $file,
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|