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
|
|
|
* The post model class. |
26
|
|
|
* |
27
|
|
|
* @author Beñat Espiña <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
class Post implements Model |
30
|
|
|
{ |
31
|
|
|
const POST_TYPES = [ |
32
|
|
|
'question', |
33
|
|
|
'answer', |
34
|
|
|
]; |
35
|
|
|
|
36
|
|
|
protected $postId; |
37
|
|
|
protected $postType; |
38
|
|
|
protected $downvoted; |
39
|
|
|
protected $lastActivityDate; |
40
|
|
|
protected $shareLink; |
41
|
|
|
protected $title; |
42
|
|
|
protected $commentCount; |
43
|
|
|
protected $comments; |
44
|
|
|
protected $lastEditDate; |
45
|
|
|
protected $lastEditor; |
46
|
|
|
protected $downVoteCount; |
47
|
|
|
protected $upVoteCount; |
48
|
|
|
protected $creationDate; |
49
|
|
|
protected $link; |
50
|
|
|
protected $owner; |
51
|
|
|
protected $score; |
52
|
|
|
protected $body; |
53
|
|
|
protected $bodyMarkdown; |
54
|
|
|
protected $upvoted; |
55
|
|
|
|
56
|
|
|
public static function fromJson(array $data) |
57
|
|
|
{ |
58
|
|
|
$comments = []; |
59
|
|
View Code Duplication |
if (array_key_exists('comments', $data) && is_array($data['comments'])) { |
|
|
|
|
60
|
|
|
foreach ($data['comments'] as $comment) { |
61
|
|
|
$comments[] = Comment::fromJson($comment); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$instance = new self(); |
66
|
|
|
$instance |
67
|
|
|
->setPostId(array_key_exists('post_id', $data) ? $data['post_id'] : null) |
68
|
|
|
->setPostType(array_key_exists('post_type', $data) ? $data['post_type'] : null) |
69
|
|
|
->setCreationDate( |
70
|
|
|
array_key_exists('creation_date', $data) |
71
|
|
|
? new \DateTimeImmutable('@' . $data['creation_date']) |
72
|
|
|
: null |
73
|
|
|
) |
74
|
|
|
->setLastActivityDate( |
75
|
|
|
array_key_exists('last_activity_date', $data) |
76
|
|
|
? new \DateTimeImmutable('@' . $data['last_activity_date']) |
77
|
|
|
: null |
78
|
|
|
) |
79
|
|
|
->setLastEditDate( |
80
|
|
|
array_key_exists('last_edit_date', $data) |
81
|
|
|
? new \DateTimeImmutable('@' . $data['last_edit_date']) |
82
|
|
|
: null |
83
|
|
|
) |
84
|
|
|
->setLink(array_key_exists('link', $data) ? $data['link'] : null) |
85
|
|
|
->setOwner(array_key_exists('owner', $data) ? ShallowUser::fromJson($data['owner']) : null) |
|
|
|
|
86
|
|
|
->setScore(array_key_exists('score', $data) ? $data['score'] : null) |
87
|
|
|
->setBody(array_key_exists('body', $data) ? $data['body'] : null) |
88
|
|
|
->setBodyMarkdown(array_key_exists('body_markdown', $data) ? $data['body_markdown'] : null) |
89
|
|
|
->setShareLink(array_key_exists('share_link', $data) ? $data['share_link'] : null) |
90
|
|
|
->setTitle(array_key_exists('title', $data) ? $data['title'] : null) |
91
|
|
|
->setCommentCount(array_key_exists('comment_count', $data) ? $data['comment_count'] : null) |
92
|
|
|
->setComments($comments) |
93
|
|
|
->setLastEditor(array_key_exists('last_editor', $data) ? ShallowUser::fromJson($data['last_editor']) : null) |
|
|
|
|
94
|
|
|
->setDownvoted(array_key_exists('downvoted', $data) ? $data['downvoted'] : null) |
95
|
|
|
->setDownVoteCount(array_key_exists('down_vote_count', $data) ? $data['down_vote_count'] : null) |
96
|
|
|
->setUpvoted(array_key_exists('upvoted', $data) ? $data['upvoted'] : null) |
97
|
|
|
->setUpVoteCount(array_key_exists('up_vote_count', $data) ? $data['up_vote_count'] : null); |
98
|
|
|
|
99
|
|
|
return $instance; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public static function fromProperties( |
103
|
|
|
$postId, |
104
|
|
|
$postType, |
105
|
|
|
\DateTimeInterface $creationDate, |
106
|
|
|
\DateTimeInterface $lastActivityDate, |
107
|
|
|
\DateTimeInterface $lastEditDate, |
108
|
|
|
$link, |
109
|
|
|
ShallowUser $owner, |
110
|
|
|
$score, |
111
|
|
|
$body = null, |
112
|
|
|
$bodyMarkdown = null, |
113
|
|
|
$shareLink = null, |
114
|
|
|
$title = null, |
115
|
|
|
$commentCount = null, |
116
|
|
|
array $comments = [], |
117
|
|
|
$lastEditDate = null, |
|
|
|
|
118
|
|
|
$lastEditor = null, |
119
|
|
|
$downvoted = null, |
120
|
|
|
$downVoteCount = null, |
121
|
|
|
$upvoted = null, |
122
|
|
|
$upVoteCount = null |
123
|
|
|
) { |
124
|
|
|
$instance = new self(); |
125
|
|
|
$instance |
126
|
|
|
->setPostId($postId) |
127
|
|
|
->setPostType($postType) |
128
|
|
|
->setCreationDate($creationDate) |
129
|
|
|
->setLastActivityDate($lastActivityDate) |
130
|
|
|
->setLastEditDate($lastEditDate) |
131
|
|
|
->setLink($link) |
132
|
|
|
->setOwner($owner) |
133
|
|
|
->setScore($score) |
134
|
|
|
->setBody($body) |
135
|
|
|
->setBodyMarkdown($bodyMarkdown) |
136
|
|
|
->setShareLink($shareLink) |
137
|
|
|
->setTitle($title) |
138
|
|
|
->setCommentCount($commentCount) |
139
|
|
|
->setComments($comments) |
140
|
|
|
->setLastEditor($lastEditor) |
141
|
|
|
->setDownvoted($downvoted) |
142
|
|
|
->setDownVoteCount($downVoteCount) |
143
|
|
|
->setUpvoted($upvoted) |
144
|
|
|
->setUpVoteCount($upVoteCount); |
145
|
|
|
|
146
|
|
|
return $instance; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
public function getPostId() |
|
|
|
|
150
|
|
|
{ |
151
|
|
|
return $this->postId; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function setPostId($postId) |
155
|
|
|
{ |
156
|
|
|
$this->postId = $postId; |
157
|
|
|
|
158
|
|
|
return $this; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function getCreationDate() |
162
|
|
|
{ |
163
|
|
|
return $this->creationDate; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
public function setCreationDate(\DateTimeInterface $creationDate = null) |
167
|
|
|
{ |
168
|
|
|
$this->creationDate = $creationDate; |
169
|
|
|
|
170
|
|
|
return $this; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
public function getLink() |
|
|
|
|
174
|
|
|
{ |
175
|
|
|
return $this->link; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
public function setLink($link) |
179
|
|
|
{ |
180
|
|
|
$this->link = $link; |
181
|
|
|
|
182
|
|
|
return $this; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
public function getOwner() |
186
|
|
|
{ |
187
|
|
|
return $this->owner; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
public function setOwner(ShallowUser $owner = null) |
191
|
|
|
{ |
192
|
|
|
$this->owner = $owner; |
193
|
|
|
|
194
|
|
|
return $this; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
public function getScore() |
|
|
|
|
198
|
|
|
{ |
199
|
|
|
return $this->score; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
public function setScore($score) |
203
|
|
|
{ |
204
|
|
|
$this->score = $score; |
205
|
|
|
|
206
|
|
|
return $this; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
public function getBody() |
|
|
|
|
210
|
|
|
{ |
211
|
|
|
return $this->body; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
public function setBody($body) |
215
|
|
|
{ |
216
|
|
|
$this->body = $body; |
217
|
|
|
|
218
|
|
|
return $this; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
public function getBodyMarkdown() |
|
|
|
|
222
|
|
|
{ |
223
|
|
|
return $this->bodyMarkdown; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
public function setBodyMarkdown($bodyMarkdown) |
227
|
|
|
{ |
228
|
|
|
$this->bodyMarkdown = $bodyMarkdown; |
229
|
|
|
|
230
|
|
|
return $this; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
public function getUpvoted() |
|
|
|
|
234
|
|
|
{ |
235
|
|
|
return $this->upvoted; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
public function setUpvoted($upvoted) |
239
|
|
|
{ |
240
|
|
|
$this->upvoted = $upvoted; |
241
|
|
|
|
242
|
|
|
return $this; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
public function setPostType($postType) |
246
|
|
|
{ |
247
|
|
|
if (in_array($postType, self::POST_TYPES, true)) { |
248
|
|
|
$this->postType = $postType; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
return $this; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
public function getPostType() |
|
|
|
|
255
|
|
|
{ |
256
|
|
|
return $this->postType; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
public function setDownvoted($downvoted) |
260
|
|
|
{ |
261
|
|
|
$this->downvoted = $downvoted; |
262
|
|
|
|
263
|
|
|
return $this; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
public function isDownvoted() |
|
|
|
|
267
|
|
|
{ |
268
|
|
|
return $this->downvoted; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
public function setLastActivityDate(\DateTimeInterface $lastActivityDate = null) |
272
|
|
|
{ |
273
|
|
|
$this->lastActivityDate = $lastActivityDate; |
274
|
|
|
|
275
|
|
|
return $this; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
public function getLastActivityDate() |
279
|
|
|
{ |
280
|
|
|
return $this->lastActivityDate; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
public function setShareLink($shareLink) |
284
|
|
|
{ |
285
|
|
|
$this->shareLink = $shareLink; |
286
|
|
|
|
287
|
|
|
return $this; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
public function getShareLink() |
|
|
|
|
291
|
|
|
{ |
292
|
|
|
return $this->shareLink; |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
public function setTitle($title) |
296
|
|
|
{ |
297
|
|
|
$this->title = $title; |
298
|
|
|
|
299
|
|
|
return $this; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
public function getTitle() |
|
|
|
|
303
|
|
|
{ |
304
|
|
|
return $this->title; |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
public function setCommentCount($commentCount) |
308
|
|
|
{ |
309
|
|
|
$this->commentCount = $commentCount; |
310
|
|
|
|
311
|
|
|
return $this; |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
public function getCommentCount() |
|
|
|
|
315
|
|
|
{ |
316
|
|
|
return $this->commentCount; |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
public function setComments(array $comments = []) |
320
|
|
|
{ |
321
|
|
|
$this->comments = $comments; |
322
|
|
|
|
323
|
|
|
return $this; |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
public function getComments() |
327
|
|
|
{ |
328
|
|
|
return $this->comments; |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
public function setLastEditDate(\DateTimeInterface $lastEditDate = null) |
332
|
|
|
{ |
333
|
|
|
$this->lastEditDate = $lastEditDate; |
334
|
|
|
|
335
|
|
|
return $this; |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
public function getLastEditDate() |
339
|
|
|
{ |
340
|
|
|
return $this->lastEditDate; |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
public function setLastEditor(ShallowUser $lastEditor = null) |
344
|
|
|
{ |
345
|
|
|
$this->lastEditor = $lastEditor; |
346
|
|
|
|
347
|
|
|
return $this; |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
public function getLastEditor() |
351
|
|
|
{ |
352
|
|
|
return $this->lastEditor; |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
public function setDownVoteCount($downVoteCount) |
356
|
|
|
{ |
357
|
|
|
$this->downVoteCount = $downVoteCount; |
358
|
|
|
|
359
|
|
|
return $this; |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
public function getDownVoteCount() |
|
|
|
|
363
|
|
|
{ |
364
|
|
|
return $this->downVoteCount; |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
public function setUpVoteCount($upVoteCount) |
368
|
|
|
{ |
369
|
|
|
$this->upVoteCount = $upVoteCount; |
370
|
|
|
|
371
|
|
|
return $this; |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
public function getUpVoteCount() |
|
|
|
|
375
|
|
|
{ |
376
|
|
|
return $this->upVoteCount; |
377
|
|
|
} |
378
|
|
|
} |
379
|
|
|
|
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.