Passed
Push — master ( 617db1...ffc38e )
by Peter
02:43
created

FileCategory::toJSON()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 0
dl 0
loc 14
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Files\Domain\Entities;
6
7
use AbterPhp\Admin\Domain\Entities\UserGroup;
8
use AbterPhp\Framework\Domain\Entities\IStringerEntity;
9
10
class FileCategory implements IStringerEntity
11
{
12
    /** @var string */
13
    protected $id;
14
15
    /** @var string */
16
    protected $identifier;
17
18
    /** @var string */
19
    protected $name;
20
21
    /** @var bool */
22
    protected $isPublic;
23
24
    /** @var UserGroup[] */
25
    protected $userGroups;
26
27
    /**
28
     * @param string $id
29
     * @param string $identifier
30
     * @param string $name
31
     * @param bool   $isPublic
32
     * @param array  $userGroups
33
     */
34
    public function __construct(string $id, string $identifier, string $name, bool $isPublic, array $userGroups = [])
35
    {
36
        $this->id         = $id;
37
        $this->identifier = $identifier;
38
        $this->name       = $name;
39
        $this->isPublic   = $isPublic;
40
        $this->userGroups = $userGroups;
41
    }
42
43
    /**
44
     * @return string
45
     */
46
    public function getId()
47
    {
48
        return $this->id;
49
    }
50
51
    /**
52
     * @param string $id
53
     */
54
    public function setId($id)
55
    {
56
        $this->id = $id;
57
    }
58
59
    /**
60
     * @return string
61
     */
62
    public function getIdentifier(): string
63
    {
64
        return $this->identifier;
65
    }
66
67
    /**
68
     * @param string $identifier
69
     *
70
     * @return $this
71
     */
72
    public function setIdentifier(string $identifier): FileCategory
73
    {
74
        $this->identifier = $identifier;
75
76
        return $this;
77
    }
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): FileCategory
94
    {
95
        $this->name = $name;
96
97
        return $this;
98
    }
99
100
    /**
101
     * @return bool
102
     */
103
    public function isPublic(): bool
104
    {
105
        return $this->isPublic;
106
    }
107
108
    /**
109
     * @param bool $isPublic
110
     *
111
     * @return $this
112
     */
113
    public function setIsPublic(bool $isPublic): FileCategory
114
    {
115
        $this->isPublic = $isPublic;
116
117
        return $this;
118
    }
119
120
    /**
121
     * @return UserGroup[]
122
     */
123
    public function getUserGroups(): array
124
    {
125
        return $this->userGroups;
126
    }
127
128
    /**
129
     * @param UserGroup[] $userGroups
130
     *
131
     * @return $this
132
     */
133
    public function setUserGroups(array $userGroups): FileCategory
134
    {
135
        $this->userGroups = $userGroups;
136
137
        return $this;
138
    }
139
140
    /**
141
     * @return string
142
     */
143
    public function __toString(): string
144
    {
145
        return $this->getName();
146
    }
147
148
    /**
149
     * @return string
150
     */
151
    public function toJSON(): string
152
    {
153
        $userGroupIds = [];
154
        foreach ($this->getUserGroups() as $userGroup) {
155
            $userGroupIds[] = $userGroup->getId();
156
        }
157
158
        return json_encode(
159
            [
160
                'id'             => $this->getId(),
161
                'identifier'     => $this->getIdentifier(),
162
                'name'           => $this->getName(),
163
                'is_public'      => $this->isPublic(),
164
                'user_group_ids' => $userGroupIds,
165
            ]
166
        );
167
    }
168
}
169