]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Entity/Tag.php
Merge pull request #4151 from ldidry/fix-4060
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Entity / Tag.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 Gedmo\Mapping\Annotation as Gedmo;
8 use JMS\Serializer\Annotation\ExclusionPolicy;
9 use JMS\Serializer\Annotation\Expose;
10 use JMS\Serializer\Annotation\XmlRoot;
11
12 /**
13 * Tag.
14 *
15 * @XmlRoot("tag")
16 * @ORM\Table(
17 * name="`tag`",
18 * options={"collate"="utf8mb4_bin", "charset"="utf8mb4"},
19 * indexes={
20 * @ORM\Index(name="tag_label", columns={"label"}, options={"lengths"={255}}),
21 * }
22 * )
23 * @ORM\Entity(repositoryClass="Wallabag\CoreBundle\Repository\TagRepository")
24 * @ExclusionPolicy("all")
25 */
26 class Tag
27 {
28 /**
29 * @var int
30 *
31 * @Expose
32 * @ORM\Column(name="id", type="integer")
33 * @ORM\Id
34 * @ORM\GeneratedValue(strategy="AUTO")
35 */
36 private $id;
37
38 /**
39 * @var string
40 *
41 * @Expose
42 * @ORM\Column(name="label", type="text")
43 */
44 private $label;
45
46 /**
47 * @Expose
48 * @Gedmo\Slug(fields={"label"})
49 * @ORM\Column(length=128, unique=true)
50 */
51 private $slug;
52
53 /**
54 * @ORM\ManyToMany(targetEntity="Entry", mappedBy="tags", cascade={"persist"})
55 */
56 private $entries;
57
58 public function __construct()
59 {
60 $this->entries = new ArrayCollection();
61 }
62
63 public function __toString()
64 {
65 return $this->label;
66 }
67
68 /**
69 * Get id.
70 *
71 * @return int
72 */
73 public function getId()
74 {
75 return $this->id;
76 }
77
78 /**
79 * Set label.
80 *
81 * @param string $label
82 *
83 * @return Tag
84 */
85 public function setLabel($label)
86 {
87 $this->label = mb_convert_case($label, MB_CASE_LOWER);
88
89 return $this;
90 }
91
92 /**
93 * Get label.
94 *
95 * @return string
96 */
97 public function getLabel()
98 {
99 return $this->label;
100 }
101
102 public function getSlug()
103 {
104 return $this->slug;
105 }
106
107 public function addEntry(Entry $entry)
108 {
109 if ($this->entries->contains($entry)) {
110 return;
111 }
112
113 $this->entries->add($entry);
114 $entry->addTag($this);
115 }
116
117 public function removeEntry(Entry $entry)
118 {
119 if (!$this->entries->contains($entry)) {
120 return;
121 }
122
123 $this->entries->removeElement($entry);
124 $entry->removeTag($this);
125 }
126
127 public function hasEntry($entry)
128 {
129 return $this->entries->contains($entry);
130 }
131
132 /**
133 * Get entries for this tag.
134 *
135 * @return ArrayCollection<Entry>
136 */
137 public function getEntries()
138 {
139 return $this->entries;
140 }
141
142 public function getEntriesByUserId($userId)
143 {
144 $filteredEntries = new ArrayCollection();
145 foreach ($this->entries as $entry) {
146 if ($entry->getUser()->getId() === $userId) {
147 $filteredEntries->add($entry);
148 }
149 }
150
151 return $filteredEntries;
152 }
153 }