feat: rewrite schema-driven widget system
This commit is contained in:
@@ -0,0 +1,98 @@
|
||||
<?php
|
||||
|
||||
namespace AKSRT\WidgetSystem;
|
||||
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
/**
|
||||
* Immutable description of one schema-driven widget.
|
||||
*/
|
||||
final class WidgetDefinition {
|
||||
private string $slug;
|
||||
private string $name;
|
||||
private string $description;
|
||||
private string $group;
|
||||
private string $renderer;
|
||||
private string $size;
|
||||
private array $fields;
|
||||
private array $common;
|
||||
private array $assets;
|
||||
|
||||
public function __construct(array $definition) {
|
||||
$slug = (string) ($definition['slug'] ?? '');
|
||||
if (!preg_match('/^[a-z][a-z0-9_]*$/', $slug)) {
|
||||
throw new \InvalidArgumentException('Invalid widget slug: ' . $slug);
|
||||
}
|
||||
|
||||
$name = trim((string) ($definition['name'] ?? ''));
|
||||
if ($name === '') {
|
||||
throw new \InvalidArgumentException('Widget name is required for ' . $slug);
|
||||
}
|
||||
|
||||
$this->slug = $slug;
|
||||
$this->name = $name;
|
||||
$this->description = trim((string) ($definition['description'] ?? ''));
|
||||
$this->group = (string) ($definition['group'] ?? 'general');
|
||||
$this->renderer = (string) ($definition['renderer'] ?? $slug);
|
||||
$this->size = in_array(($definition['size'] ?? 'any'), ['any', 'wide', 'sidebar'], true)
|
||||
? (string) $definition['size']
|
||||
: 'any';
|
||||
$this->fields = array_values((array) ($definition['fields'] ?? []));
|
||||
$this->common = array_merge(
|
||||
[
|
||||
'title' => true,
|
||||
'affix' => false,
|
||||
'visibility' => true,
|
||||
'animation' => true,
|
||||
'layout' => true,
|
||||
],
|
||||
(array) ($definition['common'] ?? [])
|
||||
);
|
||||
$this->assets = array_values(array_unique(array_map('strval', (array) ($definition['assets'] ?? []))));
|
||||
}
|
||||
|
||||
public function slug(): string {
|
||||
return $this->slug;
|
||||
}
|
||||
|
||||
public function id_base(): string {
|
||||
return 'aksrt_widget_' . $this->slug;
|
||||
}
|
||||
|
||||
public function name(): string {
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
public function description(): string {
|
||||
return $this->description;
|
||||
}
|
||||
|
||||
public function group(): string {
|
||||
return $this->group;
|
||||
}
|
||||
|
||||
public function renderer(): string {
|
||||
return $this->renderer;
|
||||
}
|
||||
|
||||
public function size(): string {
|
||||
return $this->size;
|
||||
}
|
||||
|
||||
public function assets(): array {
|
||||
return $this->assets;
|
||||
}
|
||||
|
||||
public function fields(): array {
|
||||
return array_merge(CommonFields::for_definition($this->common), $this->fields);
|
||||
}
|
||||
|
||||
public function defaults(): array {
|
||||
return FieldSchema::defaults($this->fields());
|
||||
}
|
||||
|
||||
public function normalize(array $input): array {
|
||||
return FieldSchema::normalize($this->fields(), $input);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user