]>
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") |
bd0f3d32 | 16 | * @ORM\Table(name="`tag`") |
6d37a7e6 | 17 | * @ORM\Entity(repositoryClass="Wallabag\CoreBundle\Repository\TagRepository") |
0a018fe0 | 18 | * @ExclusionPolicy("all") |
9d50517c | 19 | */ |
653e8be4 | 20 | class Tag |
9d50517c NL |
21 | { |
22 | /** | |
4346a860 | 23 | * @var int |
9d50517c | 24 | * |
0a018fe0 | 25 | * @Expose |
653e8be4 | 26 | * @ORM\Column(name="id", type="integer") |
9d50517c | 27 | * @ORM\Id |
653e8be4 | 28 | * @ORM\GeneratedValue(strategy="AUTO") |
9d50517c NL |
29 | */ |
30 | private $id; | |
31 | ||
32 | /** | |
33 | * @var string | |
34 | * | |
0a018fe0 | 35 | * @Expose |
653e8be4 | 36 | * @ORM\Column(name="label", type="text") |
9d50517c | 37 | */ |
653e8be4 | 38 | private $label; |
9d50517c | 39 | |
3c65dfb7 | 40 | /** |
fc732227 | 41 | * @Expose |
3c65dfb7 NL |
42 | * @Gedmo\Slug(fields={"label"}) |
43 | * @ORM\Column(length=128, unique=true) | |
44 | */ | |
45 | private $slug; | |
46 | ||
0a018fe0 | 47 | /** |
46bbd8d3 | 48 | * @ORM\ManyToMany(targetEntity="Entry", mappedBy="tags", cascade={"persist"}) |
0a018fe0 NL |
49 | */ |
50 | private $entries; | |
51 | ||
fc732227 | 52 | public function __construct() |
46bbd8d3 NL |
53 | { |
54 | $this->entries = new ArrayCollection(); | |
55 | } | |
82d6d9cb JB |
56 | |
57 | public function __toString() | |
58 | { | |
59 | return $this->label; | |
60 | } | |
61 | ||
9d50517c | 62 | /** |
4346a860 | 63 | * Get id. |
9d50517c | 64 | * |
4346a860 | 65 | * @return int |
9d50517c NL |
66 | */ |
67 | public function getId() | |
68 | { | |
69 | return $this->id; | |
70 | } | |
71 | ||
72 | /** | |
4346a860 JB |
73 | * Set label. |
74 | * | |
75 | * @param string $label | |
9d50517c | 76 | * |
653e8be4 | 77 | * @return Tag |
9d50517c | 78 | */ |
653e8be4 | 79 | public function setLabel($label) |
9d50517c | 80 | { |
7036d91f | 81 | $this->label = mb_convert_case($label, MB_CASE_LOWER); |
9d50517c NL |
82 | |
83 | return $this; | |
84 | } | |
85 | ||
86 | /** | |
4346a860 | 87 | * Get label. |
9d50517c | 88 | * |
7df80cb3 | 89 | * @return string |
9d50517c | 90 | */ |
653e8be4 | 91 | public function getLabel() |
9d50517c | 92 | { |
2691cf04 | 93 | return $this->label; |
9d50517c | 94 | } |
46bbd8d3 | 95 | |
3c65dfb7 NL |
96 | public function getSlug() |
97 | { | |
98 | return $this->slug; | |
99 | } | |
100 | ||
e42b13bc JB |
101 | /** |
102 | * @param Entry $entry | |
103 | */ | |
46bbd8d3 NL |
104 | public function addEntry(Entry $entry) |
105 | { | |
e42b13bc JB |
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); | |
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 | } |