2026-01-08 23:23:48 +02:00
|
|
|
<?php namespace Lucent\Core\Record;
|
|
|
|
|
|
|
|
|
|
use Carbon\Carbon;
|
|
|
|
|
use Lucent\Core\Data\Record;
|
|
|
|
|
use Lucent\Core\Data\RecordField;
|
|
|
|
|
use stdClass;
|
|
|
|
|
|
|
|
|
|
class RecordModule
|
|
|
|
|
{
|
2026-01-09 16:54:42 +02:00
|
|
|
public static function updateField(
|
|
|
|
|
Record $record,
|
|
|
|
|
RecordField $field,
|
|
|
|
|
): Record {
|
|
|
|
|
$recordFields = collect($record->draftData)->filter(
|
|
|
|
|
fn(RecordField $rf) => !(
|
|
|
|
|
$rf->id == $field->id && $rf->locale == $field->locale
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
$recordFields->push($field);
|
|
|
|
|
$record->draftData = $recordFields->values()->toArray();
|
|
|
|
|
return $record;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-08 23:23:48 +02:00
|
|
|
public static function toDb(Record $record): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
"id" => $record->id,
|
|
|
|
|
"schema_id" => $record->schemaId,
|
|
|
|
|
"created_at" => $record->createdAt->toJSON(),
|
|
|
|
|
"created_by" => $record->createdBy,
|
|
|
|
|
"published_at" => empty($record->publishedAt)
|
|
|
|
|
? null
|
|
|
|
|
: record->publishedAt->toJSON(),
|
|
|
|
|
"published_by" => $record->publishedBy,
|
|
|
|
|
"trashed_at" => empty($record->trashedAt)
|
|
|
|
|
? null
|
|
|
|
|
: record->trashedAt->toJSON(),
|
|
|
|
|
"trashed_by" => $record->trashedBy,
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function fromDb(stdClass $data): Record
|
|
|
|
|
{
|
|
|
|
|
return new Record(
|
|
|
|
|
id: data_get($data, "id"),
|
|
|
|
|
schemaId: data_get($data, "schema_id"),
|
|
|
|
|
createdAt: Carbon::parse(data_get($data, "created_at")),
|
|
|
|
|
createdBy: data_get($data, "created_by"),
|
|
|
|
|
publishedAt: empty(data_get($data, "published_at"))
|
|
|
|
|
? null
|
|
|
|
|
: Carbon::parse(data_get($data, "published_at")),
|
|
|
|
|
publishedBy: data_get($data, "published_by"),
|
|
|
|
|
trashedAt: empty(data_get($data, "trashed_at"))
|
|
|
|
|
? null
|
|
|
|
|
: Carbon::parse(data_get($data, "trashed_at")),
|
|
|
|
|
trashedBy: data_get($data, "trashed_by"),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|