1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AlexisLefebvre\Bundle\AsyncTweetsBundle\Entity; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Tweet. |
9
|
|
|
*/ |
10
|
|
|
class Tweet |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var int |
14
|
|
|
*/ |
15
|
|
|
private $id; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var \DateTime |
19
|
|
|
*/ |
20
|
|
|
private $created_at; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
private $text; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var int |
29
|
|
|
*/ |
30
|
|
|
private $retweet_count = 0; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var int |
34
|
|
|
*/ |
35
|
|
|
private $favorite_count = 0; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var User |
39
|
|
|
*/ |
40
|
|
|
private $user; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* In timeline: false for retweeted Tweets. |
44
|
|
|
*/ |
45
|
|
|
private $in_timeline = false; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var Tweet |
49
|
|
|
*/ |
50
|
|
|
private $retweeted_status = null; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var ArrayCollection |
54
|
|
|
*/ |
55
|
|
|
private $retweeting_statuses; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var ArrayCollection |
59
|
|
|
*/ |
60
|
|
|
private $medias; |
61
|
|
|
|
62
|
14 |
|
public function __construct() |
63
|
|
|
{ |
64
|
14 |
|
$this->medias = new ArrayCollection(); |
65
|
14 |
|
$this->retweeting_statuses = new ArrayCollection(); |
66
|
14 |
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Set id. |
70
|
|
|
* |
71
|
|
|
* @return Tweet |
72
|
|
|
*/ |
73
|
14 |
|
public function setId(int $id) |
74
|
|
|
{ |
75
|
14 |
|
$this->id = $id; |
76
|
|
|
|
77
|
14 |
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Get id. |
82
|
|
|
* |
83
|
|
|
* @return int |
84
|
|
|
*/ |
85
|
9 |
|
public function getId(): int |
86
|
|
|
{ |
87
|
9 |
|
return $this->id; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Set created_at. |
92
|
|
|
* |
93
|
|
|
* @param \DateTime $createdAt |
94
|
|
|
* |
95
|
|
|
* @return Tweet |
96
|
|
|
*/ |
97
|
14 |
|
public function setCreatedAt(\DateTime $createdAt) |
98
|
|
|
{ |
99
|
14 |
|
$this->created_at = $createdAt; |
100
|
|
|
|
101
|
14 |
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Get created_at. |
106
|
|
|
* |
107
|
|
|
* @return \DateTime |
108
|
|
|
*/ |
109
|
10 |
|
public function getCreatedAt() |
110
|
|
|
{ |
111
|
10 |
|
return $this->created_at; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Set text. |
116
|
|
|
* |
117
|
|
|
* @param string $text |
118
|
|
|
* |
119
|
|
|
* @return Tweet |
120
|
|
|
*/ |
121
|
14 |
|
public function setText($text) |
122
|
|
|
{ |
123
|
14 |
|
$this->text = $text; |
124
|
|
|
|
125
|
14 |
|
return $this; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Get text. |
130
|
|
|
* |
131
|
|
|
* @return string |
132
|
|
|
*/ |
133
|
11 |
|
public function getText() |
134
|
|
|
{ |
135
|
11 |
|
return $this->text; |
136
|
|
|
} |
137
|
|
|
|
138
|
6 |
|
public function getTextLinkified() |
139
|
|
|
{ |
140
|
|
|
/* @see http://stackoverflow.com/questions/507436/how-do-i-linkify-urls-in-a-string-with-php/507459#507459 */ |
141
|
6 |
|
return preg_replace( |
142
|
6 |
|
'~[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]~', |
143
|
6 |
|
'<a href="\\0">\\0</a>', |
144
|
6 |
|
$this->getText() |
145
|
|
|
); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Set retweet_count. |
150
|
|
|
* |
151
|
|
|
* @param int $retweetCount |
152
|
|
|
* |
153
|
|
|
* @return Tweet |
154
|
|
|
*/ |
155
|
14 |
|
public function setRetweetCount($retweetCount) |
156
|
|
|
{ |
157
|
14 |
|
$this->retweet_count = $retweetCount; |
158
|
|
|
|
159
|
14 |
|
return $this; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Get retweet_count. |
164
|
|
|
* |
165
|
|
|
* @return int |
166
|
|
|
*/ |
167
|
8 |
|
public function getRetweetCount() |
168
|
|
|
{ |
169
|
8 |
|
return $this->retweet_count; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Set favorite_count. |
174
|
|
|
* |
175
|
|
|
* @param int $favoriteCount |
176
|
|
|
* |
177
|
|
|
* @return Tweet |
178
|
|
|
*/ |
179
|
14 |
|
public function setFavoriteCount($favoriteCount) |
180
|
|
|
{ |
181
|
14 |
|
$this->favorite_count = $favoriteCount; |
182
|
|
|
|
183
|
14 |
|
return $this; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Get favorite_count. |
188
|
|
|
* |
189
|
|
|
* @return int |
190
|
|
|
*/ |
191
|
8 |
|
public function getFavoriteCount() |
192
|
|
|
{ |
193
|
8 |
|
return $this->favorite_count; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Set user. |
198
|
|
|
* |
199
|
|
|
* @param User $user |
200
|
|
|
* |
201
|
|
|
* @return Tweet |
202
|
|
|
*/ |
203
|
14 |
|
public function setUser(User $user) |
204
|
|
|
{ |
205
|
14 |
|
$this->user = $user; |
206
|
14 |
|
$this->user->addTweet($this); |
207
|
|
|
|
208
|
14 |
|
return $this; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Get User. |
213
|
|
|
* |
214
|
|
|
* @return User |
215
|
|
|
*/ |
216
|
9 |
|
public function getUser() |
217
|
|
|
{ |
218
|
9 |
|
return $this->user; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Set in timeline. |
223
|
|
|
* |
224
|
|
|
* @param bool $inTimeline |
225
|
|
|
* |
226
|
|
|
* @return Tweet |
227
|
|
|
*/ |
228
|
14 |
|
public function setInTimeline($inTimeline) |
229
|
|
|
{ |
230
|
14 |
|
$this->in_timeline = $inTimeline; |
231
|
|
|
|
232
|
14 |
|
return $this; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Get in timeline. |
237
|
|
|
* |
238
|
|
|
* @return bool |
239
|
|
|
*/ |
240
|
2 |
|
public function isInTimeline() |
241
|
|
|
{ |
242
|
2 |
|
return $this->in_timeline; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Set retweeted |
247
|
|
|
* "This attribute contains a representation of the original Tweet |
248
|
|
|
* that was retweeted.". |
249
|
|
|
* |
250
|
|
|
* @param self $retweetedStatus |
|
|
|
|
251
|
|
|
* |
252
|
|
|
* @return Tweet |
253
|
|
|
*/ |
254
|
13 |
|
public function setRetweetedStatus(self $retweetedStatus) |
255
|
|
|
{ |
256
|
13 |
|
$this->retweeted_status = $retweetedStatus; |
|
|
|
|
257
|
|
|
|
258
|
13 |
|
return $this; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Get retweeted status. |
263
|
|
|
* |
264
|
|
|
* @return Tweet |
265
|
|
|
*/ |
266
|
7 |
|
public function getRetweetedStatus() |
267
|
|
|
{ |
268
|
7 |
|
return $this->retweeted_status; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Get medias. |
273
|
|
|
* |
274
|
|
|
* @return ArrayCollection |
275
|
|
|
*/ |
276
|
6 |
|
public function getMedias() |
277
|
|
|
{ |
278
|
6 |
|
return $this->medias; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* Add a media. |
283
|
|
|
* |
284
|
|
|
* @param Media $media |
285
|
|
|
* |
286
|
|
|
* @return Tweet |
287
|
|
|
*/ |
288
|
13 |
|
public function addMedia(Media $media) |
289
|
|
|
{ |
290
|
13 |
|
$this->medias->add($media); |
291
|
13 |
|
$media->addTweet($this); |
292
|
|
|
|
293
|
13 |
|
return $this; |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* Remove a media. |
298
|
|
|
* |
299
|
|
|
* @param Media $media |
300
|
|
|
* |
301
|
|
|
* @return Tweet |
302
|
|
|
*/ |
303
|
|
|
public function removeMedia(Media $media) |
304
|
|
|
{ |
305
|
|
|
$this->medias->removeElement($media); |
306
|
|
|
$media->removeTweet($this); |
307
|
|
|
|
308
|
|
|
return $this; |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* Get retweeting status. |
313
|
|
|
* |
314
|
|
|
* @return ArrayCollection |
315
|
|
|
*/ |
316
|
|
|
public function getRetweetingStatuses() |
317
|
|
|
{ |
318
|
|
|
return $this->retweeting_statuses; |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* Call setter functions. |
323
|
|
|
* |
324
|
|
|
* @param \stdClass $tweetTmp |
325
|
|
|
* |
326
|
|
|
* @return Tweet |
327
|
|
|
*/ |
328
|
3 |
|
public function setValues(\stdClass $tweetTmp) |
329
|
|
|
{ |
330
|
|
|
$this |
331
|
3 |
|
->setCreatedAt(new \DateTime($tweetTmp->created_at)) |
332
|
3 |
|
->setText($tweetTmp->text) |
333
|
3 |
|
->setRetweetCount($tweetTmp->retweet_count) |
334
|
3 |
|
->setFavoriteCount($tweetTmp->favorite_count); |
335
|
|
|
|
336
|
3 |
|
return $this; |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
/** |
340
|
|
|
* Check that tweet can be deleted. |
341
|
|
|
* |
342
|
|
|
* @param int $tweetId |
343
|
|
|
* |
344
|
|
|
* @return bool |
345
|
|
|
*/ |
346
|
|
|
public function mustBeKept($tweetId) |
347
|
|
|
{ |
348
|
|
|
if (count($this->getRetweetingStatuses()) == 0) { |
349
|
|
|
// This tweet has not been retweeted |
350
|
|
|
return false; |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
// Check that this tweet has not been retweeted after $tweetId |
354
|
|
|
foreach ($this->getRetweetingStatuses() as $retweeting_status) { |
355
|
|
|
// This tweet is retweeted in the timeline, keep it |
356
|
|
|
if ($retweeting_status->getId() >= $tweetId) { |
357
|
|
|
return true; |
358
|
|
|
} |
359
|
|
|
} |
360
|
|
|
|
361
|
|
|
return false; |
362
|
|
|
} |
363
|
|
|
} |
364
|
|
|
|
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.