Files
lucent-laravel/src/Schema/SchemaService.php
T

85 lines
3.0 KiB
PHP
Raw Normal View History

2023-10-02 23:10:49 +03:00
<?php
namespace Lucent\Schema;
2024-08-27 12:24:51 +03:00
use Lucent\LucentException;
2023-10-02 23:10:49 +03:00
use Lucent\Primitive\Collection;
class SchemaService
{
public function __construct()
{
}
public function fromArray(array $schemaArr): Schema
{
return match ($schemaArr["type"]) {
"collection" => new CollectionSchema(
name: $schemaArr["name"],
label: $schemaArr["label"],
visible: $schemaArr["visible"] ?? [],
2023-10-04 13:32:30 +03:00
groups: $schemaArr["groups"] ?? [],
2023-10-02 23:10:49 +03:00
fields: (new Collection($schemaArr["fields"]))->map([$this, 'mapFields']),
2023-10-04 13:32:30 +03:00
isEntry: $schemaArr["isEntry"] ?? false,
color: $schemaArr["color"] ?? "",
2023-10-23 19:43:59 +03:00
sortBy: $schemaArr["sortBy"] ?? "-_sys.updatedAt",
2024-08-19 17:48:10 +03:00
cardTitle: $schemaArr["titleTemplate"] ?? $schemaArr["cardTitle"] ?? null,
cardImage: $schemaArr["cardImage"] ?? null,
2023-10-15 19:14:07 +03:00
revisions: $schemaArr["revisions"] ?? 0,
2023-10-17 22:57:25 +03:00
read: $schemaArr["read"] ?? [],
write: $schemaArr["write"] ?? [],
2023-10-02 23:10:49 +03:00
),
"files" => new FilesSchema(
name: $schemaArr["name"],
label: $schemaArr["label"],
fields: (new Collection($schemaArr["fields"]))->map([$this, 'mapFields']),
2023-10-04 13:32:30 +03:00
path: $schemaArr["path"] ?? $schemaArr["name"],
groups: $schemaArr["groups"] ?? [],
isEntry: $schemaArr["isEntry"] ?? false,
2023-10-23 19:43:59 +03:00
sortBy: $schemaArr["sortBy"] ?? "-_sys.updatedAt",
2023-10-04 13:32:30 +03:00
color: $schemaArr["color"] ?? "",
2024-08-19 17:48:10 +03:00
cardTitle: $schemaArr["titleTemplate"] ?? $schemaArr["cardTitle"] ?? null,
cardImage: $schemaArr["cardImage"] ?? null,
2023-10-15 19:14:07 +03:00
revisions: $schemaArr["revisions"] ?? 0,
2023-10-17 22:57:25 +03:00
read: $schemaArr["read"] ?? [],
write: $schemaArr["write"] ?? [],
2023-10-02 23:10:49 +03:00
)
};
}
public function mapFields(array $field): FieldInterface
{
2024-08-18 17:23:18 +03:00
2024-08-27 12:24:51 +03:00
$schemaFields = [
\Lucent\Schema\Ui\Checkbox::class,
\Lucent\Schema\Ui\Color::class,
\Lucent\Schema\Ui\Date::class,
\Lucent\Schema\Ui\Datetime::class,
\Lucent\Schema\Ui\File::class,
\Lucent\Schema\Ui\Json::class,
\Lucent\Schema\Ui\Markdown::class,
\Lucent\Schema\Ui\Number::class,
\Lucent\Schema\Ui\Reference::class,
\Lucent\Schema\Ui\Rich::class,
\Lucent\Schema\Ui\Slug::class,
\Lucent\Schema\Ui\Text::class,
\Lucent\Schema\Ui\Textarea::class,
];
$ui = collect($schemaFields)->filter(function ($className) use ($field) {
return str_ends_with(strtolower($className), "\\" . strtolower($field["ui"]));
})->first();
if (empty($ui)) {
throw new LucentException("Field UI " . $field["ui"] . " not found");
}
2023-10-14 18:06:36 +03:00
2023-10-02 23:10:49 +03:00
unset($field["ui"]);
2024-08-27 12:24:51 +03:00
return new $ui(...$field);
2023-10-02 23:10:49 +03:00
}
2024-08-27 12:24:51 +03:00
2023-10-02 23:10:49 +03:00
}