]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Entity/Tag.php
Add user_created index
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Entity / Tag.php
CommitLineData
9d50517c
NL
1<?php
2
ad4d1caa 3namespace Wallabag\CoreBundle\Entity;
9d50517c 4
619cc453 5use Doctrine\Common\Collections\ArrayCollection;
9d50517c 6use Doctrine\ORM\Mapping as ORM;
f808b016 7use Gedmo\Mapping\Annotation as Gedmo;
0a018fe0
NL
8use JMS\Serializer\Annotation\ExclusionPolicy;
9use JMS\Serializer\Annotation\Expose;
619cc453 10use JMS\Serializer\Annotation\XmlRoot;
9d50517c
NL
11
12/**
4346a860 13 * Tag.
9d50517c 14 *
6d37a7e6 15 * @XmlRoot("tag")
feb239ea
KD
16 * @ORM\Table(
17 * name="`tag`",
18 * options={"collate"="utf8mb4_bin", "charset"="utf8mb4"},
19 * )
6d37a7e6 20 * @ORM\Entity(repositoryClass="Wallabag\CoreBundle\Repository\TagRepository")
0a018fe0 21 * @ExclusionPolicy("all")
9d50517c 22 */
653e8be4 23class Tag
9d50517c
NL
24{
25 /**
4346a860 26 * @var int
9d50517c 27 *
0a018fe0 28 * @Expose
653e8be4 29 * @ORM\Column(name="id", type="integer")
9d50517c 30 * @ORM\Id
653e8be4 31 * @ORM\GeneratedValue(strategy="AUTO")
9d50517c
NL
32 */
33 private $id;
34
35 /**
36 * @var string
37 *
0a018fe0 38 * @Expose
653e8be4 39 * @ORM\Column(name="label", type="text")
9d50517c 40 */
653e8be4 41 private $label;
9d50517c 42
3c65dfb7 43 /**
fc732227 44 * @Expose
3c65dfb7
NL
45 * @Gedmo\Slug(fields={"label"})
46 * @ORM\Column(length=128, unique=true)
47 */
48 private $slug;
49
0a018fe0 50 /**
46bbd8d3 51 * @ORM\ManyToMany(targetEntity="Entry", mappedBy="tags", cascade={"persist"})
0a018fe0
NL
52 */
53 private $entries;
54
fc732227 55 public function __construct()
46bbd8d3
NL
56 {
57 $this->entries = new ArrayCollection();
58 }
82d6d9cb
JB
59
60 public function __toString()
61 {
62 return $this->label;
63 }
64
9d50517c 65 /**
4346a860 66 * Get id.
9d50517c 67 *
4346a860 68 * @return int
9d50517c
NL
69 */
70 public function getId()
71 {
72 return $this->id;
73 }
74
75 /**
4346a860
JB
76 * Set label.
77 *
78 * @param string $label
9d50517c 79 *
653e8be4 80 * @return Tag
9d50517c 81 */
653e8be4 82 public function setLabel($label)
9d50517c 83 {
7036d91f 84 $this->label = mb_convert_case($label, MB_CASE_LOWER);
9d50517c
NL
85
86 return $this;
87 }
88
89 /**
4346a860 90 * Get label.
9d50517c 91 *
7df80cb3 92 * @return string
9d50517c 93 */
653e8be4 94 public function getLabel()
9d50517c 95 {
2691cf04 96 return $this->label;
9d50517c 97 }
46bbd8d3 98
3c65dfb7
NL
99 public function getSlug()
100 {
101 return $this->slug;
102 }
103
e42b13bc
JB
104 /**
105 * @param Entry $entry
106 */
46bbd8d3
NL
107 public function addEntry(Entry $entry)
108 {
e42b13bc
JB
109 if ($this->entries->contains($entry)) {
110 return;
111 }
112
113 $this->entries->add($entry);
114 $entry->addTag($this);
115 }
116
117 /**
118 * @param Entry $entry
119 */
120 public function removeEntry(Entry $entry)
121 {
122 if (!$this->entries->contains($entry)) {
123 return;
124 }
125
126 $this->entries->removeElement($entry);
127 $entry->removeTag($this);
46bbd8d3 128 }
48b67328 129
7244d6cb
NL
130 public function hasEntry($entry)
131 {
132 return $this->entries->contains($entry);
133 }
567421af
TC
134
135 /**
136 * Get entries for this tag.
137 *
138 * @return ArrayCollection<Entry>
139 */
140 public function getEntries()
141 {
142 return $this->entries;
143 }
12c69756 144
d9926005 145 public function getEntriesByUserId($userId)
12c69756
NL
146 {
147 $filteredEntries = new ArrayCollection();
148 foreach ($this->entries as $entry) {
149 if ($entry->getUser()->getId() === $userId) {
150 $filteredEntries->add($entry);
151 }
152 }
153
154 return $filteredEntries;
155 }
9d50517c 156}