]> git.immae.eu Git - github/wallabag/wallabag.git/blobdiff - src/Wallabag/CoreBundle/Entity/Tag.php
Merge pull request #4151 from ldidry/fix-4060
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Entity / Tag.php
index c1940e99b29f4723196fc4f8665e2a9da68cf087..9fb2f94fddcf8216e879f80d6e27e5064315d198 100644 (file)
@@ -4,16 +4,22 @@ namespace Wallabag\CoreBundle\Entity;
 
 use Doctrine\Common\Collections\ArrayCollection;
 use Doctrine\ORM\Mapping as ORM;
+use Gedmo\Mapping\Annotation as Gedmo;
 use JMS\Serializer\Annotation\ExclusionPolicy;
 use JMS\Serializer\Annotation\Expose;
-use Gedmo\Mapping\Annotation as Gedmo;
 use JMS\Serializer\Annotation\XmlRoot;
 
 /**
  * Tag.
  *
  * @XmlRoot("tag")
- * @ORM\Table(name="`tag`")
+ * @ORM\Table(
+ *     name="`tag`",
+ *     options={"collate"="utf8mb4_bin", "charset"="utf8mb4"},
+ *     indexes={
+ *         @ORM\Index(name="tag_label", columns={"label"}, options={"lengths"={255}}),
+ *     }
+ * )
  * @ORM\Entity(repositoryClass="Wallabag\CoreBundle\Repository\TagRepository")
  * @ExclusionPolicy("all")
  */
@@ -78,7 +84,7 @@ class Tag
      */
     public function setLabel($label)
     {
-        $this->label = $label;
+        $this->label = mb_convert_case($label, MB_CASE_LOWER);
 
         return $this;
     }
@@ -100,11 +106,48 @@ class Tag
 
     public function addEntry(Entry $entry)
     {
-        $this->entries[] = $entry;
+        if ($this->entries->contains($entry)) {
+            return;
+        }
+
+        $this->entries->add($entry);
+        $entry->addTag($this);
+    }
+
+    public function removeEntry(Entry $entry)
+    {
+        if (!$this->entries->contains($entry)) {
+            return;
+        }
+
+        $this->entries->removeElement($entry);
+        $entry->removeTag($this);
     }
 
     public function hasEntry($entry)
     {
         return $this->entries->contains($entry);
     }
+
+    /**
+     * Get entries for this tag.
+     *
+     * @return ArrayCollection<Entry>
+     */
+    public function getEntries()
+    {
+        return $this->entries;
+    }
+
+    public function getEntriesByUserId($userId)
+    {
+        $filteredEntries = new ArrayCollection();
+        foreach ($this->entries as $entry) {
+            if ($entry->getUser()->getId() === $userId) {
+                $filteredEntries->add($entry);
+            }
+        }
+
+        return $filteredEntries;
+    }
 }