]>
Commit | Line | Data |
---|---|---|
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 | * ) | |
20 | * @ORM\Entity(repositoryClass="Wallabag\CoreBundle\Repository\TagRepository") | |
21 | * @ExclusionPolicy("all") | |
22 | */ | |
23 | class Tag | |
24 | { | |
25 | /** | |
26 | * @var int | |
27 | * | |
28 | * @Expose | |
29 | * @ORM\Column(name="id", type="integer") | |
30 | * @ORM\Id | |
31 | * @ORM\GeneratedValue(strategy="AUTO") | |
32 | */ | |
33 | private $id; | |
34 | ||
35 | /** | |
36 | * @var string | |
37 | * | |
38 | * @Expose | |
39 | * @ORM\Column(name="label", type="text") | |
40 | */ | |
41 | private $label; | |
42 | ||
43 | /** | |
44 | * @Expose | |
45 | * @Gedmo\Slug(fields={"label"}) | |
46 | * @ORM\Column(length=128, unique=true) | |
47 | */ | |
48 | private $slug; | |
49 | ||
50 | /** | |
51 | * @ORM\ManyToMany(targetEntity="Entry", mappedBy="tags", cascade={"persist"}) | |
52 | */ | |
53 | private $entries; | |
54 | ||
55 | public function __construct() | |
56 | { | |
57 | $this->entries = new ArrayCollection(); | |
58 | } | |
59 | ||
60 | public function __toString() | |
61 | { | |
62 | return $this->label; | |
63 | } | |
64 | ||
65 | /** | |
66 | * Get id. | |
67 | * | |
68 | * @return int | |
69 | */ | |
70 | public function getId() | |
71 | { | |
72 | return $this->id; | |
73 | } | |
74 | ||
75 | /** | |
76 | * Set label. | |
77 | * | |
78 | * @param string $label | |
79 | * | |
80 | * @return Tag | |
81 | */ | |
82 | public function setLabel($label) | |
83 | { | |
84 | $this->label = mb_convert_case($label, MB_CASE_LOWER); | |
85 | ||
86 | return $this; | |
87 | } | |
88 | ||
89 | /** | |
90 | * Get label. | |
91 | * | |
92 | * @return string | |
93 | */ | |
94 | public function getLabel() | |
95 | { | |
96 | return $this->label; | |
97 | } | |
98 | ||
99 | public function getSlug() | |
100 | { | |
101 | return $this->slug; | |
102 | } | |
103 | ||
104 | /** | |
105 | * @param Entry $entry | |
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 | /** | |
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); | |
128 | } | |
129 | ||
130 | public function hasEntry($entry) | |
131 | { | |
132 | return $this->entries->contains($entry); | |
133 | } | |
134 | ||
135 | /** | |
136 | * Get entries for this tag. | |
137 | * | |
138 | * @return ArrayCollection<Entry> | |
139 | */ | |
140 | public function getEntries() | |
141 | { | |
142 | return $this->entries; | |
143 | } | |
144 | ||
145 | public function getEntriesByUserId($userId) | |
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 | } | |
156 | } |