Passed
Push — master ( 2c100d...7a68ca )
by Peter
03:10
created

UserGroup::toData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
c 0
b 0
f 0
dl 0
loc 12
rs 10
cc 2
nc 2
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Admin\Domain\Entities;
6
7
use AbterPhp\Framework\Domain\Entities\IStringerEntity;
8
9
class UserGroup implements IStringerEntity
10
{
11
    /** @var string */
12
    protected $id;
13
14
    /** @var string */
15
    protected $identifier;
16
17
    /** @var string */
18
    protected $name;
19
20
    /** @var AdminResource[] */
21
    protected $adminResources;
22
23
    /**
24
     * UserGroup constructor.
25
     *
26
     * @param string $id
27
     * @param string $identifier
28
     * @param string $name
29
     * @param array  $adminResources
30
     */
31
    public function __construct(
32
        string $id,
33
        string $identifier,
34
        string $name,
35
        array $adminResources = []
36
    ) {
37
        $this->id         = $id;
38
        $this->identifier = $identifier;
39
        $this->name       = $name;
40
41
        $this->setAdminResources($adminResources);
42
    }
43
44
    /**
45
     * @return string
46
     */
47
    public function getId()
48
    {
49
        return $this->id;
50
    }
51
52
    /**
53
     * @param string $id
54
     */
55
    public function setId($id)
56
    {
57
        $this->id = $id;
58
    }
59
60
    /**
61
     * @return string
62
     */
63
    public function getIdentifier(): string
64
    {
65
        return $this->identifier;
66
    }
67
68
    /**
69
     * @param string $identifier
70
     *
71
     * @return $this
72
     */
73
    public function setIdentifier(string $identifier): UserGroup
74
    {
75
        $this->identifier = $identifier;
76
77
        return $this;
78
    }
79
80
    /**
81
     * @return string
82
     */
83
    public function getName(): string
84
    {
85
        return $this->name;
86
    }
87
88
    /**
89
     * @param string $name
90
     *
91
     * @return $this
92
     */
93
    public function setName(string $name): UserGroup
94
    {
95
        $this->name = $name;
96
97
        return $this;
98
    }
99
100
    /**
101
     * @return AdminResource[]
102
     */
103
    public function getAdminResources(): array
104
    {
105
        return $this->adminResources;
106
    }
107
108
    /**
109
     * @param AdminResource[] $adminResources
110
     *
111
     * @return $this
112
     */
113
    public function setAdminResources(array $adminResources): UserGroup
114
    {
115
        foreach ($adminResources as $adminResource) {
116
            assert($adminResource instanceof AdminResource, new \InvalidArgumentException());
117
        }
118
119
        $this->adminResources = $adminResources;
120
121
        return $this;
122
    }
123
124
    /**
125
     * @return string
126
     */
127
    public function __toString(): string
128
    {
129
        return $this->getName();
130
    }
131
132
    /**
133
     * @return array|null
134
     */
135
    public function toData(): ?array
136
    {
137
        $adminResourceIds = [];
138
        foreach ($this->getAdminResources() as $adminResource) {
139
            $adminResourceIds[] = $adminResource->getId();
140
        }
141
142
        return [
143
            "id"                 => $this->getId(),
144
            "identifier"         => $this->getIdentifier(),
145
            "name"               => $this->getName(),
146
            "admin_resource_ids" => $adminResourceIds,
147
        ];
148
    }
149
150
    /**
151
     * @return string
152
     */
153
    public function toJSON(): string
154
    {
155
        return json_encode($this->toData());
156
    }
157
}
158