ApiClient::toData()   A
last analyzed

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