]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Entity/Tag.php
manage assets through npm
[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 JMS\Serializer\Annotation\ExclusionPolicy;
8 use JMS\Serializer\Annotation\Expose;
9 use Gedmo\Mapping\Annotation as Gedmo;
10 use JMS\Serializer\Annotation\XmlRoot;
11
12 /**
13 * Tag.
14 *
15 * @XmlRoot("tag")
16 * @ORM\Table(name="`tag`")
17 * @ORM\Entity(repositoryClass="Wallabag\CoreBundle\Repository\TagRepository")
18 * @ExclusionPolicy("all")
19 */
20 class Tag
21 {
22 /**
23 * @var int
24 *
25 * @Expose
26 * @ORM\Column(name="id", type="integer")
27 * @ORM\Id
28 * @ORM\GeneratedValue(strategy="AUTO")
29 */
30 private $id;
31
32 /**
33 * @var string
34 *
35 * @Expose
36 * @ORM\Column(name="label", type="text")
37 */
38 private $label;
39
40 /**
41 * @Expose
42 * @Gedmo\Slug(fields={"label"})
43 * @ORM\Column(length=128, unique=true)
44 */
45 private $slug;
46
47 /**
48 * @ORM\ManyToMany(targetEntity="Entry", mappedBy="tags", cascade={"persist"})
49 */
50 private $entries;
51
52 public function __construct()
53 {
54 $this->entries = new ArrayCollection();
55 }
56
57 public function __toString()
58 {
59 return $this->label;
60 }
61
62 /**
63 * Get id.
64 *
65 * @return int
66 */
67 public function getId()
68 {
69 return $this->id;
70 }
71
72 /**
73 * Set label.
74 *
75 * @param string $label
76 *
77 * @return Tag
78 */
79 public function setLabel($label)
80 {
81 $this->label = $label;
82
83 return $this;
84 }
85
86 /**
87 * Get label.
88 *
89 * @return string
90 */
91 public function getLabel()
92 {
93 return $this->label;
94 }
95
96 public function getSlug()
97 {
98 return $this->slug;
99 }
100
101 /**
102 * @param Entry $entry
103 */
104 public function addEntry(Entry $entry)
105 {
106 if ($this->entries->contains($entry)) {
107 return;
108 }
109
110 $this->entries->add($entry);
111 $entry->addTag($this);
112 }
113
114 /**
115 * @param Entry $entry
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 }