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

170 lines
5.3 KiB
PHP
Raw Normal View History

2023-10-02 23:10:49 +03:00
<?php
namespace Lucent\Schema;
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",
2023-10-04 13:32:30 +03:00
titleTemplate: $schemaArr["titleTemplate"] ?? "",
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"] ?? "",
titleTemplate: $schemaArr["titleTemplate"] ?? "",
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-14 18:06:36 +03:00
),
"block" => new BlockSchema(
name: $schemaArr["name"],
label: $schemaArr["label"],
fields: (new Collection($schemaArr["fields"]))->map([$this, 'mapBlockFields'])
2023-10-02 23:10:49 +03:00
)
};
}
public function mapFields(array $field): FieldInterface
{
$className = "\\Lucent\Schema\Ui\\" . ucfirst($field["ui"]);
2023-10-14 18:06:36 +03:00
unset($field["ui"]);
2024-08-18 17:23:18 +03:00
2023-10-14 18:06:36 +03:00
return new $className(...$field);
}
public function mapBlockFields(array $field): FieldInterface
{
$className = "\\Lucent\Schema\BlockUi\\" . ucfirst($field["ui"]);
2023-10-02 23:10:49 +03:00
unset($field["ui"]);
return new $className(...$field);
}
2023-10-17 22:57:25 +03:00
2023-10-02 23:10:49 +03:00
//
// /**
// * @param array<string> $visible
// * @throws LucentException
// */
// public function create(
// string $name,
// string $label,
// string $type,
// bool $isEntry,
// int $revisionRetentionDays,
// int $revisionRetentionNumber,
// int $trashedRetentionDays,
// array $fields,
// string $titleTemplate = "",
// array $visible = [],
// string $path = ""
// ): Schema
// {
// if (empty($name) || empty($label)) {
// throw new LucentException("Name and Label are required");
// }
//
// $newFields = [];
// if (!empty($fields)) {
// $newFields = array_map([Field::class, 'fromArray'], $fields);
// }
//
// $schema = new Schema(
// name: new SchemaName($name),
// label: $label,
// type: Type::from($type),
// visible: new Collection($visible),
// fields: new Collection($newFields),
// isEntry: $isEntry,
// color: "",
// titleTemplate: $titleTemplate,
// views: new Collection(),
// revisionRetentionDays: $revisionRetentionDays,
// revisionRetentionNumber: $revisionRetentionNumber,
// trashedRetentionDays: $trashedRetentionDays,
// path: $path,
// );
//
// $this->schemaRepo->insert($schema);
// return $schema;
//
// }
//
// /**
// * @param array<string> $visible
// * @throws LucentException
// */
// public function update(
// string $name,
// string $label,
// bool $isEntry,
// string $color,
// array $visible,
// string $titleTemplate,
// int $revisionRetentionDays,
// int $revisionRetentionNumber,
// int $trashedRetentionDays,
// string $path = ""
// ): Schema
// {
// if (empty($name) || empty($label)) {
// throw new LucentException("Name and Label are required");
// }
//
//
// $channel = ChannelRepo::current();
// $schema = $channel->schemas->firstWhere("name", $name);
// $schema->label = $label;
// $schema->isEntry = $isEntry;
// $schema->color = $color;
// $schema->visible = new Collection($visible);
// $schema->titleTemplate = $titleTemplate;
// $schema->revisionRetentionDays = $revisionRetentionDays;
// $schema->revisionRetentionNumber = $revisionRetentionNumber;
// $schema->trashedRetentionDays = $trashedRetentionDays;
// $schema->path = $path;
// $this->schemaRepo->update($schema);
// return $schema;
// }
//
// public function delete(string $name): void
// {
// $channel = ChannelRepo::current();
// $schema = $channel->schemas->firstWhere("name", $name);
// if ($schema) {
// $this->schemaRepo->delete($schema);
// }
//
// }
}