]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Entity/Entry.php
Merge pull request #2593 from wallabag/test-clarify
[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"},
25 * indexes={@ORM\Index(name="created_at", columns={"created_at"})}
26 * )
34d15eb4 27 * @ORM\HasLifecycleCallbacks()
0f006880 28 * @Hateoas\Relation("self", href = "expr('/api/entries/' ~ object.getId())")
9d50517c 29 */
be463487 30class Entry
9d50517c 31{
0f006880 32 /** @Serializer\XmlAttribute */
9d50517c 33 /**
4346a860 34 * @var int
9d50517c 35 *
be463487 36 * @ORM\Column(name="id", type="integer")
9d50517c 37 * @ORM\Id
34d15eb4 38 * @ORM\GeneratedValue(strategy="AUTO")
5b7da076
TC
39 *
40 * @Groups({"entries_for_user", "export_all"})
9d50517c 41 */
be463487 42 private $id;
9d50517c 43
f3d0cb91 44 /**
78b3c31d 45 * @var string
f3d0cb91
NL
46 *
47 * @ORM\Column(name="uuid", type="text", nullable=true)
48 *
49 * @Groups({"entries_for_user", "export_all"})
50 */
51 private $uuid;
52
9d50517c
NL
53 /**
54 * @var string
55 *
56 * @ORM\Column(name="title", type="text", nullable=true)
5b7da076
TC
57 *
58 * @Groups({"entries_for_user", "export_all"})
9d50517c
NL
59 */
60 private $title;
61
62 /**
63 * @var string
64 *
b84a8055 65 * @Assert\NotBlank()
9d50517c 66 * @ORM\Column(name="url", type="text", nullable=true)
5b7da076
TC
67 *
68 * @Groups({"entries_for_user", "export_all"})
9d50517c
NL
69 */
70 private $url;
71
72 /**
4346a860 73 * @var bool
9d50517c 74 *
189ef634
TC
75 * @Exclude
76 *
be463487 77 * @ORM\Column(name="is_archived", type="boolean")
5b7da076
TC
78 *
79 * @Groups({"entries_for_user", "export_all"})
9d50517c 80 */
905ae369 81 private $isArchived = false;
9d50517c
NL
82
83 /**
4346a860 84 * @var bool
9d50517c 85 *
189ef634
TC
86 * @Exclude
87 *
be463487 88 * @ORM\Column(name="is_starred", type="boolean")
5b7da076
TC
89 *
90 * @Groups({"entries_for_user", "export_all"})
9d50517c 91 */
905ae369 92 private $isStarred = false;
9d50517c 93
9d50517c
NL
94 /**
95 * @var string
96 *
eacaf7f8 97 * @ORM\Column(name="content", type="text", nullable=true)
5b7da076
TC
98 *
99 * @Groups({"entries_for_user", "export_all"})
9d50517c 100 */
eacaf7f8 101 private $content;
9d50517c 102
34d15eb4 103 /**
8664069e 104 * @var \DateTime
34d15eb4 105 *
be463487 106 * @ORM\Column(name="created_at", type="datetime")
5b7da076 107 *
9401696f 108 * @Groups({"entries_for_user", "export_all"})
34d15eb4
NL
109 */
110 private $createdAt;
111
112 /**
8664069e 113 * @var \DateTime
34d15eb4 114 *
be463487 115 * @ORM\Column(name="updated_at", type="datetime")
5b7da076 116 *
9401696f 117 * @Groups({"entries_for_user", "export_all"})
34d15eb4
NL
118 */
119 private $updatedAt;
120
34d15eb4 121 /**
4dc87223 122 * @ORM\OneToMany(targetEntity="Wallabag\AnnotationBundle\Entity\Annotation", mappedBy="entry", cascade={"persist", "remove"})
f38e03dc 123 * @ORM\JoinTable
5b7da076 124 *
f38e03dc 125 * @Groups({"entries_for_user", "export_all"})
34d15eb4 126 */
4dc87223 127 private $annotations;
34d15eb4
NL
128
129 /**
130 * @var string
131 *
132 * @ORM\Column(name="mimetype", type="text", nullable=true)
5b7da076
TC
133 *
134 * @Groups({"entries_for_user", "export_all"})
34d15eb4
NL
135 */
136 private $mimetype;
137
98f0929f
JB
138 /**
139 * @var string
140 *
141 * @ORM\Column(name="language", type="text", nullable=true)
5b7da076
TC
142 *
143 * @Groups({"entries_for_user", "export_all"})
98f0929f
JB
144 */
145 private $language;
146
34d15eb4 147 /**
4346a860 148 * @var int
34d15eb4 149 *
26864574 150 * @ORM\Column(name="reading_time", type="integer", nullable=true)
5b7da076
TC
151 *
152 * @Groups({"entries_for_user", "export_all"})
34d15eb4
NL
153 */
154 private $readingTime;
155
156 /**
157 * @var string
158 *
159 * @ORM\Column(name="domain_name", type="text", nullable=true)
5b7da076
TC
160 *
161 * @Groups({"entries_for_user", "export_all"})
34d15eb4
NL
162 */
163 private $domainName;
164
fad31615
JB
165 /**
166 * @var string
167 *
168 * @ORM\Column(name="preview_picture", type="text", nullable=true)
5b7da076
TC
169 *
170 * @Groups({"entries_for_user", "export_all"})
fad31615
JB
171 */
172 private $previewPicture;
173
34d15eb4 174 /**
4346a860 175 * @var bool
34d15eb4
NL
176 *
177 * @ORM\Column(name="is_public", type="boolean", nullable=true, options={"default" = false})
5b7da076
TC
178 *
179 * @Groups({"export_all"})
34d15eb4
NL
180 */
181 private $isPublic;
182
5f09650e 183 /**
7d1fdab2
TC
184 * @Exclude
185 *
1210dae1 186 * @ORM\ManyToOne(targetEntity="Wallabag\UserBundle\Entity\User", inversedBy="entries")
5b7da076
TC
187 *
188 * @Groups({"export_all"})
5f09650e
NL
189 */
190 private $user;
191
0a018fe0 192 /**
af95c09c 193 * @ORM\ManyToMany(targetEntity="Tag", inversedBy="entries", cascade={"persist"})
e42b13bc
JB
194 * @ORM\JoinTable(
195 * name="entry_tag",
196 * joinColumns={
206bade5 197 * @ORM\JoinColumn(name="entry_id", referencedColumnName="id", onDelete="cascade")
e42b13bc
JB
198 * },
199 * inverseJoinColumns={
206bade5 200 * @ORM\JoinColumn(name="tag_id", referencedColumnName="id", onDelete="cascade")
e42b13bc
JB
201 * }
202 * )
0a018fe0
NL
203 */
204 private $tags;
205
5f09650e
NL
206 /*
207 * @param User $user
208 */
5d6f6f56 209 public function __construct(User $user)
5f09650e
NL
210 {
211 $this->user = $user;
0a018fe0 212 $this->tags = new ArrayCollection();
5f09650e
NL
213 }
214
9d50517c 215 /**
4346a860 216 * Get id.
9d50517c 217 *
4346a860 218 * @return int
9d50517c
NL
219 */
220 public function getId()
221 {
222 return $this->id;
223 }
224
225 /**
4346a860
JB
226 * Set title.
227 *
228 * @param string $title
9d50517c 229 *
be463487 230 * @return Entry
9d50517c
NL
231 */
232 public function setTitle($title)
233 {
234 $this->title = $title;
235
236 return $this;
237 }
238
239 /**
4346a860 240 * Get title.
9d50517c 241 *
7df80cb3 242 * @return string
9d50517c
NL
243 */
244 public function getTitle()
245 {
246 return $this->title;
247 }
248
249 /**
4346a860
JB
250 * Set url.
251 *
252 * @param string $url
9d50517c 253 *
be463487 254 * @return Entry
9d50517c
NL
255 */
256 public function setUrl($url)
257 {
258 $this->url = $url;
259
260 return $this;
261 }
262
263 /**
4346a860 264 * Get url.
9d50517c 265 *
7df80cb3 266 * @return string
9d50517c
NL
267 */
268 public function getUrl()
269 {
270 return $this->url;
271 }
272
273 /**
4346a860
JB
274 * Set isArchived.
275 *
8eedc8cf 276 * @param bool $isArchived
9d50517c 277 *
be463487 278 * @return Entry
9d50517c 279 */
905ae369 280 public function setArchived($isArchived)
9d50517c 281 {
905ae369 282 $this->isArchived = $isArchived;
9d50517c
NL
283
284 return $this;
285 }
286
287 /**
4346a860 288 * Get isArchived.
9d50517c 289 *
8eedc8cf 290 * @return bool
9d50517c 291 */
905ae369 292 public function isArchived()
9d50517c 293 {
905ae369 294 return $this->isArchived;
9d50517c
NL
295 }
296
189ef634
TC
297 /**
298 * @VirtualProperty
299 * @SerializedName("is_archived")
300 * @Groups({"entries_for_user", "export_all"})
301 */
302 public function is_Archived()
303 {
304 return (int) $this->isArchived();
305 }
306
163eae0b
NL
307 public function toggleArchive()
308 {
905ae369 309 $this->isArchived = $this->isArchived() ^ 1;
7df80cb3 310
163eae0b
NL
311 return $this;
312 }
313
9d50517c 314 /**
4346a860
JB
315 * Set isStarred.
316 *
8eedc8cf 317 * @param bool $isStarred
9d50517c 318 *
be463487 319 * @return Entry
9d50517c 320 */
905ae369 321 public function setStarred($isStarred)
9d50517c 322 {
905ae369 323 $this->isStarred = $isStarred;
9d50517c
NL
324
325 return $this;
326 }
327
328 /**
4346a860 329 * Get isStarred.
9d50517c 330 *
8eedc8cf 331 * @return bool
9d50517c 332 */
905ae369 333 public function isStarred()
9d50517c 334 {
905ae369 335 return $this->isStarred;
9d50517c
NL
336 }
337
189ef634
TC
338 /**
339 * @VirtualProperty
340 * @SerializedName("is_starred")
341 * @Groups({"entries_for_user", "export_all"})
342 */
343 public function is_Starred()
344 {
345 return (int) $this->isStarred();
346 }
347
163eae0b
NL
348 public function toggleStar()
349 {
905ae369 350 $this->isStarred = $this->isStarred() ^ 1;
163eae0b
NL
351
352 return $this;
353 }
354
9d50517c 355 /**
4346a860
JB
356 * Set content.
357 *
358 * @param string $content
9d50517c 359 *
be463487 360 * @return Entry
9d50517c
NL
361 */
362 public function setContent($content)
363 {
364 $this->content = $content;
365
366 return $this;
367 }
368
369 /**
4346a860 370 * Get content.
9d50517c 371 *
7df80cb3 372 * @return string
9d50517c
NL
373 */
374 public function getContent()
375 {
376 return $this->content;
377 }
378
379 /**
5f09650e 380 * @return User
9d50517c 381 */
5f09650e 382 public function getUser()
9d50517c 383 {
5f09650e 384 return $this->user;
9d50517c 385 }
42a90646 386
7d1fdab2
TC
387 /**
388 * @VirtualProperty
389 * @SerializedName("user_name")
390 */
391 public function getUserName()
392 {
393 return $this->user->getUserName();
394 }
395
396 /**
397 * @VirtualProperty
398 * @SerializedName("user_email")
399 */
400 public function getUserEmail()
401 {
402 return $this->user->getEmail();
403 }
404
405 /**
406 * @VirtualProperty
407 * @SerializedName("user_id")
408 */
409 public function getUserId()
410 {
411 return $this->user->getId();
412 }
413
34d15eb4 414 /**
6d65c0a8
JB
415 * Set created_at.
416 * Only used when importing data from an other service.
417 *
8664069e 418 * @param \DateTime $createdAt
6d65c0a8
JB
419 *
420 * @return Entry
421 */
422 public function setCreatedAt(\DateTime $createdAt)
423 {
424 $this->createdAt = $createdAt;
425
426 return $this;
427 }
428
429 /**
8664069e 430 * @return \DateTime
34d15eb4
NL
431 */
432 public function getCreatedAt()
433 {
434 return $this->createdAt;
435 }
436
34d15eb4 437 /**
8664069e 438 * @return \DateTime
34d15eb4
NL
439 */
440 public function getUpdatedAt()
441 {
442 return $this->updatedAt;
443 }
444
445 /**
be463487 446 * @ORM\PrePersist
34d15eb4
NL
447 * @ORM\PreUpdate
448 */
be463487 449 public function timestamps()
34d15eb4 450 {
be463487
NL
451 if (is_null($this->createdAt)) {
452 $this->createdAt = new \DateTime();
453 }
454
34d15eb4
NL
455 $this->updatedAt = new \DateTime();
456 }
457
458 /**
4dc87223 459 * @return ArrayCollection<Annotation>
34d15eb4 460 */
4dc87223 461 public function getAnnotations()
34d15eb4 462 {
4dc87223 463 return $this->annotations;
34d15eb4
NL
464 }
465
466 /**
4dc87223 467 * @param Annotation $annotation
34d15eb4 468 */
4dc87223 469 public function setAnnotation(Annotation $annotation)
34d15eb4 470 {
4dc87223 471 $this->annotations[] = $annotation;
34d15eb4
NL
472 }
473
474 /**
475 * @return string
476 */
477 public function getMimetype()
478 {
479 return $this->mimetype;
480 }
481
482 /**
483 * @param string $mimetype
484 */
485 public function setMimetype($mimetype)
486 {
487 $this->mimetype = $mimetype;
488 }
489
490 /**
491 * @return int
492 */
493 public function getReadingTime()
494 {
495 return $this->readingTime;
496 }
497
498 /**
499 * @param int $readingTime
500 */
501 public function setReadingTime($readingTime)
502 {
503 $this->readingTime = $readingTime;
504 }
505
506 /**
507 * @return string
508 */
509 public function getDomainName()
510 {
511 return $this->domainName;
512 }
513
514 /**
515 * @param string $domainName
516 */
517 public function setDomainName($domainName)
518 {
519 $this->domainName = $domainName;
520 }
521
522 /**
4346a860 523 * @return bool
34d15eb4 524 */
2c093b03 525 public function isPublic()
34d15eb4
NL
526 {
527 return $this->isPublic;
528 }
529
530 /**
4346a860 531 * @param bool $isPublic
34d15eb4 532 */
82d6d9cb 533 public function setIsPublic($isPublic)
34d15eb4
NL
534 {
535 $this->isPublic = $isPublic;
536 }
0a018fe0
NL
537
538 /**
539 * @return ArrayCollection<Tag>
540 */
541 public function getTags()
542 {
543 return $this->tags;
544 }
545
b0458874
JB
546 /**
547 * @VirtualProperty
548 * @SerializedName("tags")
549 * @Groups({"entries_for_user", "export_all"})
550 */
551 public function getSerializedTags()
552 {
553 $data = [];
554 foreach ($this->tags as $tag) {
555 $data[] = $tag->getLabel();
556 }
557
558 return $data;
559 }
560
0a018fe0
NL
561 /**
562 * @param Tag $tag
563 */
564 public function addTag(Tag $tag)
565 {
625acf33
KG
566 if ($this->tags->contains($tag)) {
567 return;
568 }
569
fc031e57
JB
570 // check if tag already exist but has not yet be persisted
571 // it seems that the previous condition with `contains()` doesn't check that case
572 foreach ($this->tags as $existingTag) {
fc732227 573 if ($existingTag->getLabel() === $tag->getLabel()) {
fc031e57
JB
574 return;
575 }
576 }
577
e42b13bc 578 $this->tags->add($tag);
46bbd8d3 579 $tag->addEntry($this);
0a018fe0 580 }
092ca707
NL
581
582 public function removeTag(Tag $tag)
583 {
e42b13bc
JB
584 if (!$this->tags->contains($tag)) {
585 return;
586 }
587
092ca707 588 $this->tags->removeElement($tag);
e42b13bc 589 $tag->removeEntry($this);
092ca707 590 }
fad31615
JB
591
592 /**
a1413a3d 593 * Set previewPicture.
fad31615
JB
594 *
595 * @param string $previewPicture
596 *
597 * @return Entry
598 */
599 public function setPreviewPicture($previewPicture)
600 {
601 $this->previewPicture = $previewPicture;
602
603 return $this;
604 }
605
606 /**
a1413a3d 607 * Get previewPicture.
fad31615
JB
608 *
609 * @return string
610 */
611 public function getPreviewPicture()
612 {
613 return $this->previewPicture;
614 }
98f0929f
JB
615
616 /**
617 * Set language.
618 *
619 * @param string $language
620 *
621 * @return Entry
622 */
623 public function setLanguage($language)
624 {
625 $this->language = $language;
626
627 return $this;
628 }
629
630 /**
631 * Get language.
632 *
633 * @return string
634 */
635 public function getLanguage()
636 {
637 return $this->language;
638 }
f3d0cb91
NL
639
640 /**
78b3c31d 641 * @return string
f3d0cb91
NL
642 */
643 public function getUuid()
644 {
645 return $this->uuid;
646 }
647
648 /**
78b3c31d 649 * @param string $uuid
f3d0cb91
NL
650 *
651 * @return Entry
652 */
653 public function setUuid($uuid)
654 {
655 $this->uuid = $uuid;
656
657 return $this;
658 }
659
660 public function generateUuid()
661 {
eddda878 662 if (null === $this->uuid) {
a7e2218e
NL
663 // @see http://blog.kevingomez.fr/til/2015/07/26/why-is-uniqid-slow/ for true parameter
664 $this->uuid = uniqid('', true);
f3d0cb91
NL
665 }
666 }
f1be7af4
NL
667
668 public function cleanUuid()
669 {
670 $this->uuid = null;
671 }
9d50517c 672}