Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
AccessRule
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
2 / 2
2
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
 toArray
100.00% covered (success)
100.00%
21 / 21
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\Access\Domain\Model;
8
9defined('AMMONLY_APP') || exit('Direct script access is forbidden.');
10
11/**
12 * Access Rule Domain Entity.
13 *
14 * Represents an explicit access grant or denial configured by the administrator.
15 *
16 * @package App\Core\Access\Domain\Model
17 */
18final readonly class AccessRule
19{
20    /**
21     * AccessRule constructor.
22     *
23     * @param int|null          $id          Primary key or null for new rules.
24     * @param string            $moduleName  Target module name.
25     * @param AccessRuleType    $ruleType    Classification (module action or record sharing).
26     * @param AccessSubjectType $subjectType Subject actor type (user or structure).
27     * @param int               $subjectId   Subject actor identifier.
28     * @param AccessSubjectType $targetType  Target owner type (user, structure, all).
29     * @param int               $targetId    Target owner identifier.
30     * @param bool              $canCreate   Whether creation permission is affected.
31     * @param bool              $canRead     Whether read permission is affected.
32     * @param bool              $canEdit     Whether update permission is affected.
33     * @param bool              $canDelete   Whether delete permission is affected.
34     * @param bool              $isDeny      True for DENY rule, false for GRANT rule.
35     * @param string|null       $description Optional administrative note.
36     * @param string|null       $createdAt   Creation ISO timestamp.
37     * @param string|null       $updatedAt   Last update ISO timestamp.
38     * @param int|null          $createdBy   Actor user ID who defined the rule.
39     */
40    public function __construct(
41        public ?int $id,
42        public string $moduleName,
43        public AccessRuleType $ruleType,
44        public AccessSubjectType $subjectType,
45        public int $subjectId,
46        public AccessSubjectType $targetType = AccessSubjectType::USER,
47        public int $targetId = 0,
48        public bool $canCreate = false,
49        public bool $canRead = true,
50        public bool $canEdit = false,
51        public bool $canDelete = false,
52        public bool $isDeny = false,
53        public ?string $description = null,
54        public ?string $createdAt = null,
55        public ?string $updatedAt = null,
56        public ?int $createdBy = null,
57    ) {
58    }
59
60    /**
61     * Converts domain rule to serializable associative array.
62     *
63     * @return array<string, mixed> Serialized data.
64     */
65    public function toArray(): array
66    {
67        return [
68            'id' => $this->id,
69            'module_name' => $this->moduleName,
70            'rule_type' => $this->ruleType->value,
71            'rule_type_label' => $this->ruleType->label(),
72            'subject_type' => $this->subjectType->value,
73            'subject_type_label' => $this->subjectType->label(),
74            'subject_id' => $this->subjectId,
75            'target_type' => $this->targetType->value,
76            'target_type_label' => $this->targetType->label(),
77            'target_id' => $this->targetId,
78            'can_create' => $this->canCreate,
79            'can_read' => $this->canRead,
80            'can_edit' => $this->canEdit,
81            'can_delete' => $this->canDelete,
82            'is_deny' => $this->isDeny,
83            'description' => $this->description,
84            'created_at' => $this->createdAt,
85            'updated_at' => $this->updatedAt,
86            'created_by' => $this->createdBy,
87        ];
88    }
89}