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