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

ApiClient::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 ApiClient implements IStringerEntity
10
{
11
    /** @var string */
12
    protected $id;
13
14
    /** @var string */
15
    protected $userId;
16
17
    /** @var string */
18
    protected $description;
19
20
    /** @var string */
21
    protected $secret;
22
23
    /** @var AdminResource[] */
24
    protected $adminResources;
25
26
    /**
27
     * ApiClient constructor.
28
     *
29
     * @param string          $id
30
     * @param string          $userId
31
     * @param string          $description
32
     * @param string          $secret
33
     * @param AdminResource[] $adminResources
34
     */
35
    public function __construct(
36
        string $id,
37
        string $userId,
38
        string $description,
39
        string $secret,
40
        array $adminResources = []
41
    ) {
42
        $this->id             = $id;
43
        $this->userId         = $userId;
44
        $this->description    = $description;
45
        $this->secret         = $secret;
46
        $this->adminResources = $adminResources;
47
    }
48
49
    /**
50
     * @return string
51
     */
52
    public function getId()
53
    {
54
        return $this->id;
55
    }
56
57
    /**
58
     * @param string $id
59
     */
60
    public function setId($id)
61
    {
62
        $this->id = $id;
63
    }
64
65
    /**
66
     * @return string
67
     */
68
    public function getUserId(): string
69
    {
70
        return $this->userId;
71
    }
72
73
    /**
74
     * @param string $userId
75
     *
76
     * @return $this
77
     */
78
    public function setUserId(string $userId): ApiClient
79
    {
80
        $this->userId = $userId;
81
82
        return $this;
83
    }
84
85
    /**
86
     * @return string
87
     */
88
    public function getDescription(): string
89
    {
90
        return $this->description;
91
    }
92
93
    /**
94
     * @param string $description
95
     *
96
     * @return $this
97
     */
98
    public function setDescription(string $description): ApiClient
99
    {
100
        $this->description = $description;
101
102
        return $this;
103
    }
104
105
    /**
106
     * @return string
107
     */
108
    public function getSecret(): string
109
    {
110
        return $this->secret;
111
    }
112
113
    /**
114
     * @param string $secret
115
     *
116
     * @return $this
117
     */
118
    public function setSecret(string $secret): ApiClient
119
    {
120
        $this->secret = $secret;
121
122
        return $this;
123
    }
124
125
    /**
126
     * @return AdminResource[]
127
     */
128
    public function getAdminResources(): array
129
    {
130
        return $this->adminResources;
131
    }
132
133
    /**
134
     * @param AdminResource[] $adminResources
135
     *
136
     * @return $this
137
     */
138
    public function setAdminResources(array $adminResources): ApiClient
139
    {
140
        $this->adminResources = $adminResources;
141
142
        return $this;
143
    }
144
145
    /**
146
     * @return string
147
     */
148
    public function __toString(): string
149
    {
150
        return $this->getDescription();
151
    }
152
153
    /**
154
     * @return string
155
     */
156
    public function toJSON(): string
157
    {
158
        $adminResourceIds = [];
159
        foreach ($this->getAdminResources() as $adminResource) {
160
            $adminResourceIds[] = $adminResource->getId();
161
        }
162
163
        return json_encode(
164
            [
165
                "id"                => $this->getId(),
166
                "user_id"           => $this->getUserId(),
167
                "description"       => $this->getDescription(),
168
                "admin_resource_id" => $adminResourceIds,
169
            ]
170
        );
171
    }
172
}
173