This commit is contained in:
2023-10-02 23:10:49 +03:00
commit c6cb488379
255 changed files with 18731 additions and 0 deletions
+27
View File
@@ -0,0 +1,27 @@
<?php
namespace Lucent\Schema;
use Lucent\Primitive\Collection;
class CollectionSchema implements Schema
{
public Type $type = Type::COLLECTION;
/**
* @param Collection<FieldInterface> $fields
* @param array<string> $visible
*/
function __construct(
public string $name,
public string $label,
public array $visible,
public Collection $fields,
public bool $isEntry = false,
public string $color = "",
public string $titleTemplate = "",
)
{
}
}
+17
View File
@@ -0,0 +1,17 @@
<?php
namespace Lucent\Schema;
class FieldInfo
{
public function __construct(
public string $name,
public string $label,
public FieldType $type,
)
{
}
}
+11
View File
@@ -0,0 +1,11 @@
<?php
namespace Lucent\Schema;
interface FieldInterface
{
public function format(array $input, array $output): array;
}
+15
View File
@@ -0,0 +1,15 @@
<?php
namespace Lucent\Schema;
enum FieldType: string
{
case STRING = 'string';
case NUMBER = 'number';
case BOOLEAN = 'boolean';
case FILE = 'file';
case JSON = 'json';
case REFERENCE = 'reference';
case TAB = 'tab';
}
+28
View File
@@ -0,0 +1,28 @@
<?php
namespace Lucent\Schema;
use Lucent\Primitive\Collection;
class FilesSchema implements Schema
{
public Type $type = Type::FILES;
/**
* @param Collection<FieldInterface> $fields
*/
function __construct(
public string $name,
public string $label,
public Collection $fields,
public string $path,
public bool $isEntry = false,
public string $color = "",
public string $titleTemplate = "",
)
{
}
}
+31
View File
@@ -0,0 +1,31 @@
<?php
namespace Lucent\Schema;
use Lucent\Field\Field;
use Lucent\View\View;
/**
* @return string[]
**/
function visibleFields(Schema $schema, ?View $view = null): array
{
$visibleFieldNames = $schema->visible;
if (!empty($view)) {
$visibleFieldNames = $view->visible;
}
return $schema->fields
->filter(function (Field $f) use ($visibleFieldNames) {
if ($f->trashed || $f->ui == "tab") {
return false;
}
return $visibleFieldNames->contains($f->name->value);
})
->values()
->map(fn(Field $f) => $f->name->value)
->toArray();
}
+27
View File
@@ -0,0 +1,27 @@
<?php
namespace Lucent\Schema;
final class Nullable
{
public function __construct(
public bool $nullable,
public mixed $value,
public mixed $default,
)
{
}
public function value(): mixed
{
if (!empty($this->value)) {
return $this->value;
}
if ($this->nullable) {
return null;
}
return $this->default;
}
}
+9
View File
@@ -0,0 +1,9 @@
<?php
namespace Lucent\Schema;
interface Schema
{
}
+144
View File
@@ -0,0 +1,144 @@
<?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"] ?? [],
fields: (new Collection($schemaArr["fields"]))->map([$this, 'mapFields']),
isEntry: $schemaArr["isEntry"],
color: $schemaArr["color"],
titleTemplate: $schemaArr["titleTemplate"],
),
"files" => new FilesSchema(
name: $schemaArr["name"],
label: $schemaArr["label"],
fields: (new Collection($schemaArr["fields"]))->map([$this, 'mapFields']),
path: $schemaArr["path"],
isEntry: $schemaArr["isEntry"],
color: $schemaArr["color"],
titleTemplate: $schemaArr["titleTemplate"]
)
};
}
public function mapFields(array $field): FieldInterface
{
$className = "\\Lucent\Schema\Ui\\" . ucfirst($field["ui"]);
unset($field["ui"]);
return new $className(...$field);
}
//
// /**
// * @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);
// }
//
// }
}
+10
View File
@@ -0,0 +1,10 @@
<?php
namespace Lucent\Schema;
enum Type: string
{
case COLLECTION = 'collection';
case FILES = 'files';
}
+44
View File
@@ -0,0 +1,44 @@
<?php
namespace Lucent\Schema\Ui;
use Lucent\Schema\FieldInfo;
use Lucent\Schema\FieldInterface;
use Lucent\Schema\FieldType;
use Lucent\Schema\Nullable;
use Lucent\Schema\Validator\RequiredInterface;
class Block implements FieldInterface, RequiredInterface
{
public FieldInfo $info;
public function __construct(
public string $name,
public string $label,
public bool $nullable = false,
public bool $required = false,
public string $default = "",
public bool $readonly = false,
public string $group = "",
)
{
$this->info = new FieldInfo("block", "Block editor", FieldType::JSON);
}
public function format(array $input, array $output): array
{
$value = $input[$this->name] ?? null;
if (is_string($value)) {
$value = json_decode($value, true);
}
$output[$this->name] = (new Nullable($this->nullable, $value, []))->value();
return $output;
}
public function failRequired(mixed $value): bool
{
return empty($value);
}
}
+48
View File
@@ -0,0 +1,48 @@
<?php
namespace Lucent\Schema\Ui;
use Lucent\Schema\FieldInfo;
use Lucent\Schema\FieldInterface;
use Lucent\Schema\FieldType;
use Lucent\Schema\Nullable;
use Lucent\Schema\Validator\RequiredInterface;
use function is_bool;
class Checkbox implements FieldInterface, RequiredInterface
{
public FieldInfo $info;
public function __construct(
public string $name,
public string $label,
public bool $required = false,
public bool $nullable = false,
public string $default = "",
public bool $readonly = false,
public string $group = "",
)
{
$this->info = new FieldInfo("checkbox", "Block Checkbox", FieldType::BOOLEAN);
}
public function format(array $input, array $output): array
{
$value = $input[$this->name] ?? null;
if (is_bool($value)) {
$newValue = $value;
} else {
$newValue = (new Nullable($this->nullable, $value, false))->value();
}
$output[$this->name] = $newValue;
return $output;
}
public function failRequired(mixed $value): bool
{
return (bool)$value !== true;
}
}
+44
View File
@@ -0,0 +1,44 @@
<?php
namespace Lucent\Schema\Ui;
use Lucent\Schema\FieldInfo;
use Lucent\Schema\FieldInterface;
use Lucent\Schema\FieldType;
use Lucent\Schema\Nullable;
use Lucent\Schema\Validator\RequiredInterface;
class Color implements FieldInterface, RequiredInterface
{
public FieldInfo $info;
public function __construct(
public string $name,
public string $label,
public bool $required = false,
public bool $nullable = false,
public string $default = "",
public bool $readonly = false,
public string $optionsFrom = "",
public string $optionsField = "",
public bool $optionsSuggest = false,
public string $group = "",
)
{
$this->info = new FieldInfo("color", "Color", FieldType::STRING);
}
public function format(array $input, array $output): array
{
$value = $input[$this->name] ?? null;
$output[$this->name] = (new Nullable($this->nullable, $value, ""))->value();
return $output;
}
public function failRequired(mixed $value): bool
{
return empty(trim($value));
}
}
+73
View File
@@ -0,0 +1,73 @@
<?php
namespace Lucent\Schema\Ui;
use Carbon\Carbon;
use Lucent\Schema\FieldInfo;
use Lucent\Schema\FieldInterface;
use Lucent\Schema\FieldType;
use Lucent\Schema\Nullable;
use Lucent\Schema\Validator\MinMaxInterface;
use Lucent\Schema\Validator\RequiredInterface;
class Date implements FieldInterface, RequiredInterface, MinMaxInterface
{
public FieldInfo $info;
public function __construct(
public string $name,
public string $label,
public bool $required = false,
public bool $nullable = false,
public ?Carbon $min = null,
public ?Carbon $max = null,
public string $default = "",
public bool $readonly = false,
public string $optionsFrom = "",
public string $optionsField = "",
public bool $optionsSuggest = false,
public string $group = "",
)
{
$this->info = new FieldInfo("date", "Date", FieldType::STRING);
}
public function format(array $input, array $output): array
{
$value = $input[$this->name] ?? null;
if (empty($value)) {
$newValue = (new Nullable($this->nullable, null, ""))->value();
} else {
$date = Carbon::parse($value);
$dateFormatted = $date->format("Y-m-d");
$newValue = (new Nullable($this->nullable, $dateFormatted, ""))->value();
}
$output[$this->name] = $newValue;
return $output;
}
public function failRequired(mixed $value): bool
{
return empty(trim($value));
}
public function failMin(mixed $value): bool
{
if (empty($this->value)) {
return false;
}
return $value->lessThanOrEqualTo($this->min);
}
public function failMax(mixed $value): bool
{
if (empty($this->value)) {
return false;
}
return $value->greaterThan($this->max);
}
}
+73
View File
@@ -0,0 +1,73 @@
<?php
namespace Lucent\Schema\Ui;
use Carbon\Carbon;
use Lucent\Schema\FieldInfo;
use Lucent\Schema\FieldInterface;
use Lucent\Schema\FieldType;
use Lucent\Schema\Nullable;
use Lucent\Schema\Validator\MinMaxInterface;
use Lucent\Schema\Validator\RequiredInterface;
class Datetime implements FieldInterface, RequiredInterface, MinMaxInterface
{
public FieldInfo $info;
public function __construct(
public string $name,
public string $label,
public bool $required = false,
public bool $nullable = false,
public ?Carbon $min = null,
public ?Carbon $max = null,
public string $default = "",
public bool $readonly = false,
public string $optionsFrom = "",
public string $optionsField = "",
public bool $optionsSuggest = false,
public string $group = "",
)
{
$this->info = new FieldInfo("datetime", "Datetime", FieldType::STRING);
}
public function format(array $input, array $output): array
{
$value = $input[$this->name] ?? null;
if (empty($value)) {
$newValue = (new Nullable($this->nullable, null, ""))->value();
} else {
$date = Carbon::parse($value);
$dateFormatted = $date->toJSON();
$newValue = (new Nullable($this->nullable, $dateFormatted, ""))->value();
}
$output[$this->name] = $newValue;
return $output;
}
public function failRequired(mixed $value): bool
{
return empty(trim($value));
}
public function failMin(mixed $value): bool
{
if (empty($this->value)) {
return false;
}
return $value->lessThanOrEqualTo($this->min);
}
public function failMax(mixed $value): bool
{
if (empty($this->value)) {
return false;
}
return $value->greaterThan($this->max);
}
}
+55
View File
@@ -0,0 +1,55 @@
<?php
namespace Lucent\Schema\Ui;
use Lucent\Schema\FieldInfo;
use Lucent\Schema\FieldInterface;
use Lucent\Schema\FieldType;
use Lucent\Schema\Validator\MinMaxInterface;
class File implements FieldInterface, MinMaxInterface
{
public FieldInfo $info;
/**
* @param string[] $collections
*/
public function __construct(
public string $name,
public string $label,
public string $mime = "",
public ?int $min = null,
public ?int $max = null,
public array $collections = [],
public string $group = "",
)
{
$this->info = new FieldInfo("file", "File", FieldType::FILE);
}
public function format(array $input, array $output): array
{
return $output;
}
public function failMin(mixed $value): bool
{
if (is_null($value)) {
return false;
}
return count($value) < $this->min;
}
public function failMax(mixed $value): bool
{
if (is_null($value)) {
return false;
}
return count($value) < $this->min;
}
}
+45
View File
@@ -0,0 +1,45 @@
<?php
namespace Lucent\Schema\Ui;
use Lucent\Schema\FieldInfo;
use Lucent\Schema\FieldInterface;
use Lucent\Schema\FieldType;
use Lucent\Schema\Nullable;
use Lucent\Schema\Validator\RequiredInterface;
class Json implements FieldInterface, RequiredInterface
{
public FieldInfo $info;
public function __construct(
public string $name,
public string $label,
public bool $required = false,
public string $default = "",
public bool $readonly = false,
public bool $nullable = false,
public string $group = "",
)
{
$this->info = new FieldInfo("json", "JSON", FieldType::JSON);
}
public function format(array $input, array $output): array
{
$value = $input[$this->name] ?? null;
if (is_string($value)) {
$value = json_decode($value, true);
}
$output[$this->name] = (new Nullable($this->nullable, $value, []))->value();
return $output;
}
public function failRequired(mixed $value): bool
{
return empty($value);
}
}
+77
View File
@@ -0,0 +1,77 @@
<?php
namespace Lucent\Schema\Ui;
use Lucent\Schema\FieldInfo;
use Lucent\Schema\FieldInterface;
use Lucent\Schema\FieldType;
use Lucent\Schema\Nullable;
use Lucent\Schema\Validator\MinMaxInterface;
use Lucent\Schema\Validator\RequiredInterface;
class Number implements FieldInterface, RequiredInterface, MinMaxInterface
{
public FieldInfo $info;
public function __construct(
public string $name,
public string $label,
public bool $required = false,
public bool $nullable = false,
public int $decimals = 0,
public ?int $min = null,
public ?int $max = null,
public ?float $default = null,
public bool $readonly = false,
public string $optionsFrom = "",
public string $optionsField = "",
public bool $optionsSuggest = false,
public string $group = "",
)
{
$this->info = new FieldInfo("number", "Number", FieldType::NUMBER);
}
public function format(array $input, array $output): array
{
$value = $input[$this->name] ?? null;
if (!is_numeric($value)) {
$newValue = null;
} else {
$newValue = number_format(
$value,
$this->decimals,
".",
""
);
$newValue = $this->decimals === 0 ? (int)$newValue : floatval($newValue);
}
$output[$this->name] = (new Nullable($this->nullable, $newValue, 0))->value();
return $output;
}
public function failRequired(mixed $value): bool
{
return is_null($value);
}
public function failMin(mixed $value): bool
{
if (is_null($value)) {
return false;
}
return $value < $this->min;
}
public function failMax(mixed $value): bool
{
if (is_null($value)) {
return false;
}
return $value > $this->min;
}
}
+55
View File
@@ -0,0 +1,55 @@
<?php
namespace Lucent\Schema\Ui;
use Lucent\Schema\FieldInfo;
use Lucent\Schema\FieldInterface;
use Lucent\Schema\FieldType;
use Lucent\Schema\Validator\MinMaxInterface;
class Reference implements FieldInterface, MinMaxInterface
{
public FieldInfo $info;
/**
* @param string[] $collections
*/
public function __construct(
public string $name,
public string $label,
public string $mime = "",
public ?int $min = null,
public ?int $max = null,
public array $collections = [],
public string $layout = "",
public string $group = "",
)
{
$this->info = new FieldInfo("reference", "Reference", FieldType::REFERENCE);
}
public function format(array $input, array $output): array
{
return $output;
}
public function failMin(mixed $value): bool
{
if (is_null($value)) {
return false;
}
return count($value) < $this->min;
}
public function failMax(mixed $value): bool
{
if (is_null($value)) {
return false;
}
return count($value) < $this->min;
}
}
+41
View File
@@ -0,0 +1,41 @@
<?php
namespace Lucent\Schema\Ui;
use Lucent\Schema\FieldInfo;
use Lucent\Schema\FieldInterface;
use Lucent\Schema\FieldType;
use Lucent\Schema\Nullable;
use Lucent\Schema\Validator\RequiredInterface;
class Rich implements FieldInterface, RequiredInterface
{
public FieldInfo $info;
public function __construct(
public string $name,
public string $label,
public bool $required = false,
public bool $nullable = false,
public string $default = "",
public ?int $min = null,
public ?int $max = null,
public bool $readonly = false,
public string $group = "",
)
{
$this->info = new FieldInfo("rich", "Rich editor", FieldType::STRING);
}
public function format(array $input, array $output): array
{
$value = $input[$this->name] ?? null;
$output[$this->name] = (new Nullable($this->nullable, $value, ""))->value();
return $output;
}
public function failRequired(mixed $value): bool
{
return empty(trim($value));
}
}
+46
View File
@@ -0,0 +1,46 @@
<?php
namespace Lucent\Schema\Ui;
use Lucent\Schema\FieldInfo;
use Lucent\Schema\FieldInterface;
use Lucent\Schema\FieldType;
use Lucent\Schema\Nullable;
use Lucent\Schema\Validator\RequiredInterface;
class Text implements FieldInterface, RequiredInterface
{
public FieldInfo $info;
public function __construct(
public string $name,
public string $label,
public bool $required = false,
public bool $nullable = false,
public ?int $min = null,
public ?int $max = null,
public string $default = "",
public bool $readonly = false,
public string $optionsFrom = "",
public string $optionsField = "",
public bool $optionsSuggest = false,
public string $group = "",
)
{
$this->info = new FieldInfo("text", "Text", FieldType::STRING);
}
public function format(array $input, array $output): array
{
$value = $input[$this->name] ?? null;
$output[$this->name] = (new Nullable($this->nullable, $value, ""))->value();
return $output;
}
public function failRequired(mixed $value): bool
{
return empty(trim($value));
}
}
+44
View File
@@ -0,0 +1,44 @@
<?php
namespace Lucent\Schema\Ui;
use Lucent\Schema\FieldInfo;
use Lucent\Schema\FieldInterface;
use Lucent\Schema\FieldType;
use Lucent\Schema\Nullable;
use Lucent\Schema\Validator\RequiredInterface;
class Textarea implements FieldInterface, RequiredInterface
{
public FieldInfo $info;
public function __construct(
public string $name,
public string $label,
public bool $required = false,
public bool $nullable = false,
public ?int $min = null,
public ?int $max = null,
public string $default = "",
public bool $readonly = false,
public string $group = "",
)
{
$this->info = new FieldInfo("textarea", "Textarea", FieldType::STRING);
}
public function format(array $input, array $output): array
{
$value = $input[$this->name] ?? null;
$output[$this->name] = (new Nullable($this->nullable, $value, ""))->value();
return $output;
}
public function failRequired(mixed $value): bool
{
return empty(trim($value));
}
}
+40
View File
@@ -0,0 +1,40 @@
<?php
namespace Lucent\Schema\Ui;
use Lucent\Schema\FieldInfo;
use Lucent\Schema\FieldInterface;
use Lucent\Schema\FieldType;
use Lucent\Schema\Nullable;
use Lucent\Schema\Validator\RequiredInterface;
class Uuid implements FieldInterface, RequiredInterface
{
public FieldInfo $info;
public function __construct(
public string $name,
public string $label,
public bool $required = false,
public bool $nullable = false,
public bool $readonly = false,
public string $group = "",
)
{
$this->info = new FieldInfo("uuid", "FieldType", FieldType::STRING);
}
public function format(array $input, array $output): array
{
$value = $input[$this->name] ?? null;
$output[$this->name] = (new Nullable($this->nullable, $value, ""))->value();
return $output;
}
public function failRequired(mixed $value): bool
{
return empty(trim($value));
}
}
+11
View File
@@ -0,0 +1,11 @@
<?php
namespace Lucent\Schema\Validator;
interface MinMaxInterface
{
public function failMin(mixed $value): bool;
public function failMax(mixed $value): bool;
}
@@ -0,0 +1,9 @@
<?php
namespace Lucent\Schema\Validator;
interface RequiredInterface
{
public function failRequired(mixed $value): bool;
}
+78
View File
@@ -0,0 +1,78 @@
<?php
namespace Lucent\Schema\Validator;
use Lucent\Channel\ChannelService;
use Lucent\Edge\EdgeCollection;
use Lucent\Primitive\Collection;
use Lucent\Record\RecordData;
use Lucent\Schema\FieldInterface;
class Validator
{
function __construct(
public ChannelService $channelService
)
{
}
/**
* @return Collection<ValidatorError>
*/
public function check(
string $schemaName,
RecordData $data,
?EdgeCollection $edges,
): Collection
{
$schema = $this->channelService->getSchema($schemaName)->get();
return $schema->fields
->map(fn(FieldInterface $f) => $this->validate($f, $data, $edges))
->filter(fn(?ValidatorError $error) => !empty($error))
->values();
}
public function validate(FieldInterface $field, RecordData $recordData, ?EdgeCollection $edges): ?ValidatorError
{
$value = $recordData->get($field->name);
if ($field instanceof RequiredInterface && $field->required && $field->failRequired($value)) {
return new ValidatorError($field->name, $field->label, "Field is required");
}
if ($field instanceof MinMaxInterface && !is_null($field->min) && $field->failMin($value)) {
return new ValidatorError($field->name, $field->label, "The minimum {$field->label} can be {$field->min}");
}
if ($field instanceof MinMaxInterface && !is_null($field->max) && $field->failMax($value)) {
return new ValidatorError($field->name, $field->label, "The maximum {$field->label} can be {$field->min}");
}
return null;
}
/**
* @param Collection<ValidatorError> $errors
* @throws ValidatorException
*/
public function throwException(Collection $errors): void
{
throw new ValidatorException($this->errorsByField($errors));
}
/**
* @param Collection<ValidatorError> $errors
* @return array<string,ValidatorError>
**/
public function errorsByField(Collection $errors): array
{
return collect($errors)->keyBy("fieldName")->toArray();
}
}
+16
View File
@@ -0,0 +1,16 @@
<?php
namespace Lucent\Schema\Validator;
class ValidatorError
{
function __construct(
public readonly string $fieldName,
public readonly string $fieldLabel,
public readonly string $message,
)
{
}
}
@@ -0,0 +1,32 @@
<?php
namespace Lucent\Schema\Validator;
use Exception;
final class ValidatorException extends Exception
{
private array $validatorErrors;
// Redefine the exception so message isn't optional
public function __construct(array $message, int $code = 0, Exception $previous = null)
{
// make sure everything is assigned properly
$this->validatorErrors = $message;
parent::__construct("You have one or more validation errors", $code, $previous);
}
public function getValidatorErrors(): array
{
return $this->validatorErrors;
}
public function getFirstValidatorError(): array
{
return (array)array_shift($this->validatorErrors);
}
}
@@ -0,0 +1,13 @@
<?php
namespace Lucent\Schema\Validator;
interface ValidatorInterface
{
public function validate(): ValidatorInterface;
public function value(): ValidatorError;
public function hasError(): bool;
}