1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Stack Exchange Api Client library. |
5
|
|
|
* |
6
|
|
|
* (c) Beñat Espiña <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
/* |
14
|
|
|
* This file is part of the Stack Exchange Api Client library. |
15
|
|
|
* |
16
|
|
|
* (c) Beñat Espiña <[email protected]> |
17
|
|
|
* |
18
|
|
|
* For the full copyright and license information, please view the LICENSE |
19
|
|
|
* file that was distributed with this source code. |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
namespace BenatEspina\StackExchangeApiClient\Model; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Class network activity model class. |
26
|
|
|
* |
27
|
|
|
* @author Beñat Espiña <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
class NetworkActivity implements Model |
30
|
|
|
{ |
31
|
|
|
const ACTIVITY_TYPES = [ |
32
|
|
|
'answer_posted', |
33
|
|
|
'badge_earned', |
34
|
|
|
'comment_posted', |
35
|
|
|
'question_posted', |
36
|
|
|
]; |
37
|
|
|
|
38
|
|
|
protected $accountId; |
39
|
|
|
protected $activityType; |
40
|
|
|
protected $apiSiteParameter; |
41
|
|
|
protected $badgeId; |
42
|
|
|
protected $description; |
43
|
|
|
protected $link; |
44
|
|
|
protected $postId; |
45
|
|
|
protected $score; |
46
|
|
|
protected $creationDate; |
47
|
|
|
protected $tags; |
48
|
|
|
protected $title; |
49
|
|
|
|
50
|
|
|
public static function fromJson(array $data) |
51
|
|
|
{ |
52
|
|
|
$tags = []; |
53
|
|
View Code Duplication |
if (array_key_exists('tags', $data) && is_array($data['tags'])) { |
|
|
|
|
54
|
|
|
foreach ($data['tags'] as $tag) { |
55
|
|
|
$tags[] = Tag::fromJson($tag); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$instance = new self(); |
60
|
|
|
$instance |
61
|
|
|
->setAccountId(array_key_exists('account_id', $data) ? $data['account_id'] : null) |
62
|
|
|
->setActivityType(array_key_exists('activity_type', $data) ? $data['activity_type'] : null) |
63
|
|
|
->setApiSiteParameter(array_key_exists('api_site_parameter', $data) ? $data['api_site_parameter'] : null) |
64
|
|
|
->setBadgeId(array_key_exists('badge_id', $data) ? $data['badge_id'] : null) |
65
|
|
|
->setDescription(array_key_exists('description', $data) ? $data['description'] : null) |
66
|
|
|
->setLink(array_key_exists('link', $data) ? $data['link'] : null) |
67
|
|
|
->setPostId(array_key_exists('post_id', $data) ? $data['post_id'] : null) |
68
|
|
|
->setScore(array_key_exists('score', $data) ? $data['score'] : null) |
69
|
|
|
->setCreationDate( |
70
|
|
|
array_key_exists('creation_date', $data) |
71
|
|
|
? new \DateTimeImmutable('@' . $data['creation_date']) |
72
|
|
|
: null |
73
|
|
|
) |
74
|
|
|
->setTags($tags) |
75
|
|
|
->setTitle(array_key_exists('title', $data) ? $data['title'] : null); |
76
|
|
|
|
77
|
|
|
return $instance; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public static function fromProperties( |
81
|
|
|
$accountId, |
82
|
|
|
$activityType, |
83
|
|
|
$apiSiteParameter, |
84
|
|
|
$badgeId, |
85
|
|
|
$description, |
86
|
|
|
$link, |
87
|
|
|
$postId, |
88
|
|
|
$score, |
89
|
|
|
\DateTimeInterface $creationDate, |
90
|
|
|
$tags, |
91
|
|
|
$title |
92
|
|
|
) { |
93
|
|
|
$instance = new self(); |
94
|
|
|
$instance |
95
|
|
|
->setAccountId($accountId) |
96
|
|
|
->setActivityType($activityType) |
97
|
|
|
->setApiSiteParameter($apiSiteParameter) |
98
|
|
|
->setBadgeId($badgeId) |
99
|
|
|
->setDescription($description) |
100
|
|
|
->setLink($link) |
101
|
|
|
->setPostId($postId) |
102
|
|
|
->setScore($score) |
103
|
|
|
->setCreationDate($creationDate) |
104
|
|
|
->setTags($tags) |
105
|
|
|
->setTitle($title); |
106
|
|
|
|
107
|
|
|
return $instance; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function setAccountId($accountId) |
111
|
|
|
{ |
112
|
|
|
$this->accountId = $accountId; |
113
|
|
|
|
114
|
|
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function getAccountId() |
|
|
|
|
118
|
|
|
{ |
119
|
|
|
return $this->accountId; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function setActivityType($activityType) |
123
|
|
|
{ |
124
|
|
|
if (in_array($activityType, self::ACTIVITY_TYPES, true)) { |
125
|
|
|
$this->activityType = $activityType; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function getActivityType() |
|
|
|
|
132
|
|
|
{ |
133
|
|
|
return $this->activityType; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function setApiSiteParameter($apiSiteParameter) |
137
|
|
|
{ |
138
|
|
|
$this->apiSiteParameter = $apiSiteParameter; |
139
|
|
|
|
140
|
|
|
return $this; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function getApiSiteParameter() |
|
|
|
|
144
|
|
|
{ |
145
|
|
|
return $this->apiSiteParameter; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function setBadgeId($badgeId) |
149
|
|
|
{ |
150
|
|
|
$this->badgeId = $badgeId; |
151
|
|
|
|
152
|
|
|
return $this; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
public function getBadgeId() |
|
|
|
|
156
|
|
|
{ |
157
|
|
|
return $this->badgeId; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
public function setDescription($description) |
161
|
|
|
{ |
162
|
|
|
$this->description = $description; |
163
|
|
|
|
164
|
|
|
return $this; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function getDescription() |
|
|
|
|
168
|
|
|
{ |
169
|
|
|
return $this->description; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public function setLink($link) |
173
|
|
|
{ |
174
|
|
|
$this->link = $link; |
175
|
|
|
|
176
|
|
|
return $this; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
public function getLink() |
|
|
|
|
180
|
|
|
{ |
181
|
|
|
return $this->link; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
public function setPostId($postId) |
185
|
|
|
{ |
186
|
|
|
$this->postId = $postId; |
187
|
|
|
|
188
|
|
|
return $this; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
public function getPostId() |
|
|
|
|
192
|
|
|
{ |
193
|
|
|
return $this->postId; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
public function setScore($score) |
197
|
|
|
{ |
198
|
|
|
$this->score = $score; |
199
|
|
|
|
200
|
|
|
return $this; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
public function getScore() |
|
|
|
|
204
|
|
|
{ |
205
|
|
|
return $this->score; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
public function setCreationDate(\DateTimeInterface $creationDate = null) |
209
|
|
|
{ |
210
|
|
|
$this->creationDate = $creationDate; |
211
|
|
|
|
212
|
|
|
return $this; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
public function getCreationDate() |
216
|
|
|
{ |
217
|
|
|
return $this->creationDate; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
public function setTags($tags) |
221
|
|
|
{ |
222
|
|
|
$this->tags = $tags; |
223
|
|
|
|
224
|
|
|
return $this; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
public function getTags() |
|
|
|
|
228
|
|
|
{ |
229
|
|
|
return $this->tags; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
public function setTitle($title) |
233
|
|
|
{ |
234
|
|
|
$this->title = $title; |
235
|
|
|
|
236
|
|
|
return $this; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
public function getTitle() |
|
|
|
|
240
|
|
|
{ |
241
|
|
|
return $this->title; |
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.