]>
Commit | Line | Data |
---|---|---|
9d50517c NL |
1 | <?php |
2 | ||
ad4d1caa | 3 | namespace Wallabag\CoreBundle\Entity; |
9d50517c | 4 | |
619cc453 | 5 | use Doctrine\Common\Collections\ArrayCollection; |
9d50517c | 6 | use Doctrine\ORM\Mapping as ORM; |
f808b016 | 7 | use Gedmo\Mapping\Annotation as Gedmo; |
0a018fe0 NL |
8 | use JMS\Serializer\Annotation\ExclusionPolicy; |
9 | use JMS\Serializer\Annotation\Expose; | |
619cc453 | 10 | use 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 | 26 | class 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 | ||
46bbd8d3 NL |
107 | public function addEntry(Entry $entry) |
108 | { | |
e42b13bc JB |
109 | if ($this->entries->contains($entry)) { |
110 | return; | |
111 | } | |
112 | ||
113 | $this->entries->add($entry); | |
114 | $entry->addTag($this); | |
115 | } | |
116 | ||
e42b13bc JB |
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); | |
46bbd8d3 | 125 | } |
48b67328 | 126 | |
7244d6cb NL |
127 | public function hasEntry($entry) |
128 | { | |
129 | return $this->entries->contains($entry); | |
130 | } | |
567421af TC |
131 | |
132 | /** | |
133 | * Get entries for this tag. | |
134 | * | |
135 | * @return ArrayCollection<Entry> | |
136 | */ | |
137 | public function getEntries() | |
138 | { | |
139 | return $this->entries; | |
140 | } | |
12c69756 | 141 | |
d9926005 | 142 | public function getEntriesByUserId($userId) |
12c69756 NL |
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 | } | |
9d50517c | 153 | } |