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: public function __construct($id = null)
63: {
64: if (!is_null($id)) {
65: $this->setId($id);
66: }
67:
68: $this->medias = new ArrayCollection();
69: $this->retweeting_statuses = new ArrayCollection();
70: }
71:
72: /**
73: * Set id.
74: *
75: * @param bigint $id
76: *
77: * @return Tweet
78: */
79: public function setId($id)
80: {
81: $this->id = $id;
82:
83: return $this;
84: }
85:
86: /**
87: * Get id.
88: *
89: * @return int
90: */
91: public function getId()
92: {
93: return $this->id;
94: }
95:
96: /**
97: * Set created_at.
98: *
99: * @param \DateTime $createdAt
100: *
101: * @return Tweet
102: */
103: public function setCreatedAt(\Datetime $createdAt)
104: {
105: $this->created_at = $createdAt;
106:
107: return $this;
108: }
109:
110: /**
111: * Get created_at.
112: *
113: * @return \DateTime
114: */
115: public function getCreatedAt()
116: {
117: return $this->created_at;
118: }
119:
120: /**
121: * Set text.
122: *
123: * @param string $text
124: *
125: * @return Tweet
126: */
127: public function setText($text)
128: {
129: $this->text = $text;
130:
131: return $this;
132: }
133:
134: /**
135: * Get text.
136: *
137: * @return string
138: */
139: public function getText()
140: {
141: return $this->text;
142: }
143:
144: public function getTextLinkified()
145: {
146: /* @see http://stackoverflow.com/questions/507436/how-do-i-linkify-urls-in-a-string-with-php/507459#507459 */
147: return preg_replace(
148: '~[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]~',
149: '<a href="\\0">\\0</a>',
150: $this->getText()
151: );
152: }
153:
154: /**
155: * Set retweet_count.
156: *
157: * @param int $retweetCount
158: *
159: * @return Tweet
160: */
161: public function setRetweetCount($retweetCount)
162: {
163: $this->retweet_count = $retweetCount;
164:
165: return $this;
166: }
167:
168: /**
169: * Get retweet_count.
170: *
171: * @return int
172: */
173: public function getRetweetCount()
174: {
175: return $this->retweet_count;
176: }
177:
178: /**
179: * Set favorite_count.
180: *
181: * @param int $favoriteCount
182: *
183: * @return Tweet
184: */
185: public function setFavoriteCount($favoriteCount)
186: {
187: $this->favorite_count = $favoriteCount;
188:
189: return $this;
190: }
191:
192: /**
193: * Get favorite_count.
194: *
195: * @return int
196: */
197: public function getFavoriteCount()
198: {
199: return $this->favorite_count;
200: }
201:
202: /**
203: * Set user.
204: *
205: * @param User $user
206: *
207: * @return Tweet
208: */
209: public function setUser(User $user)
210: {
211: $this->user = $user;
212: $this->user->addTweet($this);
213:
214: return $this;
215: }
216:
217: /**
218: * Get User.
219: *
220: * @return User
221: */
222: public function getUser()
223: {
224: return $this->user;
225: }
226:
227: /**
228: * Set in timeline.
229: *
230: * @param bool $inTimeline
231: *
232: * @return Tweet
233: */
234: public function setInTimeline($inTimeline)
235: {
236: $this->in_timeline = $inTimeline;
237:
238: return $this;
239: }
240:
241: /**
242: * Get in timeline.
243: *
244: * @return bool
245: */
246: public function isInTimeline()
247: {
248: return $this->in_timeline;
249: }
250:
251: /**
252: * Set retweeted
253: * "This attribute contains a representation of the original Tweet
254: * that was retweeted.".
255: *
256: * @param Tweet $retweetedStatus
257: *
258: * @return Tweet
259: */
260: public function setRetweetedStatus(Tweet $retweetedStatus)
261: {
262: $this->retweeted_status = $retweetedStatus;
263:
264: return $this;
265: }
266:
267: /**
268: * Get retweeted status.
269: *
270: * @return Tweet
271: */
272: public function getRetweetedStatus()
273: {
274: return $this->retweeted_status;
275: }
276:
277: /**
278: * Get medias.
279: *
280: * @return ArrayCollection
281: */
282: public function getMedias()
283: {
284: return $this->medias;
285: }
286:
287: /**
288: * Add a media.
289: *
290: * @param Media $media
291: *
292: * @return Tweet
293: */
294: public function addMedia(Media $media)
295: {
296: $this->medias->add($media);
297: $media->addTweet($this);
298:
299: return $this;
300: }
301:
302: /**
303: * Remove a media.
304: *
305: * @param Media $media
306: *
307: * @return Tweet
308: */
309: public function removeMedia(Media $media)
310: {
311: $this->medias->removeElement($media);
312: $media->removeTweet($this);
313:
314: return $this;
315: }
316:
317: /**
318: * Get retweeting status.
319: *
320: * @return ArrayCollection
321: */
322: public function getRetweetingStatuses()
323: {
324: return $this->retweeting_statuses;
325: }
326:
327: /**
328: * Call setter functions.
329: *
330: * @param \stdClass $tweetTmp
331: *
332: * @return Tweet
333: */
334: public function setValues(\stdClass $tweetTmp)
335: {
336: $this
337: ->setCreatedAt(new \Datetime($tweetTmp->created_at))
338: ->setText($tweetTmp->text)
339: ->setRetweetCount($tweetTmp->retweet_count)
340: ->setFavoriteCount($tweetTmp->favorite_count);
341:
342: return $this;
343: }
344:
345: /**
346: * Check that tweet can be deleted.
347: *
348: * @param int $tweetId
349: *
350: * @return bool
351: */
352: public function mustBeKept($tweetId)
353: {
354: if (count($this->getRetweetingStatuses()) == 0) {
355: // This tweet has not been retweeted
356: return false;
357: }
358:
359: // Check that this tweet has not been retweeted after $tweetId
360: foreach ($this->getRetweetingStatuses() as $retweeting_status) {
361: // This tweet is retweeted in the timeline, keep it
362: if ($retweeting_status->getId() >= $tweetId) {
363: return true;
364: }
365: }
366:
367: return false;
368: }
369: }
370: