]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Entity/Tag.php
Add environment variables to control scripts/dev.sh behavior
[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 /**
108 * @param Entry $entry
109 */
110 public function addEntry(Entry $entry)
111 {
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);
131 }
132
133 public function hasEntry($entry)
134 {
135 return $this->entries->contains($entry);
136 }
137
138 /**
139 * Get entries for this tag.
140 *
141 * @return ArrayCollection<Entry>
142 */
143 public function getEntries()
144 {
145 return $this->entries;
146 }
147
148 public function getEntriesByUserId($userId)
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 }
159 }