Passed
Push — master ( b37186...7c51ba )
by Peter
04:28
created

UserGroup::toJSON()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 13
rs 10
c 0
b 0
f 0
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
            if (!($adminResource instanceof AdminResource)) {
117
                throw new \InvalidArgumentException();
118
            }
119
        }
120
121
        $this->adminResources = $adminResources;
122
123
        return $this;
124
    }
125
126
    /**
127
     * @return string
128
     */
129
    public function __toString(): string
130
    {
131
        return $this->getName();
132
    }
133
134
    /**
135
     * @return string
136
     */
137
    public function toJSON(): string
138
    {
139
        $adminResourceIds = [];
140
        foreach ($this->getAdminResources() as $adminResource) {
141
            $adminResourceIds[] = $adminResource->getId();
142
        }
143
144
        return json_encode(
145
            [
146
                "id"                 => $this->getId(),
147
                "identifier"         => $this->getIdentifier(),
148
                "name"               => $this->getName(),
149
                "admin_resource_ids" => $adminResourceIds,
150
            ]
151
        );
152
    }
153
}
154