Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
96.30% |
26 / 27 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| WidgetMetadata | |
100.00% |
26 / 26 |
|
100.00% |
2 / 2 |
8 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| fromRow | |
100.00% |
25 / 25 |
|
100.00% |
1 / 1 |
7 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | /** @license For full copyright and license information, please see the LICENSE.md file. */ |
| 6 | |
| 7 | namespace App\Core\Engine\Domain\Model; |
| 8 | |
| 9 | defined('AMMONLY_APP') || exit('Direct script access is forbidden.'); |
| 10 | |
| 11 | /** |
| 12 | * Widget Metadata Value Object. |
| 13 | * |
| 14 | * Immutable value object representing a widget catalog item from a_core_widget_records. |
| 15 | * Defines widget attributes, default dimensions, constraints, and renderer class. |
| 16 | * |
| 17 | * @package App\Core\Engine\Domain\Model |
| 18 | */ |
| 19 | final readonly class WidgetMetadata |
| 20 | { |
| 21 | /** |
| 22 | * WidgetMetadata constructor. |
| 23 | * |
| 24 | * @param int $id Widget primary key. |
| 25 | * @param string $name Machine-readable widget code name. |
| 26 | * @param string $label Human-readable display title. |
| 27 | * @param string $category Widget category ('table', 'diagnostics', etc.). |
| 28 | * @param string|null $handlerClass PHP Renderer class for server rendering. |
| 29 | * @param int $defaultW Default width in Gridstack columns (1-12). |
| 30 | * @param int $defaultH Default height in Gridstack rows. |
| 31 | * @param int $minW Minimum allowed width. |
| 32 | * @param int $maxW Maximum allowed width. |
| 33 | * @param int $minH Minimum allowed height. |
| 34 | * @param int $maxH Maximum allowed height. |
| 35 | * @param bool $isResizable Whether the tile can be resized. |
| 36 | * @param bool $isMovable Whether the tile can be dragged/moved. |
| 37 | * @param bool $isActive Whether the widget is available in system. |
| 38 | * @param bool $isSystem Whether the widget is core system-protected. |
| 39 | * @param string|null $description Optional functional description. |
| 40 | * @param array<string, mixed> $defaultParams Default JSON configuration parameters. |
| 41 | */ |
| 42 | public function __construct( |
| 43 | public int $id, |
| 44 | public string $name, |
| 45 | public string $label, |
| 46 | public string $category = 'table', |
| 47 | public ?string $handlerClass = null, |
| 48 | public int $defaultW = 12, |
| 49 | public int $defaultH = 8, |
| 50 | public int $minW = 4, |
| 51 | public int $maxW = 12, |
| 52 | public int $minH = 4, |
| 53 | public int $maxH = 24, |
| 54 | public bool $isResizable = true, |
| 55 | public bool $isMovable = true, |
| 56 | public bool $isActive = true, |
| 57 | public bool $isSystem = false, |
| 58 | public ?string $description = null, |
| 59 | public array $defaultParams = [], |
| 60 | ) { |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Creates a WidgetMetadata instance from a raw database row array. |
| 65 | * |
| 66 | * @param array<string, mixed> $row Raw database row from a_core_widget_records. |
| 67 | * @return self Hydrated WidgetMetadata value object. |
| 68 | */ |
| 69 | public static function fromRow(array $row): self |
| 70 | { |
| 71 | $defaultParams = []; |
| 72 | if (isset($row['default_params']) && is_string($row['default_params'])) { |
| 73 | $decoded = json_decode($row['default_params'], true); |
| 74 | $defaultParams = is_array($decoded) ? $decoded : []; |
| 75 | } elseif (is_array($row['default_params'] ?? null)) { |
| 76 | $defaultParams = $row['default_params']; |
| 77 | } |
| 78 | |
| 79 | return new self( |
| 80 | id: (int) $row['id'], |
| 81 | name: (string) $row['name'], |
| 82 | label: (string) $row['label'], |
| 83 | category: (string) ($row['category'] ?? 'table'), |
| 84 | handlerClass: isset($row['handler_class']) ? (string) $row['handler_class'] : null, |
| 85 | defaultW: (int) ($row['default_w'] ?? 12), |
| 86 | defaultH: (int) ($row['default_h'] ?? 8), |
| 87 | minW: (int) ($row['min_w'] ?? 4), |
| 88 | maxW: (int) ($row['max_w'] ?? 12), |
| 89 | minH: (int) ($row['min_h'] ?? 4), |
| 90 | maxH: (int) ($row['max_h'] ?? 24), |
| 91 | isResizable: (bool) ($row['is_resizable'] ?? true), |
| 92 | isMovable: (bool) ($row['is_movable'] ?? true), |
| 93 | isActive: (bool) ($row['is_active'] ?? true), |
| 94 | isSystem: (bool) ($row['is_system'] ?? false), |
| 95 | description: isset($row['description']) ? (string) $row['description'] : null, |
| 96 | defaultParams: $defaultParams, |
| 97 | ); |
| 98 | } |
| 99 | } |