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

74 lines
1.6 KiB
PHP
Raw Normal View History

2023-10-02 23:10:49 +03:00
<?php
namespace Lucent\Revision;
use Illuminate\Support\Str;
use Lucent\Record\File;
use Lucent\Record\Record;
use Lucent\Record\RecordData;
use Lucent\Record\System;
use stdClass;
readonly class Revision
{
function __construct(
public string $id,
public string $recordId,
2023-10-04 13:32:30 +03:00
public string $schema,
2023-10-02 23:10:49 +03:00
public System $_sys,
public RecordData $data,
public ?File $_file = null,
)
{
}
public static function fromDB(stdClass $data): Revision
{
$file = json_decode($data->_file, true);
if (!empty($file)) {
$file = new File(...$file);
} else {
$file = null;
}
return new Revision(
id: $data->id,
recordId: $data->recordId,
2023-10-04 13:32:30 +03:00
schema: $data->schema,
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 toDB(): array
{
return [
"id" => $this->id,
"recordId" => $this->recordId,
2023-10-04 13:32:30 +03:00
"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 fromRecord(Record $record): Revision
{
return new Revision(
id: (string)Str::uuid(),
recordId: $record->id,
2023-10-04 13:32:30 +03:00
schema: $record->schema,
2023-10-02 23:10:49 +03:00
_sys: $record->_sys,
data: $record->data,
_file: $record->_file,
);
}
}