Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
94.74% covered (success)
94.74%
54 / 57
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
ModuleBasicInfoDto
94.64% covered (success)
94.64%
53 / 56
66.67% covered (warning)
66.67%
2 / 3
16.04
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 fromArray
92.31% covered (success)
92.31%
36 / 39
0.00% covered (danger)
0.00%
0 / 1
14.09
 toArray
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5/** @license For full copyright and license information, please see the LICENSE.md file. */
6
7namespace App\Core\ModuleBuilder\Domain\Model;
8
9defined('AMMONLY_APP') || exit('Direct script access is forbidden.');
10
11/**
12 * Basic module parameters DTO.
13 *
14 * @package App\Core\ModuleBuilder\Domain\Model
15 */
16final readonly class ModuleBasicInfoDto
17{
18    /**
19     * @param string       $name                    Technical machine slug (e.g. 'contracts').
20     * @param string       $label                   Human readable label (e.g. 'Contracts').
21     * @param string       $type                    Module type ('crud', 'dynamic', 'log').
22     * @param string       $routeUrl                Frontend URL route (e.g. '/contracts').
23     * @param string       $tableName               Physical database table name.
24     * @param string       $tableAlias              SQL query table alias.
25     * @param string       $iconClass               Bootstrap icon class (e.g. 'bi bi-file-text').
26     * @param string       $version                 Semantic version.
27     * @param string|null  $description             Optional module description.
28     * @param list<string> $supportedViews          Supported view modes
29     *                                              ('list', 'grid', 'calendar', 'kanban', 'gantt').
30     * @param string       $defaultView             Default view mode.
31     * @param string       $defaultCreateMode       Default creation mode ('standard', 'quick').
32     * @param string       $createPresentationMode Default creation presentation ('drawer', 'modal', 'popup').
33     * @param int          $specialAccess           Special access level (1=Available, 0=System).
34     */
35    public function __construct(
36        public string $name,
37        public string $label,
38        public string $type = 'crud',
39        public string $routeUrl = '',
40        public string $tableName = '',
41        public string $tableAlias = '',
42        public string $iconClass = 'bi bi-box-seam',
43        public string $version = '0.0.1',
44        public ?string $description = null,
45        public array $supportedViews = ['list', 'grid'],
46        public string $defaultView = 'list',
47        public string $defaultCreateMode = 'standard',
48        public string $createPresentationMode = 'drawer',
49        public int $specialAccess = 1
50    ) {
51    }
52
53    /**
54     * Creates instance from request array with smart defaults.
55     *
56     * @param array<string, mixed> $data
57     * @return self
58     */
59    public static function fromArray(array $data): self
60    {
61        $label = trim((string) ($data['label'] ?? ''));
62        $name = trim((string) ($data['name'] ?? ''));
63        if ($name === '' && $label !== '') {
64            $name = strtolower((string) preg_replace('/\W/', '_', $label));
65            $name = trim((string) preg_replace('/_+/', '_', $name), '_');
66        }
67
68        $route = (string) ($data['route_url'] ?? $data['routeUrl'] ?? '');
69        if ($route === '' && $name !== '') {
70            $route = '/' . str_replace('_', '-', $name);
71        }
72
73        $table = (string) ($data['table_name'] ?? $data['tableName'] ?? '');
74        if ($table === '' && $name !== '') {
75            $table = 'a_mod_' . $name . '_records';
76        }
77
78        $alias = (string) ($data['table_alias'] ?? $data['tableAlias'] ?? '');
79        if ($alias === '' && $name !== '') {
80            $parts = explode('_', $name);
81            $alias = '';
82            foreach ($parts as $p) {
83                $alias .= substr($p, 0, 1);
84            }
85            if (strlen($alias) < 2) {
86                $alias = substr($name, 0, 2);
87            }
88        }
89
90        $views = (array) ($data['supported_views'] ?? ['list', 'grid']);
91        $cleanViews = array_values(array_filter(array_map('strval', $views)));
92        if ($cleanViews === []) {
93            $cleanViews = ['list', 'grid'];
94        }
95
96        return new self(
97            name: $name,
98            label: $label,
99            type: (string) ($data['type'] ?? 'crud'),
100            routeUrl: $route,
101            tableName: $table,
102            tableAlias: $alias,
103            iconClass: (string) ($data['icon_class'] ?? 'bi bi-box-seam'),
104            version: (string) ($data['version'] ?? '0.0.1'),
105            description: isset($data['description']) ? (string) $data['description'] : null,
106            supportedViews: $cleanViews,
107            defaultView: (string) ($data['default_view'] ?? 'list'),
108            defaultCreateMode: (string) ($data['default_create_mode'] ?? 'standard'),
109            createPresentationMode: (string) ($data['create_presentation_mode'] ?? 'drawer'),
110            specialAccess: isset($data['special_access']) ? (int) $data['special_access'] : 1
111        );
112    }
113
114    /**
115     * Serializes to array.
116     *
117     * @return array<string, mixed>
118     */
119    public function toArray(): array
120    {
121        return [
122            'name' => $this->name,
123            'label' => $this->label,
124            'type' => $this->type,
125            'route_url' => $this->routeUrl,
126            'table_name' => $this->tableName,
127            'table_alias' => $this->tableAlias,
128            'icon_class' => $this->iconClass,
129            'version' => $this->version,
130            'description' => $this->description,
131            'supported_views' => $this->supportedViews,
132            'default_view' => $this->defaultView,
133            'default_create_mode' => $this->defaultCreateMode,
134            'create_presentation_mode' => $this->createPresentationMode,
135            'special_access' => $this->specialAccess,
136        ];
137    }
138}