85 lines
3.1 KiB
PHP
85 lines
3.1 KiB
PHP
|
|
<?php namespace Lucent\Core\Record;
|
||
|
|
|
||
|
|
use Carbon\Carbon;
|
||
|
|
use Lucent\Core\Data\Record;
|
||
|
|
use Lucent\Core\Data\RecordField;
|
||
|
|
use stdClass;
|
||
|
|
|
||
|
|
class RecordModule
|
||
|
|
{
|
||
|
|
public static function toDb(Record $record): array
|
||
|
|
{
|
||
|
|
return [
|
||
|
|
"id" => $record->id,
|
||
|
|
"schema_id" => $record->schemaId,
|
||
|
|
"draft_data" => json_encode(
|
||
|
|
array_map(static::recordFieldToDb(...), $record->draftData),
|
||
|
|
),
|
||
|
|
"live_data" => json_encode(
|
||
|
|
array_map(static::recordFieldToDb(...), $record->liveData),
|
||
|
|
),
|
||
|
|
"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
|
||
|
|
{
|
||
|
|
$draftDataArr = json_decode(data_get($data, "draft_data"), true);
|
||
|
|
$liveDataArr = json_decode(data_get($data, "live_data"), true);
|
||
|
|
|
||
|
|
return new Record(
|
||
|
|
id: data_get($data, "id"),
|
||
|
|
schemaId: data_get($data, "schema_id"),
|
||
|
|
draftData: array_map(static::recordFieldFromDb(...), $draftDataArr),
|
||
|
|
liveData: array_map(static::recordFieldFromDb(...), $liveDataArr),
|
||
|
|
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"),
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function recordFieldFromDb(array $field): RecordField
|
||
|
|
{
|
||
|
|
return new RecordField(
|
||
|
|
id: data_get($field, "id"),
|
||
|
|
locale: data_get($field, "locale", []),
|
||
|
|
value: data_get($field, "value"),
|
||
|
|
createdAt: Carbon::parse(data_get($field, "created_at")),
|
||
|
|
createdBy: data_get($field, "created_by"),
|
||
|
|
updatedAt: Carbon::parse(data_get($field, "updated_at")),
|
||
|
|
updatedBy: data_get($field, "updated_by"),
|
||
|
|
version: data_get($field, "version"),
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function recordFieldToDb(RecordField $field): array
|
||
|
|
{
|
||
|
|
return [
|
||
|
|
"id" => $field->id,
|
||
|
|
"locale" => $field->locale,
|
||
|
|
"value" => $field->value,
|
||
|
|
"created_at" => $field->createdAt->toJSON(),
|
||
|
|
"created_by" => $field->createdBy,
|
||
|
|
"updated_at" => $field->updatedAt->toJSON(),
|
||
|
|
"updated_by" => $field->updatedBy,
|
||
|
|
"version" => $field->version,
|
||
|
|
];
|
||
|
|
}
|
||
|
|
}
|