1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Stack Exchange Api Client library. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2014-2016 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
|
|
|
namespace BenatEspina\StackExchangeApiClient\Model; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* The comment model class. |
16
|
|
|
* |
17
|
|
|
* @author Beñat Espiña <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
class Comment implements Model |
20
|
|
|
{ |
21
|
|
|
const POST_TYPES = ['question', 'answer']; |
22
|
|
|
|
23
|
|
|
protected $id; |
24
|
|
|
protected $body; |
25
|
|
|
protected $bodyMarkdown; |
26
|
|
|
protected $canFlag; |
27
|
|
|
protected $creationDate; |
28
|
|
|
protected $edited; |
29
|
|
|
protected $link; |
30
|
|
|
protected $owner; |
31
|
|
|
protected $postId; |
32
|
|
|
protected $postType; |
33
|
|
|
protected $replyToUser; |
34
|
|
|
protected $score; |
35
|
|
|
protected $upvoted; |
36
|
|
|
|
37
|
|
|
public static function fromProperties( |
38
|
|
|
$id, |
39
|
|
|
$body, |
40
|
|
|
$bodyMarkdown, |
41
|
|
|
$canFlag, |
42
|
|
|
\DateTimeInterface $creationDate, |
43
|
|
|
$edited, |
44
|
|
|
$link, |
45
|
|
|
$postId, |
46
|
|
|
$postType, |
47
|
|
|
$score, |
48
|
|
|
$upvoted, |
49
|
|
|
ShallowUser $owner = null, |
50
|
|
|
ShallowUser $replyToUser = null |
51
|
|
|
) { |
52
|
|
|
$instance = new self(); |
53
|
|
|
$instance |
54
|
|
|
->setId($id) |
55
|
|
|
->setBody($body) |
56
|
|
|
->setBodyMarkdown($bodyMarkdown) |
57
|
|
|
->setCanFlag($canFlag) |
58
|
|
|
->setCreationDate($creationDate) |
59
|
|
|
->setEdited($edited) |
60
|
|
|
->setLink($link) |
61
|
|
|
->setOwner($owner) |
62
|
|
|
->setPostId($postId) |
63
|
|
|
->setPostType($postType) |
64
|
|
|
->setReplyToUser($replyToUser) |
65
|
|
|
->setScore($score) |
66
|
|
|
->setUpvoted($upvoted); |
67
|
|
|
|
68
|
|
|
return $instance; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public static function fromJson(array $data) |
72
|
|
|
{ |
73
|
|
|
$instance = new self(); |
74
|
|
|
$instance |
75
|
|
|
->setId(array_key_exists('comment_id', $data) ? $data['comment_id'] : null) |
76
|
|
|
->setBody(array_key_exists('body', $data) ? $data['body'] : null) |
77
|
|
|
->setBodyMarkdown(array_key_exists('body_markdown', $data) ? $data['body_markdown'] : null) |
78
|
|
|
->setCanFlag(array_key_exists('can_flag', $data) ? $data['can_flag'] : null) |
79
|
|
|
->setCreationDate( |
80
|
|
|
array_key_exists('creation_date', $data) |
|
|
|
|
81
|
|
|
? new \DateTimeImmutable('@' . $data['creation_date']) |
82
|
|
|
: null |
83
|
|
|
) |
84
|
|
|
->setEdited(array_key_exists('edited', $data) ? $data['edited'] : null) |
85
|
|
|
->setLink(array_key_exists('link', $data) ? $data['link'] : null) |
86
|
|
|
->setOwner(array_key_exists('owner', $data) ? ShallowUser::fromJson($data['owner']) : null) |
|
|
|
|
87
|
|
|
->setPostId(array_key_exists('postId', $data) ? $data['postId'] : null) |
88
|
|
|
->setPostType(array_key_exists('post_type', $data) ? $data['post_type'] : null) |
89
|
|
|
->setReplyToUser(array_key_exists( |
|
|
|
|
90
|
|
|
'reply_to_user', $data) |
91
|
|
|
? ShallowUser::fromJson($data['reply_to_user']) |
92
|
|
|
: null |
93
|
|
|
) |
94
|
|
|
->setScore(array_key_exists('score', $data) ? $data['score'] : null) |
95
|
|
|
->setUpvoted(array_key_exists('upvoted', $data) ? $data['upvoted'] : null); |
96
|
|
|
|
97
|
|
|
return $instance; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function getId() |
|
|
|
|
101
|
|
|
{ |
102
|
|
|
return $this->id; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function setId($id) |
106
|
|
|
{ |
107
|
|
|
$this->id = $id; |
108
|
|
|
|
109
|
|
|
return $this; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function getBody() |
|
|
|
|
113
|
|
|
{ |
114
|
|
|
return $this->body; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function setBody($body) |
118
|
|
|
{ |
119
|
|
|
$this->body = $body; |
120
|
|
|
|
121
|
|
|
return $this; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function getBodyMarkdown() |
|
|
|
|
125
|
|
|
{ |
126
|
|
|
return $this->bodyMarkdown; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function setBodyMarkdown($bodyMarkdown) |
130
|
|
|
{ |
131
|
|
|
$this->bodyMarkdown = $bodyMarkdown; |
132
|
|
|
|
133
|
|
|
return $this; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function getCanFlag() |
|
|
|
|
137
|
|
|
{ |
138
|
|
|
return $this->canFlag; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function setCanFlag($canFlag) |
142
|
|
|
{ |
143
|
|
|
$this->canFlag = $canFlag; |
144
|
|
|
|
145
|
|
|
return $this; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function getCreationDate() |
149
|
|
|
{ |
150
|
|
|
return $this->creationDate; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function setCreationDate(\DateTimeInterface $creationDate) |
154
|
|
|
{ |
155
|
|
|
$this->creationDate = $creationDate; |
156
|
|
|
|
157
|
|
|
return $this; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
public function getEdited() |
|
|
|
|
161
|
|
|
{ |
162
|
|
|
return $this->edited; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
public function setEdited($edited) |
166
|
|
|
{ |
167
|
|
|
$this->edited = $edited; |
168
|
|
|
|
169
|
|
|
return $this; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public function getLink() |
|
|
|
|
173
|
|
|
{ |
174
|
|
|
return $this->link; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
public function setLink($link) |
178
|
|
|
{ |
179
|
|
|
$this->link = $link; |
180
|
|
|
|
181
|
|
|
return $this; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
public function getOwner() |
185
|
|
|
{ |
186
|
|
|
return $this->owner; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
public function setOwner(ShallowUser $owner = null) |
190
|
|
|
{ |
191
|
|
|
$this->owner = $owner; |
192
|
|
|
|
193
|
|
|
return $this; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
public function getPostId() |
|
|
|
|
197
|
|
|
{ |
198
|
|
|
return $this->postId; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
public function setPostId($postId) |
202
|
|
|
{ |
203
|
|
|
$this->postId = $postId; |
204
|
|
|
|
205
|
|
|
return $this; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
public function getPostType() |
|
|
|
|
209
|
|
|
{ |
210
|
|
|
return $this->postType; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
public function setPostType($postType) |
214
|
|
|
{ |
215
|
|
|
if (in_array($postType, self::POST_TYPES, true)) { |
216
|
|
|
$this->postType = $postType; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
return $this; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
public function getReplyToUser() |
223
|
|
|
{ |
224
|
|
|
return $this->replyToUser; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
public function setReplyToUser(ShallowUser $replyToUser = null) |
228
|
|
|
{ |
229
|
|
|
$this->replyToUser = $replyToUser; |
230
|
|
|
|
231
|
|
|
return $this; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
public function getScore() |
|
|
|
|
235
|
|
|
{ |
236
|
|
|
return $this->score; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
public function setScore($score) |
240
|
|
|
{ |
241
|
|
|
$this->score = $score; |
242
|
|
|
|
243
|
|
|
return $this; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
public function getUpvoted() |
|
|
|
|
247
|
|
|
{ |
248
|
|
|
return $this->upvoted; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
public function setUpvoted($upvoted) |
252
|
|
|
{ |
253
|
|
|
$this->upvoted = $upvoted; |
254
|
|
|
|
255
|
|
|
return $this; |
256
|
|
|
} |
257
|
|
|
} |
258
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: