]>
Commit | Line | Data |
---|---|---|
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 | * @Exclude | |
215 | * | |
216 | * @ORM\ManyToOne(targetEntity="Wallabag\UserBundle\Entity\User", inversedBy="entries") | |
217 | * | |
218 | * @Groups({"export_all"}) | |
219 | */ | |
220 | private $user; | |
221 | ||
222 | /** | |
223 | * @ORM\ManyToMany(targetEntity="Tag", inversedBy="entries", cascade={"persist"}) | |
224 | * @ORM\JoinTable( | |
225 | * name="entry_tag", | |
226 | * joinColumns={ | |
227 | * @ORM\JoinColumn(name="entry_id", referencedColumnName="id", onDelete="cascade") | |
228 | * }, | |
229 | * inverseJoinColumns={ | |
230 | * @ORM\JoinColumn(name="tag_id", referencedColumnName="id", onDelete="cascade") | |
231 | * } | |
232 | * ) | |
233 | */ | |
234 | private $tags; | |
235 | ||
236 | /* | |
237 | * @param User $user | |
238 | */ | |
239 | public function __construct(User $user) | |
240 | { | |
241 | $this->user = $user; | |
242 | $this->tags = new ArrayCollection(); | |
243 | } | |
244 | ||
245 | /** | |
246 | * Get id. | |
247 | * | |
248 | * @return int | |
249 | */ | |
250 | public function getId() | |
251 | { | |
252 | return $this->id; | |
253 | } | |
254 | ||
255 | /** | |
256 | * Set title. | |
257 | * | |
258 | * @param string $title | |
259 | * | |
260 | * @return Entry | |
261 | */ | |
262 | public function setTitle($title) | |
263 | { | |
264 | $this->title = $title; | |
265 | ||
266 | return $this; | |
267 | } | |
268 | ||
269 | /** | |
270 | * Get title. | |
271 | * | |
272 | * @return string | |
273 | */ | |
274 | public function getTitle() | |
275 | { | |
276 | return $this->title; | |
277 | } | |
278 | ||
279 | /** | |
280 | * Set url. | |
281 | * | |
282 | * @param string $url | |
283 | * | |
284 | * @return Entry | |
285 | */ | |
286 | public function setUrl($url) | |
287 | { | |
288 | $this->url = $url; | |
289 | ||
290 | return $this; | |
291 | } | |
292 | ||
293 | /** | |
294 | * Get url. | |
295 | * | |
296 | * @return string | |
297 | */ | |
298 | public function getUrl() | |
299 | { | |
300 | return $this->url; | |
301 | } | |
302 | ||
303 | /** | |
304 | * Set isArchived. | |
305 | * | |
306 | * @param bool $isArchived | |
307 | * | |
308 | * @return Entry | |
309 | */ | |
310 | public function setArchived($isArchived) | |
311 | { | |
312 | $this->isArchived = $isArchived; | |
313 | ||
314 | return $this; | |
315 | } | |
316 | ||
317 | /** | |
318 | * Get isArchived. | |
319 | * | |
320 | * @return bool | |
321 | */ | |
322 | public function isArchived() | |
323 | { | |
324 | return $this->isArchived; | |
325 | } | |
326 | ||
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 | ||
337 | public function toggleArchive() | |
338 | { | |
339 | $this->isArchived = $this->isArchived() ^ 1; | |
340 | ||
341 | return $this; | |
342 | } | |
343 | ||
344 | /** | |
345 | * Set isStarred. | |
346 | * | |
347 | * @param bool $isStarred | |
348 | * | |
349 | * @return Entry | |
350 | */ | |
351 | public function setStarred($isStarred) | |
352 | { | |
353 | $this->isStarred = $isStarred; | |
354 | ||
355 | return $this; | |
356 | } | |
357 | ||
358 | /** | |
359 | * Get isStarred. | |
360 | * | |
361 | * @return bool | |
362 | */ | |
363 | public function isStarred() | |
364 | { | |
365 | return $this->isStarred; | |
366 | } | |
367 | ||
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 | ||
378 | public function toggleStar() | |
379 | { | |
380 | $this->isStarred = $this->isStarred() ^ 1; | |
381 | ||
382 | return $this; | |
383 | } | |
384 | ||
385 | /** | |
386 | * Set content. | |
387 | * | |
388 | * @param string $content | |
389 | * | |
390 | * @return Entry | |
391 | */ | |
392 | public function setContent($content) | |
393 | { | |
394 | $this->content = $content; | |
395 | ||
396 | return $this; | |
397 | } | |
398 | ||
399 | /** | |
400 | * Get content. | |
401 | * | |
402 | * @return string | |
403 | */ | |
404 | public function getContent() | |
405 | { | |
406 | return $this->content; | |
407 | } | |
408 | ||
409 | /** | |
410 | * @return User | |
411 | */ | |
412 | public function getUser() | |
413 | { | |
414 | return $this->user; | |
415 | } | |
416 | ||
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 | ||
444 | /** | |
445 | * Set created_at. | |
446 | * Only used when importing data from an other service. | |
447 | * | |
448 | * @param \DateTime $createdAt | |
449 | * | |
450 | * @return Entry | |
451 | */ | |
452 | public function setCreatedAt(\DateTime $createdAt) | |
453 | { | |
454 | $this->createdAt = $createdAt; | |
455 | ||
456 | return $this; | |
457 | } | |
458 | ||
459 | /** | |
460 | * @return \DateTime | |
461 | */ | |
462 | public function getCreatedAt() | |
463 | { | |
464 | return $this->createdAt; | |
465 | } | |
466 | ||
467 | /** | |
468 | * @return \DateTime | |
469 | */ | |
470 | public function getUpdatedAt() | |
471 | { | |
472 | return $this->updatedAt; | |
473 | } | |
474 | ||
475 | /** | |
476 | * @ORM\PrePersist | |
477 | * @ORM\PreUpdate | |
478 | */ | |
479 | public function timestamps() | |
480 | { | |
481 | if (is_null($this->createdAt)) { | |
482 | $this->createdAt = new \DateTime(); | |
483 | } | |
484 | ||
485 | $this->updatedAt = new \DateTime(); | |
486 | } | |
487 | ||
488 | /** | |
489 | * @return ArrayCollection<Annotation> | |
490 | */ | |
491 | public function getAnnotations() | |
492 | { | |
493 | return $this->annotations; | |
494 | } | |
495 | ||
496 | /** | |
497 | * @param Annotation $annotation | |
498 | */ | |
499 | public function setAnnotation(Annotation $annotation) | |
500 | { | |
501 | $this->annotations[] = $annotation; | |
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 | ||
552 | /** | |
553 | * @return ArrayCollection | |
554 | */ | |
555 | public function getTags() | |
556 | { | |
557 | return $this->tags; | |
558 | } | |
559 | ||
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 | ||
575 | /** | |
576 | * @param Tag $tag | |
577 | */ | |
578 | public function addTag(Tag $tag) | |
579 | { | |
580 | if ($this->tags->contains($tag)) { | |
581 | return; | |
582 | } | |
583 | ||
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) { | |
587 | if ($existingTag->getLabel() === $tag->getLabel()) { | |
588 | return; | |
589 | } | |
590 | } | |
591 | ||
592 | $this->tags->add($tag); | |
593 | $tag->addEntry($this); | |
594 | } | |
595 | ||
596 | public function removeTag(Tag $tag) | |
597 | { | |
598 | if (!$this->tags->contains($tag)) { | |
599 | return; | |
600 | } | |
601 | ||
602 | $this->tags->removeElement($tag); | |
603 | $tag->removeEntry($this); | |
604 | } | |
605 | ||
606 | /** | |
607 | * Set previewPicture. | |
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 | /** | |
621 | * Get previewPicture. | |
622 | * | |
623 | * @return string | |
624 | */ | |
625 | public function getPreviewPicture() | |
626 | { | |
627 | return $this->previewPicture; | |
628 | } | |
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 | } | |
653 | ||
654 | /** | |
655 | * @return string | |
656 | */ | |
657 | public function getUid() | |
658 | { | |
659 | return $this->uid; | |
660 | } | |
661 | ||
662 | /** | |
663 | * @param string $uid | |
664 | * | |
665 | * @return Entry | |
666 | */ | |
667 | public function setUid($uid) | |
668 | { | |
669 | $this->uid = $uid; | |
670 | ||
671 | return $this; | |
672 | } | |
673 | ||
674 | public function generateUid() | |
675 | { | |
676 | if (null === $this->uid) { | |
677 | // @see http://blog.kevingomez.fr/til/2015/07/26/why-is-uniqid-slow/ for true parameter | |
678 | $this->uid = uniqid('', true); | |
679 | } | |
680 | } | |
681 | ||
682 | public function cleanUid() | |
683 | { | |
684 | $this->uid = null; | |
685 | } | |
686 | ||
687 | /** | |
688 | * @return string | |
689 | */ | |
690 | public function getHttpStatus() | |
691 | { | |
692 | return $this->httpStatus; | |
693 | } | |
694 | ||
695 | /** | |
696 | * @param string $httpStatus | |
697 | * | |
698 | * @return Entry | |
699 | */ | |
700 | public function setHttpStatus($httpStatus) | |
701 | { | |
702 | $this->httpStatus = $httpStatus; | |
703 | ||
704 | return $this; | |
705 | } | |
706 | ||
707 | /** | |
708 | * @return \Datetime | |
709 | */ | |
710 | public function getPublishedAt() | |
711 | { | |
712 | return $this->publishedAt; | |
713 | } | |
714 | ||
715 | /** | |
716 | * @param \Datetime $publishedAt | |
717 | * | |
718 | * @return Entry | |
719 | */ | |
720 | public function setPublishedAt(\Datetime $publishedAt) | |
721 | { | |
722 | $this->publishedAt = $publishedAt; | |
723 | ||
724 | return $this; | |
725 | } | |
726 | ||
727 | /** | |
728 | * @return array | |
729 | */ | |
730 | public function getPublishedBy() | |
731 | { | |
732 | return $this->publishedBy; | |
733 | } | |
734 | ||
735 | /** | |
736 | * @param array $publishedBy | |
737 | * | |
738 | * @return Entry | |
739 | */ | |
740 | public function setPublishedBy($publishedBy) | |
741 | { | |
742 | $this->publishedBy = $publishedBy; | |
743 | ||
744 | return $this; | |
745 | } | |
746 | ||
747 | /** | |
748 | * @return array | |
749 | */ | |
750 | public function getHeaders() | |
751 | { | |
752 | return $this->headers; | |
753 | } | |
754 | ||
755 | /** | |
756 | * @param array $headers | |
757 | * | |
758 | * @return Entry | |
759 | */ | |
760 | public function setHeaders($headers) | |
761 | { | |
762 | $this->headers = $headers; | |
763 | ||
764 | return $this; | |
765 | } | |
766 | } |