init
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
<?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,
|
||||
public System $_sys,
|
||||
public RecordData $data,
|
||||
public ?File $_file = null,
|
||||
)
|
||||
{
|
||||
}
|
||||
|
||||
public function toArray(): array
|
||||
{
|
||||
return json_decode(json_encode($this), true);
|
||||
}
|
||||
|
||||
|
||||
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,
|
||||
_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,
|
||||
"_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,
|
||||
_sys: $record->_sys,
|
||||
data: $record->data,
|
||||
_file: $record->_file,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user