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