Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
88.89% covered (warning)
88.89%
24 / 27
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
ModuleSectionDto
88.46% covered (warning)
88.46%
23 / 26
66.67% covered (warning)
66.67%
2 / 3
11.19
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
81.25% covered (warning)
81.25%
13 / 16
0.00% covered (danger)
0.00%
0 / 1
9.53
 toArray
100.00% covered (success)
100.00%
9 / 9
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 * Module Section / Block DTO.
13 *
14 * @package App\Core\ModuleBuilder\Domain\Model
15 */
16final readonly class ModuleSectionDto
17{
18    /**
19     * @param string      $name       Section machine name slug.
20     * @param string      $label      Section display title.
21     * @param int|null    $id         Existing primary key if editing.
22     * @param string|null $iconClass  Optional section icon.
23     * @param int         $sortOrder  Section visual order.
24     * @param bool        $isDefault  Whether this is the primary default section.
25     * @param bool        $isActive   Whether section is enabled.
26     */
27    public function __construct(
28        public string $name,
29        public string $label,
30        public ?int $id = null,
31        public ?string $iconClass = null,
32        public int $sortOrder = 10,
33        public bool $isDefault = false,
34        public bool $isActive = true
35    ) {
36    }
37
38    /**
39     * Creates instance from array.
40     *
41     * @param array<string, mixed> $data
42     * @return self
43     */
44    public static function fromArray(array $data): self
45    {
46        $label = trim((string) ($data['label'] ?? ''));
47        $name = trim((string) ($data['name'] ?? ''));
48        if ($name === '' && $label !== '') {
49            $name = strtolower((string) preg_replace('/\W/', '_', $label));
50            $name = trim((string) preg_replace('/_+/', '_', $name), '_');
51        }
52
53        return new self(
54            name: $name,
55            label: $label,
56            id: isset($data['id']) && is_numeric($data['id']) ? (int) $data['id'] : null,
57            iconClass: isset($data['icon_class']) && (string) $data['icon_class'] !== ''
58                ? (string) $data['icon_class']
59                : null,
60            sortOrder: isset($data['sort_order']) ? (int) $data['sort_order'] : 10,
61            isDefault: !empty($data['is_default']),
62            isActive: !isset($data['is_active']) || !empty($data['is_active'])
63        );
64    }
65
66    /**
67     * Serializes to array.
68     *
69     * @return array<string, mixed>
70     */
71    public function toArray(): array
72    {
73        return [
74            'id' => $this->id,
75            'name' => $this->name,
76            'label' => $this->label,
77            'icon_class' => $this->iconClass,
78            'sort_order' => $this->sortOrder,
79            'is_default' => $this->isDefault,
80            'is_active' => $this->isActive,
81        ];
82    }
83}