Files
lucent-laravel/src/Record/Record.php
T

66 lines
1.3 KiB
PHP
Raw Normal View History

2023-10-02 23:10:49 +03:00
<?php
namespace Lucent\Record;
use JsonSerializable;
use stdClass;
class Record implements JsonSerializable
{
function __construct(
public string $id,
2023-10-04 13:32:30 +03:00
public string $schema,
public Status $status,
2023-10-02 23:10:49 +03:00
public System $_sys,
public RecordData $data,
public ?File $_file = null,
)
{
}
public function toDB(): array
{
return [
"id" => $this->id,
2023-10-04 13:32:30 +03:00
"status" => $this->status->value,
"schema" => $this->schema,
2023-10-02 23:10:49 +03:00
"_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,
2023-10-04 13:32:30 +03:00
schema: $data->schema,
status: Status::from($data->status),
2023-10-02 23:10:49 +03:00
_sys: System::fromArray(json_decode($data->_sys, true)),
data: new RecordData(json_decode($data->data, true)),
_file: $file,
);
}
public function jsonSerialize(): static
{
return $this;
}
}