2023-10-02 23:10:49 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Lucent\Record;
|
|
|
|
|
|
|
|
|
|
use JsonSerializable;
|
|
|
|
|
use stdClass;
|
2023-10-22 16:09:36 +03:00
|
|
|
use Illuminate\Support\Str;
|
2023-10-02 23:10:49 +03:00
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2023-10-23 18:05:06 +03:00
|
|
|
private function indexValues(array $arrObject){
|
|
|
|
|
|
|
|
|
|
return trim(Str::lower(collect($arrObject)
|
|
|
|
|
->map(function($value){
|
2023-10-25 14:19:44 +03:00
|
|
|
if(is_array($value)){
|
2023-10-23 18:05:06 +03:00
|
|
|
return $this->indexValues($value ?? []);
|
|
|
|
|
}
|
2023-10-25 14:19:44 +03:00
|
|
|
return (string)$value;
|
2023-10-23 18:05:06 +03:00
|
|
|
})
|
|
|
|
|
->values()->join(" ")." ". $this->_file?->originalName ?? ""));
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-02 23:10:49 +03:00
|
|
|
public function toDB(): array
|
|
|
|
|
{
|
2023-10-23 18:05:06 +03:00
|
|
|
$searchIndex = $this->indexValues($this->data->toArray());
|
2023-10-02 23:10:49 +03:00
|
|
|
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),
|
2023-10-22 16:09:36 +03:00
|
|
|
"search" => $searchIndex,
|
2023-10-02 23:10:49 +03:00
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|