]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Entity/Entry.php
Add EntityTimestampsTrait to handle dates
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Entity / Entry.php
1 <?php
2
3 namespace Wallabag\CoreBundle\Entity;
4
5 use Doctrine\Common\Collections\ArrayCollection;
6 use Doctrine\ORM\Mapping as ORM;
7 use Hateoas\Configuration\Annotation as Hateoas;
8 use JMS\Serializer\Annotation\Exclude;
9 use JMS\Serializer\Annotation\Groups;
10 use JMS\Serializer\Annotation\SerializedName;
11 use JMS\Serializer\Annotation\VirtualProperty;
12 use JMS\Serializer\Annotation\XmlRoot;
13 use Symfony\Component\Validator\Constraints as Assert;
14 use Wallabag\AnnotationBundle\Entity\Annotation;
15 use Wallabag\CoreBundle\Helper\EntityTimestampsTrait;
16 use Wallabag\UserBundle\Entity\User;
17
18 /**
19 * Entry.
20 *
21 * @XmlRoot("entry")
22 * @ORM\Entity(repositoryClass="Wallabag\CoreBundle\Repository\EntryRepository")
23 * @ORM\Table(
24 * name="`entry`",
25 * options={"collate"="utf8mb4_unicode_ci", "charset"="utf8mb4"},
26 * indexes={
27 * @ORM\Index(name="created_at", columns={"created_at"}),
28 * @ORM\Index(name="uid", columns={"uid"})
29 * }
30 * )
31 * @ORM\HasLifecycleCallbacks()
32 * @Hateoas\Relation("self", href = "expr('/api/entries/' ~ object.getId())")
33 */
34 class Entry
35 {
36 use EntityTimestampsTrait;
37
38 /** @Serializer\XmlAttribute */
39 /**
40 * @var int
41 *
42 * @ORM\Column(name="id", type="integer")
43 * @ORM\Id
44 * @ORM\GeneratedValue(strategy="AUTO")
45 *
46 * @Groups({"entries_for_user", "export_all"})
47 */
48 private $id;
49
50 /**
51 * @var string
52 *
53 * @ORM\Column(name="uid", type="string", length=23, nullable=true)
54 *
55 * @Groups({"entries_for_user", "export_all"})
56 */
57 private $uid;
58
59 /**
60 * @var string
61 *
62 * @ORM\Column(name="title", type="text", nullable=true)
63 *
64 * @Groups({"entries_for_user", "export_all"})
65 */
66 private $title;
67
68 /**
69 * @var string
70 *
71 * @Assert\NotBlank()
72 * @ORM\Column(name="url", type="text", nullable=true)
73 *
74 * @Groups({"entries_for_user", "export_all"})
75 */
76 private $url;
77
78 /**
79 * @var bool
80 *
81 * @Exclude
82 *
83 * @ORM\Column(name="is_archived", type="boolean")
84 *
85 * @Groups({"entries_for_user", "export_all"})
86 */
87 private $isArchived = false;
88
89 /**
90 * @var bool
91 *
92 * @Exclude
93 *
94 * @ORM\Column(name="is_starred", type="boolean")
95 *
96 * @Groups({"entries_for_user", "export_all"})
97 */
98 private $isStarred = false;
99
100 /**
101 * @var string
102 *
103 * @ORM\Column(name="content", type="text", nullable=true)
104 *
105 * @Groups({"entries_for_user", "export_all"})
106 */
107 private $content;
108
109 /**
110 * @var \DateTime
111 *
112 * @ORM\Column(name="created_at", type="datetime")
113 *
114 * @Groups({"entries_for_user", "export_all"})
115 */
116 private $createdAt;
117
118 /**
119 * @var \DateTime
120 *
121 * @ORM\Column(name="updated_at", type="datetime")
122 *
123 * @Groups({"entries_for_user", "export_all"})
124 */
125 private $updatedAt;
126
127 /**
128 * @var \DateTime
129 *
130 * @ORM\Column(name="published_at", type="datetime", nullable=true)
131 *
132 * @Groups({"entries_for_user", "export_all"})
133 */
134 private $publishedAt;
135
136 /**
137 * @var array
138 *
139 * @ORM\Column(name="published_by", type="array", nullable=true)
140 *
141 * @Groups({"entries_for_user", "export_all"})
142 */
143 private $publishedBy;
144
145 /**
146 * @ORM\OneToMany(targetEntity="Wallabag\AnnotationBundle\Entity\Annotation", mappedBy="entry", cascade={"persist", "remove"})
147 * @ORM\JoinTable
148 *
149 * @Groups({"entries_for_user", "export_all"})
150 */
151 private $annotations;
152
153 /**
154 * @var string
155 *
156 * @ORM\Column(name="mimetype", type="text", nullable=true)
157 *
158 * @Groups({"entries_for_user", "export_all"})
159 */
160 private $mimetype;
161
162 /**
163 * @var string
164 *
165 * @ORM\Column(name="language", type="text", nullable=true)
166 *
167 * @Groups({"entries_for_user", "export_all"})
168 */
169 private $language;
170
171 /**
172 * @var int
173 *
174 * @ORM\Column(name="reading_time", type="integer", nullable=true)
175 *
176 * @Groups({"entries_for_user", "export_all"})
177 */
178 private $readingTime;
179
180 /**
181 * @var string
182 *
183 * @ORM\Column(name="domain_name", type="text", nullable=true)
184 *
185 * @Groups({"entries_for_user", "export_all"})
186 */
187 private $domainName;
188
189 /**
190 * @var string
191 *
192 * @ORM\Column(name="preview_picture", type="text", nullable=true)
193 *
194 * @Groups({"entries_for_user", "export_all"})
195 */
196 private $previewPicture;
197
198 /**
199 * @var string
200 *
201 * @ORM\Column(name="http_status", type="string", length=3, nullable=true)
202 *
203 * @Groups({"entries_for_user", "export_all"})
204 */
205 private $httpStatus;
206
207 /**
208 * @var array
209 *
210 * @ORM\Column(name="headers", type="array", nullable=true)
211 *
212 * @Groups({"entries_for_user", "export_all"})
213 */
214 private $headers;
215
216 /**
217 * @Exclude
218 *
219 * @ORM\ManyToOne(targetEntity="Wallabag\UserBundle\Entity\User", inversedBy="entries")
220 *
221 * @Groups({"export_all"})
222 */
223 private $user;
224
225 /**
226 * @ORM\ManyToMany(targetEntity="Tag", inversedBy="entries", cascade={"persist"})
227 * @ORM\JoinTable(
228 * name="entry_tag",
229 * joinColumns={
230 * @ORM\JoinColumn(name="entry_id", referencedColumnName="id", onDelete="cascade")
231 * },
232 * inverseJoinColumns={
233 * @ORM\JoinColumn(name="tag_id", referencedColumnName="id", onDelete="cascade")
234 * }
235 * )
236 */
237 private $tags;
238
239 /*
240 * @param User $user
241 */
242 public function __construct(User $user)
243 {
244 $this->user = $user;
245 $this->tags = new ArrayCollection();
246 }
247
248 /**
249 * Get id.
250 *
251 * @return int
252 */
253 public function getId()
254 {
255 return $this->id;
256 }
257
258 /**
259 * Set title.
260 *
261 * @param string $title
262 *
263 * @return Entry
264 */
265 public function setTitle($title)
266 {
267 $this->title = $title;
268
269 return $this;
270 }
271
272 /**
273 * Get title.
274 *
275 * @return string
276 */
277 public function getTitle()
278 {
279 return $this->title;
280 }
281
282 /**
283 * Set url.
284 *
285 * @param string $url
286 *
287 * @return Entry
288 */
289 public function setUrl($url)
290 {
291 $this->url = $url;
292
293 return $this;
294 }
295
296 /**
297 * Get url.
298 *
299 * @return string
300 */
301 public function getUrl()
302 {
303 return $this->url;
304 }
305
306 /**
307 * Set isArchived.
308 *
309 * @param bool $isArchived
310 *
311 * @return Entry
312 */
313 public function setArchived($isArchived)
314 {
315 $this->isArchived = $isArchived;
316
317 return $this;
318 }
319
320 /**
321 * Get isArchived.
322 *
323 * @return bool
324 */
325 public function isArchived()
326 {
327 return $this->isArchived;
328 }
329
330 /**
331 * @VirtualProperty
332 * @SerializedName("is_archived")
333 * @Groups({"entries_for_user", "export_all"})
334 */
335 public function is_Archived()
336 {
337 return (int) $this->isArchived();
338 }
339
340 public function toggleArchive()
341 {
342 $this->isArchived = $this->isArchived() ^ 1;
343
344 return $this;
345 }
346
347 /**
348 * Set isStarred.
349 *
350 * @param bool $isStarred
351 *
352 * @return Entry
353 */
354 public function setStarred($isStarred)
355 {
356 $this->isStarred = $isStarred;
357
358 return $this;
359 }
360
361 /**
362 * Get isStarred.
363 *
364 * @return bool
365 */
366 public function isStarred()
367 {
368 return $this->isStarred;
369 }
370
371 /**
372 * @VirtualProperty
373 * @SerializedName("is_starred")
374 * @Groups({"entries_for_user", "export_all"})
375 */
376 public function is_Starred()
377 {
378 return (int) $this->isStarred();
379 }
380
381 public function toggleStar()
382 {
383 $this->isStarred = $this->isStarred() ^ 1;
384
385 return $this;
386 }
387
388 /**
389 * Set content.
390 *
391 * @param string $content
392 *
393 * @return Entry
394 */
395 public function setContent($content)
396 {
397 $this->content = $content;
398
399 return $this;
400 }
401
402 /**
403 * Get content.
404 *
405 * @return string
406 */
407 public function getContent()
408 {
409 return $this->content;
410 }
411
412 /**
413 * @return User
414 */
415 public function getUser()
416 {
417 return $this->user;
418 }
419
420 /**
421 * @VirtualProperty
422 * @SerializedName("user_name")
423 */
424 public function getUserName()
425 {
426 return $this->user->getUserName();
427 }
428
429 /**
430 * @VirtualProperty
431 * @SerializedName("user_email")
432 */
433 public function getUserEmail()
434 {
435 return $this->user->getEmail();
436 }
437
438 /**
439 * @VirtualProperty
440 * @SerializedName("user_id")
441 */
442 public function getUserId()
443 {
444 return $this->user->getId();
445 }
446
447 /**
448 * Set created_at.
449 * Only used when importing data from an other service.
450 *
451 * @param \DateTime $createdAt
452 *
453 * @return Entry
454 */
455 public function setCreatedAt(\DateTime $createdAt)
456 {
457 $this->createdAt = $createdAt;
458
459 return $this;
460 }
461
462 /**
463 * @return \DateTime
464 */
465 public function getCreatedAt()
466 {
467 return $this->createdAt;
468 }
469
470 /**
471 * @return \DateTime
472 */
473 public function getUpdatedAt()
474 {
475 return $this->updatedAt;
476 }
477
478 /**
479 * @return ArrayCollection<Annotation>
480 */
481 public function getAnnotations()
482 {
483 return $this->annotations;
484 }
485
486 /**
487 * @param Annotation $annotation
488 */
489 public function setAnnotation(Annotation $annotation)
490 {
491 $this->annotations[] = $annotation;
492 }
493
494 /**
495 * @return string
496 */
497 public function getMimetype()
498 {
499 return $this->mimetype;
500 }
501
502 /**
503 * @param string $mimetype
504 */
505 public function setMimetype($mimetype)
506 {
507 $this->mimetype = $mimetype;
508 }
509
510 /**
511 * @return int
512 */
513 public function getReadingTime()
514 {
515 return $this->readingTime;
516 }
517
518 /**
519 * @param int $readingTime
520 */
521 public function setReadingTime($readingTime)
522 {
523 $this->readingTime = $readingTime;
524 }
525
526 /**
527 * @return string
528 */
529 public function getDomainName()
530 {
531 return $this->domainName;
532 }
533
534 /**
535 * @param string $domainName
536 */
537 public function setDomainName($domainName)
538 {
539 $this->domainName = $domainName;
540 }
541
542 /**
543 * @return ArrayCollection
544 */
545 public function getTags()
546 {
547 return $this->tags;
548 }
549
550 /**
551 * @VirtualProperty
552 * @SerializedName("tags")
553 * @Groups({"entries_for_user", "export_all"})
554 */
555 public function getSerializedTags()
556 {
557 $data = [];
558 foreach ($this->tags as $tag) {
559 $data[] = $tag->getLabel();
560 }
561
562 return $data;
563 }
564
565 /**
566 * @param Tag $tag
567 */
568 public function addTag(Tag $tag)
569 {
570 if ($this->tags->contains($tag)) {
571 return;
572 }
573
574 // check if tag already exist but has not yet be persisted
575 // it seems that the previous condition with `contains()` doesn't check that case
576 foreach ($this->tags as $existingTag) {
577 if ($existingTag->getLabel() === $tag->getLabel()) {
578 return;
579 }
580 }
581
582 $this->tags->add($tag);
583 $tag->addEntry($this);
584 }
585
586 /**
587 * Remove the given tag from the entry (if the tag is associated).
588 *
589 * @param Tag $tag
590 */
591 public function removeTag(Tag $tag)
592 {
593 if (!$this->tags->contains($tag)) {
594 return;
595 }
596
597 $this->tags->removeElement($tag);
598 $tag->removeEntry($this);
599 }
600
601 /**
602 * Remove all assigned tags from the entry.
603 */
604 public function removeAllTags()
605 {
606 foreach ($this->tags as $tag) {
607 $this->tags->removeElement($tag);
608 $tag->removeEntry($this);
609 }
610 }
611
612 /**
613 * Set previewPicture.
614 *
615 * @param string $previewPicture
616 *
617 * @return Entry
618 */
619 public function setPreviewPicture($previewPicture)
620 {
621 $this->previewPicture = $previewPicture;
622
623 return $this;
624 }
625
626 /**
627 * Get previewPicture.
628 *
629 * @return string
630 */
631 public function getPreviewPicture()
632 {
633 return $this->previewPicture;
634 }
635
636 /**
637 * Set language.
638 *
639 * @param string $language
640 *
641 * @return Entry
642 */
643 public function setLanguage($language)
644 {
645 $this->language = $language;
646
647 return $this;
648 }
649
650 /**
651 * Get language.
652 *
653 * @return string
654 */
655 public function getLanguage()
656 {
657 return $this->language;
658 }
659
660 /**
661 * @return string
662 */
663 public function getUid()
664 {
665 return $this->uid;
666 }
667
668 /**
669 * @param string $uid
670 *
671 * @return Entry
672 */
673 public function setUid($uid)
674 {
675 $this->uid = $uid;
676
677 return $this;
678 }
679
680 public function generateUid()
681 {
682 if (null === $this->uid) {
683 // @see http://blog.kevingomez.fr/til/2015/07/26/why-is-uniqid-slow/ for true parameter
684 $this->uid = uniqid('', true);
685 }
686 }
687
688 public function cleanUid()
689 {
690 $this->uid = null;
691 }
692
693 /**
694 * Used in the entries filter so it's more explicit for the end user than the uid.
695 * Also used in the API.
696 *
697 * @VirtualProperty
698 * @SerializedName("is_public")
699 * @Groups({"entries_for_user"})
700 *
701 * @return bool
702 */
703 public function isPublic()
704 {
705 return null !== $this->uid;
706 }
707
708 /**
709 * @return string
710 */
711 public function getHttpStatus()
712 {
713 return $this->httpStatus;
714 }
715
716 /**
717 * @param string $httpStatus
718 *
719 * @return Entry
720 */
721 public function setHttpStatus($httpStatus)
722 {
723 $this->httpStatus = $httpStatus;
724
725 return $this;
726 }
727
728 /**
729 * @return \Datetime
730 */
731 public function getPublishedAt()
732 {
733 return $this->publishedAt;
734 }
735
736 /**
737 * @param \Datetime $publishedAt
738 *
739 * @return Entry
740 */
741 public function setPublishedAt(\Datetime $publishedAt)
742 {
743 $this->publishedAt = $publishedAt;
744
745 return $this;
746 }
747
748 /**
749 * @return array
750 */
751 public function getPublishedBy()
752 {
753 return $this->publishedBy;
754 }
755
756 /**
757 * @param array $publishedBy
758 *
759 * @return Entry
760 */
761 public function setPublishedBy($publishedBy)
762 {
763 $this->publishedBy = $publishedBy;
764
765 return $this;
766 }
767
768 /**
769 * @return array
770 */
771 public function getHeaders()
772 {
773 return $this->headers;
774 }
775
776 /**
777 * @param array $headers
778 *
779 * @return Entry
780 */
781 public function setHeaders($headers)
782 {
783 $this->headers = $headers;
784
785 return $this;
786 }
787 }