]> git.immae.eu Git - github/wallabag/wallabag.git/blame_incremental - src/Wallabag/CoreBundle/Entity/Entry.php
Merge remote-tracking branch 'origin/master' into 2.3
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Entity / Entry.php
... / ...
CommitLineData
1<?php
2
3namespace Wallabag\CoreBundle\Entity;
4
5use Doctrine\Common\Collections\ArrayCollection;
6use Doctrine\ORM\Mapping as ORM;
7use Hateoas\Configuration\Annotation as Hateoas;
8use JMS\Serializer\Annotation\Groups;
9use JMS\Serializer\Annotation\XmlRoot;
10use JMS\Serializer\Annotation\Exclude;
11use JMS\Serializer\Annotation\VirtualProperty;
12use JMS\Serializer\Annotation\SerializedName;
13use Symfony\Component\Validator\Constraints as Assert;
14use Wallabag\UserBundle\Entity\User;
15use 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 */
33class 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="json_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 * @Exclude
206 *
207 * @ORM\ManyToOne(targetEntity="Wallabag\UserBundle\Entity\User", inversedBy="entries")
208 *
209 * @Groups({"export_all"})
210 */
211 private $user;
212
213 /**
214 * @ORM\ManyToMany(targetEntity="Tag", inversedBy="entries", cascade={"persist"})
215 * @ORM\JoinTable(
216 * name="entry_tag",
217 * joinColumns={
218 * @ORM\JoinColumn(name="entry_id", referencedColumnName="id", onDelete="cascade")
219 * },
220 * inverseJoinColumns={
221 * @ORM\JoinColumn(name="tag_id", referencedColumnName="id", onDelete="cascade")
222 * }
223 * )
224 */
225 private $tags;
226
227 /*
228 * @param User $user
229 */
230 public function __construct(User $user)
231 {
232 $this->user = $user;
233 $this->tags = new ArrayCollection();
234 }
235
236 /**
237 * Get id.
238 *
239 * @return int
240 */
241 public function getId()
242 {
243 return $this->id;
244 }
245
246 /**
247 * Set title.
248 *
249 * @param string $title
250 *
251 * @return Entry
252 */
253 public function setTitle($title)
254 {
255 $this->title = $title;
256
257 return $this;
258 }
259
260 /**
261 * Get title.
262 *
263 * @return string
264 */
265 public function getTitle()
266 {
267 return $this->title;
268 }
269
270 /**
271 * Set url.
272 *
273 * @param string $url
274 *
275 * @return Entry
276 */
277 public function setUrl($url)
278 {
279 $this->url = $url;
280
281 return $this;
282 }
283
284 /**
285 * Get url.
286 *
287 * @return string
288 */
289 public function getUrl()
290 {
291 return $this->url;
292 }
293
294 /**
295 * Set isArchived.
296 *
297 * @param bool $isArchived
298 *
299 * @return Entry
300 */
301 public function setArchived($isArchived)
302 {
303 $this->isArchived = $isArchived;
304
305 return $this;
306 }
307
308 /**
309 * Get isArchived.
310 *
311 * @return bool
312 */
313 public function isArchived()
314 {
315 return $this->isArchived;
316 }
317
318 /**
319 * @VirtualProperty
320 * @SerializedName("is_archived")
321 * @Groups({"entries_for_user", "export_all"})
322 */
323 public function is_Archived()
324 {
325 return (int) $this->isArchived();
326 }
327
328 public function toggleArchive()
329 {
330 $this->isArchived = $this->isArchived() ^ 1;
331
332 return $this;
333 }
334
335 /**
336 * Set isStarred.
337 *
338 * @param bool $isStarred
339 *
340 * @return Entry
341 */
342 public function setStarred($isStarred)
343 {
344 $this->isStarred = $isStarred;
345
346 return $this;
347 }
348
349 /**
350 * Get isStarred.
351 *
352 * @return bool
353 */
354 public function isStarred()
355 {
356 return $this->isStarred;
357 }
358
359 /**
360 * @VirtualProperty
361 * @SerializedName("is_starred")
362 * @Groups({"entries_for_user", "export_all"})
363 */
364 public function is_Starred()
365 {
366 return (int) $this->isStarred();
367 }
368
369 public function toggleStar()
370 {
371 $this->isStarred = $this->isStarred() ^ 1;
372
373 return $this;
374 }
375
376 /**
377 * Set content.
378 *
379 * @param string $content
380 *
381 * @return Entry
382 */
383 public function setContent($content)
384 {
385 $this->content = $content;
386
387 return $this;
388 }
389
390 /**
391 * Get content.
392 *
393 * @return string
394 */
395 public function getContent()
396 {
397 return $this->content;
398 }
399
400 /**
401 * @return User
402 */
403 public function getUser()
404 {
405 return $this->user;
406 }
407
408 /**
409 * @VirtualProperty
410 * @SerializedName("user_name")
411 */
412 public function getUserName()
413 {
414 return $this->user->getUserName();
415 }
416
417 /**
418 * @VirtualProperty
419 * @SerializedName("user_email")
420 */
421 public function getUserEmail()
422 {
423 return $this->user->getEmail();
424 }
425
426 /**
427 * @VirtualProperty
428 * @SerializedName("user_id")
429 */
430 public function getUserId()
431 {
432 return $this->user->getId();
433 }
434
435 /**
436 * Set created_at.
437 * Only used when importing data from an other service.
438 *
439 * @param \DateTime $createdAt
440 *
441 * @return Entry
442 */
443 public function setCreatedAt(\DateTime $createdAt)
444 {
445 $this->createdAt = $createdAt;
446
447 return $this;
448 }
449
450 /**
451 * @return \DateTime
452 */
453 public function getCreatedAt()
454 {
455 return $this->createdAt;
456 }
457
458 /**
459 * @return \DateTime
460 */
461 public function getUpdatedAt()
462 {
463 return $this->updatedAt;
464 }
465
466 /**
467 * @ORM\PrePersist
468 * @ORM\PreUpdate
469 */
470 public function timestamps()
471 {
472 if (is_null($this->createdAt)) {
473 $this->createdAt = new \DateTime();
474 }
475
476 $this->updatedAt = new \DateTime();
477 }
478
479 /**
480 * @return ArrayCollection<Annotation>
481 */
482 public function getAnnotations()
483 {
484 return $this->annotations;
485 }
486
487 /**
488 * @param Annotation $annotation
489 */
490 public function setAnnotation(Annotation $annotation)
491 {
492 $this->annotations[] = $annotation;
493 }
494
495 /**
496 * @return string
497 */
498 public function getMimetype()
499 {
500 return $this->mimetype;
501 }
502
503 /**
504 * @param string $mimetype
505 */
506 public function setMimetype($mimetype)
507 {
508 $this->mimetype = $mimetype;
509 }
510
511 /**
512 * @return int
513 */
514 public function getReadingTime()
515 {
516 return $this->readingTime;
517 }
518
519 /**
520 * @param int $readingTime
521 */
522 public function setReadingTime($readingTime)
523 {
524 $this->readingTime = $readingTime;
525 }
526
527 /**
528 * @return string
529 */
530 public function getDomainName()
531 {
532 return $this->domainName;
533 }
534
535 /**
536 * @param string $domainName
537 */
538 public function setDomainName($domainName)
539 {
540 $this->domainName = $domainName;
541 }
542
543 /**
544 * @return ArrayCollection<Tag>
545 */
546 public function getTags()
547 {
548 return $this->tags;
549 }
550
551 /**
552 * @VirtualProperty
553 * @SerializedName("tags")
554 * @Groups({"entries_for_user", "export_all"})
555 */
556 public function getSerializedTags()
557 {
558 $data = [];
559 foreach ($this->tags as $tag) {
560 $data[] = $tag->getLabel();
561 }
562
563 return $data;
564 }
565
566 /**
567 * @param Tag $tag
568 */
569 public function addTag(Tag $tag)
570 {
571 if ($this->tags->contains($tag)) {
572 return;
573 }
574
575 // check if tag already exist but has not yet be persisted
576 // it seems that the previous condition with `contains()` doesn't check that case
577 foreach ($this->tags as $existingTag) {
578 if ($existingTag->getLabel() === $tag->getLabel()) {
579 return;
580 }
581 }
582
583 $this->tags->add($tag);
584 $tag->addEntry($this);
585 }
586
587 public function removeTag(Tag $tag)
588 {
589 if (!$this->tags->contains($tag)) {
590 return;
591 }
592
593 $this->tags->removeElement($tag);
594 $tag->removeEntry($this);
595 }
596
597 /**
598 * Set previewPicture.
599 *
600 * @param string $previewPicture
601 *
602 * @return Entry
603 */
604 public function setPreviewPicture($previewPicture)
605 {
606 $this->previewPicture = $previewPicture;
607
608 return $this;
609 }
610
611 /**
612 * Get previewPicture.
613 *
614 * @return string
615 */
616 public function getPreviewPicture()
617 {
618 return $this->previewPicture;
619 }
620
621 /**
622 * Set language.
623 *
624 * @param string $language
625 *
626 * @return Entry
627 */
628 public function setLanguage($language)
629 {
630 $this->language = $language;
631
632 return $this;
633 }
634
635 /**
636 * Get language.
637 *
638 * @return string
639 */
640 public function getLanguage()
641 {
642 return $this->language;
643 }
644
645 /**
646 * @return string
647 */
648 public function getUid()
649 {
650 return $this->uid;
651 }
652
653 /**
654 * @param string $uid
655 *
656 * @return Entry
657 */
658 public function setUid($uid)
659 {
660 $this->uid = $uid;
661
662 return $this;
663 }
664
665 public function generateUid()
666 {
667 if (null === $this->uid) {
668 // @see http://blog.kevingomez.fr/til/2015/07/26/why-is-uniqid-slow/ for true parameter
669 $this->uid = uniqid('', true);
670 }
671 }
672
673 public function cleanUid()
674 {
675 $this->uid = null;
676 }
677
678 /**
679 * @return int
680 */
681 public function getHttpStatus()
682 {
683 return $this->httpStatus;
684 }
685
686 /**
687 * @param int $httpStatus
688 *
689 * @return Entry
690 */
691 public function setHttpStatus($httpStatus)
692 {
693 $this->httpStatus = $httpStatus;
694
695 return $this;
696 }
697
698 /**
699 * @return \Datetime
700 */
701 public function getPublishedAt()
702 {
703 return $this->publishedAt;
704 }
705
706 /**
707 * @param \Datetime $publishedAt
708 *
709 * @return Entry
710 */
711 public function setPublishedAt(\Datetime $publishedAt)
712 {
713 $this->publishedAt = $publishedAt;
714
715 return $this;
716 }
717
718 /**
719 * @return string
720 */
721 public function getPublishedBy()
722 {
723 return $this->publishedBy;
724 }
725
726 /**
727 * @param string $publishedBy
728 *
729 * @return Entry
730 */
731 public function setPublishedBy($publishedBy)
732 {
733 $this->publishedBy = $publishedBy;
734
735 return $this;
736 }
737}