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