Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
97.44% |
38 / 39 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
| ModuleRelationDto | |
97.37% |
37 / 38 |
|
66.67% |
2 / 3 |
11 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| fromArray | |
94.74% |
18 / 19 |
|
0.00% |
0 / 1 |
9.01 | |||
| toArray | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
1 | |||
| 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\ModuleBuilder\Domain\Model; |
| 8 | |
| 9 | defined('AMMONLY_APP') || exit('Direct script access is forbidden.'); |
| 10 | |
| 11 | /** |
| 12 | * Module Relation Definition DTO. |
| 13 | * |
| 14 | * @package App\Core\ModuleBuilder\Domain\Model |
| 15 | */ |
| 16 | final readonly class ModuleRelationDto |
| 17 | { |
| 18 | /** |
| 19 | * @param string $name Machine slug (e.g. 'rel_contracts_comments'). |
| 20 | * @param string $label Tab display label (e.g. 'Komentarze'). |
| 21 | * @param string $targetModule Target module slug (e.g. 'comments'). |
| 22 | * @param int|null $id Primary key if editing. |
| 23 | * @param string $relationType '1:M' or 'M:M'. |
| 24 | * @param string $targetFieldKey Foreign key on target table (e.g. 'parent_id'). |
| 25 | * @param string|null $intermediateTable Pivot table if M:M. |
| 26 | * @param list<string> $visibleFields Field keys shown in related tab. |
| 27 | * @param int $sortOrder Display order in related tabs. |
| 28 | * @param bool $isActive Whether relation is active. |
| 29 | */ |
| 30 | public string $relationName; |
| 31 | public string $targetModuleName; |
| 32 | |
| 33 | public function __construct( |
| 34 | public string $name, |
| 35 | public string $label, |
| 36 | public string $targetModule, |
| 37 | public ?int $id = null, |
| 38 | public string $relationType = '1:M', |
| 39 | public string $targetFieldKey = 'parent_id', |
| 40 | public ?string $intermediateTable = null, |
| 41 | public array $visibleFields = [], |
| 42 | public int $sortOrder = 10, |
| 43 | public bool $isActive = true |
| 44 | ) { |
| 45 | $this->relationName = $this->name; |
| 46 | $this->targetModuleName = $this->targetModule; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Creates instance from array. |
| 51 | * |
| 52 | * @param array<string, mixed> $data |
| 53 | * @return self |
| 54 | */ |
| 55 | public static function fromArray(array $data): self |
| 56 | { |
| 57 | $label = trim((string) ($data['label'] ?? '')); |
| 58 | $targetMod = trim((string) ($data['target_module'] ?? $data['targetModuleName'] ?? '')); |
| 59 | $name = trim((string) ($data['name'] ?? $data['relationName'] ?? $data['relation_name'] ?? '')); |
| 60 | if ($name === '' && $targetMod !== '') { |
| 61 | $name = 'rel_' . $targetMod; |
| 62 | } |
| 63 | |
| 64 | $fields = (array) ($data['visible_fields'] ?? []); |
| 65 | $cleanFields = array_values(array_filter(array_map('strval', $fields))); |
| 66 | |
| 67 | return new self( |
| 68 | name: $name, |
| 69 | label: $label !== '' ? $label : ucfirst($targetMod), |
| 70 | targetModule: $targetMod, |
| 71 | id: isset($data['id']) && is_numeric($data['id']) ? (int) $data['id'] : null, |
| 72 | relationType: (string) ($data['relation_type'] ?? '1:M'), |
| 73 | targetFieldKey: (string) ($data['target_field_key'] ?? 'parent_id'), |
| 74 | intermediateTable: isset($data['intermediate_table']) ? (string) $data['intermediate_table'] : null, |
| 75 | visibleFields: $cleanFields, |
| 76 | sortOrder: isset($data['sort_order']) ? (int) $data['sort_order'] : 10, |
| 77 | isActive: !isset($data['is_active']) || !empty($data['is_active']) |
| 78 | ); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Serializes to array. |
| 83 | * |
| 84 | * @return array<string, mixed> |
| 85 | */ |
| 86 | public function toArray(): array |
| 87 | { |
| 88 | return [ |
| 89 | 'id' => $this->id, |
| 90 | 'name' => $this->name, |
| 91 | 'relation_name' => $this->name, |
| 92 | 'relationName' => $this->name, |
| 93 | 'label' => $this->label, |
| 94 | 'target_module' => $this->targetModule, |
| 95 | 'targetModuleName' => $this->targetModule, |
| 96 | 'relation_type' => $this->relationType, |
| 97 | 'relationType' => $this->relationType, |
| 98 | 'target_field_key' => $this->targetFieldKey, |
| 99 | 'targetFieldKey' => $this->targetFieldKey, |
| 100 | 'intermediate_table' => $this->intermediateTable, |
| 101 | 'visible_fields' => $this->visibleFields, |
| 102 | 'sort_order' => $this->sortOrder, |
| 103 | 'is_active' => $this->isActive, |
| 104 | ]; |
| 105 | } |
| 106 | } |