]> 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 4ed588be867be0a3b38227dce42b863999721bee..9fb2f94fddcf8216e879f80d6e27e5064315d198 100644 (file)
@@ -2,18 +2,24 @@
 
 namespace Wallabag\CoreBundle\Entity;
 
+use Doctrine\Common\Collections\ArrayCollection;
 use Doctrine\ORM\Mapping as ORM;
-use JMS\Serializer\Annotation\XmlRoot;
+use Gedmo\Mapping\Annotation as Gedmo;
 use JMS\Serializer\Annotation\ExclusionPolicy;
 use JMS\Serializer\Annotation\Expose;
-use Doctrine\Common\Collections\ArrayCollection;
-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")
  */
@@ -38,6 +44,7 @@ class Tag
     private $label;
 
     /**
+     * @Expose
      * @Gedmo\Slug(fields={"label"})
      * @ORM\Column(length=128, unique=true)
      */
@@ -48,14 +55,8 @@ class Tag
      */
     private $entries;
 
-    /**
-     * @ORM\ManyToOne(targetEntity="Wallabag\UserBundle\Entity\User", inversedBy="tags")
-     */
-    private $user;
-
-    public function __construct(\Wallabag\UserBundle\Entity\User $user)
+    public function __construct()
     {
-        $this->user = $user;
         $this->entries = new ArrayCollection();
     }
 
@@ -83,7 +84,7 @@ class Tag
      */
     public function setLabel($label)
     {
-        $this->label = $label;
+        $this->label = mb_convert_case($label, MB_CASE_LOWER);
 
         return $this;
     }
@@ -105,7 +106,22 @@ 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)
@@ -114,10 +130,24 @@ class Tag
     }
 
     /**
-     * @return User
+     * Get entries for this tag.
+     *
+     * @return ArrayCollection<Entry>
      */
-    public function getUser()
+    public function getEntries()
+    {
+        return $this->entries;
+    }
+
+    public function getEntriesByUserId($userId)
     {
-        return $this->user;
+        $filteredEntries = new ArrayCollection();
+        foreach ($this->entries as $entry) {
+            if ($entry->getUser()->getId() === $userId) {
+                $filteredEntries->add($entry);
+            }
+        }
+
+        return $filteredEntries;
     }
 }