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