78 lines
2.2 KiB
PHP
78 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace Lucent\Schema;
|
|
|
|
use Lucent\Data\Schema;
|
|
use Lucent\LucentException;
|
|
use Lucent\Primitive\Collection;
|
|
|
|
class SchemaService
|
|
{
|
|
public function __construct() {}
|
|
|
|
public function fromArray(array $schemaArr): Schema
|
|
{
|
|
$schemaArr["fields"] = [
|
|
[
|
|
"ui" => "text",
|
|
"name" => "name",
|
|
"label" => "Name",
|
|
"required" => true,
|
|
],
|
|
...$schemaArr["fields"],
|
|
];
|
|
|
|
return new Schema(
|
|
name: $schemaArr["name"],
|
|
label: $schemaArr["label"],
|
|
visible: $schemaArr["visible"] ?? [],
|
|
groups: $schemaArr["groups"] ?? [],
|
|
fields: Collection::make($schemaArr["fields"])->map([
|
|
$this,
|
|
"mapFields",
|
|
]),
|
|
sortBy: $schemaArr["sortBy"] ?? "-_sys.updatedAt",
|
|
cardImage: $schemaArr["cardImage"] ?? null,
|
|
revisions: $schemaArr["revisions"] ?? 0,
|
|
read: $schemaArr["read"] ?? [],
|
|
write: $schemaArr["write"] ?? [],
|
|
);
|
|
}
|
|
|
|
public function mapFields(array $field): FieldInterface
|
|
{
|
|
$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",
|
|
);
|
|
}
|
|
|
|
unset($field["ui"]);
|
|
return new $ui(...$field);
|
|
}
|
|
}
|