]> git.immae.eu Git - github/wallabag/wallabag.git/blobdiff - src/Wallabag/CoreBundle/Entity/Tag.php
manage assets through npm
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Entity / Tag.php
index 310175635763cbe119e5c2ebf601978806c4be7f..4b480ff1cdc4f2c4ebc2b7e4bf4b558edb95eb9a 100644 (file)
@@ -2,19 +2,27 @@
 
 namespace Wallabag\CoreBundle\Entity;
 
+use Doctrine\Common\Collections\ArrayCollection;
 use Doctrine\ORM\Mapping as ORM;
+use JMS\Serializer\Annotation\ExclusionPolicy;
+use JMS\Serializer\Annotation\Expose;
+use Gedmo\Mapping\Annotation as Gedmo;
+use JMS\Serializer\Annotation\XmlRoot;
 
 /**
- * Tag
+ * Tag.
  *
- * @ORM\Table(name="tag")
- * @ORM\Entity
+ * @XmlRoot("tag")
+ * @ORM\Table(name="`tag`")
+ * @ORM\Entity(repositoryClass="Wallabag\CoreBundle\Repository\TagRepository")
+ * @ExclusionPolicy("all")
  */
 class Tag
 {
     /**
-     * @var integer
+     * @var int
      *
+     * @Expose
      * @ORM\Column(name="id", type="integer")
      * @ORM\Id
      * @ORM\GeneratedValue(strategy="AUTO")
@@ -24,14 +32,37 @@ class Tag
     /**
      * @var string
      *
+     * @Expose
      * @ORM\Column(name="label", type="text")
      */
     private $label;
 
     /**
-     * Get id
+     * @Expose
+     * @Gedmo\Slug(fields={"label"})
+     * @ORM\Column(length=128, unique=true)
+     */
+    private $slug;
+
+    /**
+     * @ORM\ManyToMany(targetEntity="Entry", mappedBy="tags", cascade={"persist"})
+     */
+    private $entries;
+
+    public function __construct()
+    {
+        $this->entries = new ArrayCollection();
+    }
+
+    public function __toString()
+    {
+        return $this->label;
+    }
+
+    /**
+     * Get id.
      *
-     * @return integer
+     * @return int
      */
     public function getId()
     {
@@ -39,9 +70,10 @@ class Tag
     }
 
     /**
-     * Set label
+     * Set label.
+     *
+     * @param string $label
      *
-     * @param  string $label
      * @return Tag
      */
     public function setLabel($label)
@@ -52,12 +84,70 @@ class Tag
     }
 
     /**
-     * Get label
+     * Get label.
      *
      * @return string
      */
     public function getLabel()
     {
-        return $this->value;
+        return $this->label;
+    }
+
+    public function getSlug()
+    {
+        return $this->slug;
+    }
+
+    /**
+     * @param Entry $entry
+     */
+    public function addEntry(Entry $entry)
+    {
+        if ($this->entries->contains($entry)) {
+            return;
+        }
+
+        $this->entries->add($entry);
+        $entry->addTag($this);
+    }
+
+    /**
+     * @param Entry $entry
+     */
+    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;
     }
 }