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