X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=src%2FWallabag%2FCoreBundle%2FEntity%2FTag.php;h=c19023af5bffdd8af1175840bea9b15a522a5251;hb=f808b01692a835673f328d7221ba8c212caa9b61;hp=1cdf4df027cc772dcbedf8f8923276ded593a77a;hpb=0a018fe03980b35c9f7aca838e67a8efa43b7f2d;p=github%2Fwallabag%2Fwallabag.git diff --git a/src/Wallabag/CoreBundle/Entity/Tag.php b/src/Wallabag/CoreBundle/Entity/Tag.php index 1cdf4df0..c19023af 100644 --- a/src/Wallabag/CoreBundle/Entity/Tag.php +++ b/src/Wallabag/CoreBundle/Entity/Tag.php @@ -2,23 +2,25 @@ 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 JMS\Serializer\Annotation\XmlRoot; /** - * Tag + * Tag. * * @XmlRoot("tag") - * @ORM\Table(name="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") @@ -36,14 +38,31 @@ class Tag private $label; /** - * @ORM\ManyToMany(targetEntity="Entry", mappedBy="tags", cascade={"persist", "merge"}) + * @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 + * Get id. * - * @return integer + * @return int */ public function getId() { @@ -51,9 +70,10 @@ class Tag } /** - * Set label + * Set label. + * + * @param string $label * - * @param string $label * @return Tag */ public function setLabel($label) @@ -64,7 +84,7 @@ class Tag } /** - * Get label + * Get label. * * @return string */ @@ -72,4 +92,62 @@ class Tag { 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 + */ + 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; + } }