Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
32 / 32
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
ModuleFilterDto
100.00% covered (success)
100.00%
31 / 31
100.00% covered (success)
100.00%
3 / 3
13
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 fromArray
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
11
 toArray
100.00% covered (success)
100.00%
13 / 13
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 Filter / View Definition DTO.
13 *
14 * @package App\Core\ModuleBuilder\Domain\Model
15 */
16final readonly class ModuleFilterDto
17{
18    /**
19     * @param string                    $name          Filter machine name (e.g. 'default').
20     * @param string                    $label         Display name (e.g. 'All Records').
21     * @param list<string>              $visibleFields Field keys visible in DataGrid table.
22     * @param int|null                  $id            Primary key if editing.
23     * @param string                    $iconClass     Filter icon.
24     * @param string                    $defaultSort   Field key for default sorting.
25     * @param string                    $defaultOrder  'ASC' or 'DESC'.
26     * @param int                       $perPage       Records per page.
27     * @param bool                      $isDefault     Whether this is the default active view.
28     * @param string                    $scope         'public' or 'private'.
29     * @param array<string, mixed>|null $conditions   JSON filter criteria.
30     */
31    public function __construct(
32        public string $name,
33        public string $label,
34        public array $visibleFields = [],
35        public ?int $id = null,
36        public string $iconClass = 'bi bi-filter',
37        public string $defaultSort = 'id',
38        public string $defaultOrder = 'DESC',
39        public int $perPage = 25,
40        public bool $isDefault = true,
41        public string $scope = 'public',
42        public ?array $conditions = null
43    ) {
44    }
45
46    /**
47     * Creates instance from array.
48     *
49     * @param array<string, mixed> $data
50     * @return self
51     */
52    public static function fromArray(array $data): self
53    {
54        $label = trim((string) ($data['label'] ?? 'Wszystkie rekordy'));
55        $name = trim((string) ($data['name'] ?? 'default'));
56        $fields = (array) ($data['visible_fields'] ?? []);
57        $cleanFields = array_values(array_filter(array_map('strval', $fields)));
58
59        return new self(
60            name: $name !== '' ? $name : 'default',
61            label: $label !== '' ? $label : 'Wszystkie rekordy',
62            visibleFields: $cleanFields,
63            id: isset($data['id']) && is_numeric($data['id']) ? (int) $data['id'] : null,
64            iconClass: (string) ($data['icon_class'] ?? 'bi bi-filter'),
65            defaultSort: (string) ($data['default_sort'] ?? 'id'),
66            defaultOrder: strtoupper((string) ($data['default_order'] ?? 'DESC')) === 'ASC' ? 'ASC' : 'DESC',
67            perPage: isset($data['per_page']) && is_numeric($data['per_page']) ? (int) $data['per_page'] : 25,
68            isDefault: !isset($data['is_default']) || !empty($data['is_default']),
69            scope: (string) ($data['scope'] ?? 'public'),
70            conditions: isset($data['conditions']) && is_array($data['conditions']) ? $data['conditions'] : null
71        );
72    }
73
74    /**
75     * Serializes to array.
76     *
77     * @return array<string, mixed>
78     */
79    public function toArray(): array
80    {
81        return [
82            'id' => $this->id,
83            'name' => $this->name,
84            'label' => $this->label,
85            'icon_class' => $this->iconClass,
86            'visible_fields' => $this->visibleFields,
87            'default_sort' => $this->defaultSort,
88            'default_order' => $this->defaultOrder,
89            'per_page' => $this->perPage,
90            'is_default' => $this->isDefault,
91            'scope' => $this->scope,
92            'conditions' => $this->conditions,
93        ];
94    }
95}