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