]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Entity/Entry.php
a4b0d7a82b4c436ed4bc79b6154e88590da36847
[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 * @Groups({"entries_for_user", "export_all"})
201 */
202 private $tags;
203
204 /*
205 * @param User $user
206 */
207 public function __construct(User $user)
208 {
209 $this->user = $user;
210 $this->tags = new ArrayCollection();
211 }
212
213 /**
214 * Get id.
215 *
216 * @return int
217 */
218 public function getId()
219 {
220 return $this->id;
221 }
222
223 /**
224 * Set title.
225 *
226 * @param string $title
227 *
228 * @return Entry
229 */
230 public function setTitle($title)
231 {
232 $this->title = $title;
233
234 return $this;
235 }
236
237 /**
238 * Get title.
239 *
240 * @return string
241 */
242 public function getTitle()
243 {
244 return $this->title;
245 }
246
247 /**
248 * Set url.
249 *
250 * @param string $url
251 *
252 * @return Entry
253 */
254 public function setUrl($url)
255 {
256 $this->url = $url;
257
258 return $this;
259 }
260
261 /**
262 * Get url.
263 *
264 * @return string
265 */
266 public function getUrl()
267 {
268 return $this->url;
269 }
270
271 /**
272 * Set isArchived.
273 *
274 * @param bool $isArchived
275 *
276 * @return Entry
277 */
278 public function setArchived($isArchived)
279 {
280 $this->isArchived = $isArchived;
281
282 return $this;
283 }
284
285 /**
286 * Get isArchived.
287 *
288 * @return bool
289 */
290 public function isArchived()
291 {
292 return $this->isArchived;
293 }
294
295 /**
296 * @VirtualProperty
297 * @SerializedName("is_archived")
298 * @Groups({"entries_for_user", "export_all"})
299 */
300 public function is_Archived()
301 {
302 return (int) $this->isArchived();
303 }
304
305 public function toggleArchive()
306 {
307 $this->isArchived = $this->isArchived() ^ 1;
308
309 return $this;
310 }
311
312 /**
313 * Set isStarred.
314 *
315 * @param bool $isStarred
316 *
317 * @return Entry
318 */
319 public function setStarred($isStarred)
320 {
321 $this->isStarred = $isStarred;
322
323 return $this;
324 }
325
326 /**
327 * Get isStarred.
328 *
329 * @return bool
330 */
331 public function isStarred()
332 {
333 return $this->isStarred;
334 }
335
336 /**
337 * @VirtualProperty
338 * @SerializedName("is_starred")
339 * @Groups({"entries_for_user", "export_all"})
340 */
341 public function is_Starred()
342 {
343 return (int) $this->isStarred();
344 }
345
346 public function toggleStar()
347 {
348 $this->isStarred = $this->isStarred() ^ 1;
349
350 return $this;
351 }
352
353 /**
354 * Set content.
355 *
356 * @param string $content
357 *
358 * @return Entry
359 */
360 public function setContent($content)
361 {
362 $this->content = $content;
363
364 return $this;
365 }
366
367 /**
368 * Get content.
369 *
370 * @return string
371 */
372 public function getContent()
373 {
374 return $this->content;
375 }
376
377 /**
378 * @return User
379 */
380 public function getUser()
381 {
382 return $this->user;
383 }
384
385 /**
386 * @VirtualProperty
387 * @SerializedName("user_name")
388 */
389 public function getUserName()
390 {
391 return $this->user->getUserName();
392 }
393
394 /**
395 * @VirtualProperty
396 * @SerializedName("user_email")
397 */
398 public function getUserEmail()
399 {
400 return $this->user->getEmail();
401 }
402
403 /**
404 * @VirtualProperty
405 * @SerializedName("user_id")
406 */
407 public function getUserId()
408 {
409 return $this->user->getId();
410 }
411
412 /**
413 * Set created_at.
414 * Only used when importing data from an other service.
415 *
416 * @param \DateTime $createdAt
417 *
418 * @return Entry
419 */
420 public function setCreatedAt(\DateTime $createdAt)
421 {
422 $this->createdAt = $createdAt;
423
424 return $this;
425 }
426
427 /**
428 * @return \DateTime
429 */
430 public function getCreatedAt()
431 {
432 return $this->createdAt;
433 }
434
435 /**
436 * @return \DateTime
437 */
438 public function getUpdatedAt()
439 {
440 return $this->updatedAt;
441 }
442
443 /**
444 * @ORM\PrePersist
445 * @ORM\PreUpdate
446 */
447 public function timestamps()
448 {
449 if (is_null($this->createdAt)) {
450 $this->createdAt = new \DateTime();
451 }
452
453 $this->updatedAt = new \DateTime();
454 }
455
456 /**
457 * @return ArrayCollection<Annotation>
458 */
459 public function getAnnotations()
460 {
461 return $this->annotations;
462 }
463
464 /**
465 * @param Annotation $annotation
466 */
467 public function setAnnotation(Annotation $annotation)
468 {
469 $this->annotations[] = $annotation;
470 }
471
472 /**
473 * @return string
474 */
475 public function getMimetype()
476 {
477 return $this->mimetype;
478 }
479
480 /**
481 * @param string $mimetype
482 */
483 public function setMimetype($mimetype)
484 {
485 $this->mimetype = $mimetype;
486 }
487
488 /**
489 * @return int
490 */
491 public function getReadingTime()
492 {
493 return $this->readingTime;
494 }
495
496 /**
497 * @param int $readingTime
498 */
499 public function setReadingTime($readingTime)
500 {
501 $this->readingTime = $readingTime;
502 }
503
504 /**
505 * @return string
506 */
507 public function getDomainName()
508 {
509 return $this->domainName;
510 }
511
512 /**
513 * @param string $domainName
514 */
515 public function setDomainName($domainName)
516 {
517 $this->domainName = $domainName;
518 }
519
520 /**
521 * @return bool
522 */
523 public function isPublic()
524 {
525 return $this->isPublic;
526 }
527
528 /**
529 * @param bool $isPublic
530 */
531 public function setIsPublic($isPublic)
532 {
533 $this->isPublic = $isPublic;
534 }
535
536 /**
537 * @return ArrayCollection<Tag>
538 */
539 public function getTags()
540 {
541 return $this->tags;
542 }
543
544 /**
545 * @param Tag $tag
546 */
547 public function addTag(Tag $tag)
548 {
549 if ($this->tags->contains($tag)) {
550 return;
551 }
552
553 // check if tag already exist but has not yet be persisted
554 // it seems that the previous condition with `contains()` doesn't check that case
555 foreach ($this->tags as $existingTag) {
556 if ($existingTag->getLabel() === $tag->getLabel()) {
557 return;
558 }
559 }
560
561 $this->tags->add($tag);
562 $tag->addEntry($this);
563 }
564
565 public function removeTag(Tag $tag)
566 {
567 if (!$this->tags->contains($tag)) {
568 return;
569 }
570
571 $this->tags->removeElement($tag);
572 $tag->removeEntry($this);
573 }
574
575 /**
576 * Set previewPicture.
577 *
578 * @param string $previewPicture
579 *
580 * @return Entry
581 */
582 public function setPreviewPicture($previewPicture)
583 {
584 $this->previewPicture = $previewPicture;
585
586 return $this;
587 }
588
589 /**
590 * Get previewPicture.
591 *
592 * @return string
593 */
594 public function getPreviewPicture()
595 {
596 return $this->previewPicture;
597 }
598
599 /**
600 * Set language.
601 *
602 * @param string $language
603 *
604 * @return Entry
605 */
606 public function setLanguage($language)
607 {
608 $this->language = $language;
609
610 return $this;
611 }
612
613 /**
614 * Get language.
615 *
616 * @return string
617 */
618 public function getLanguage()
619 {
620 return $this->language;
621 }
622
623 /**
624 * @return string
625 */
626 public function getUuid()
627 {
628 return $this->uuid;
629 }
630
631 /**
632 * @param string $uuid
633 *
634 * @return Entry
635 */
636 public function setUuid($uuid)
637 {
638 $this->uuid = $uuid;
639
640 return $this;
641 }
642
643 public function generateUuid()
644 {
645 if (null === $this->uuid) {
646 // @see http://blog.kevingomez.fr/til/2015/07/26/why-is-uniqid-slow/ for true parameter
647 $this->uuid = uniqid('', true);
648 }
649 }
650
651 public function cleanUuid()
652 {
653 $this->uuid = null;
654 }
655 }