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