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