Extracted the tag functions into a trait, so it can be used by events and repeating events.

This commit is contained in:
Tim Schumacher 2014-07-28 22:11:02 +02:00
parent 68d3ff6f1e
commit 4eb53a8ceb
3 changed files with 67 additions and 37 deletions

View file

@ -3,7 +3,6 @@
namespace Hackspace\Bundle\CalciferBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\PersistentCollection;
/**
* Event
@ -13,6 +12,8 @@ use Doctrine\ORM\PersistentCollection;
*/
class Event extends BaseEntity
{
use TagTrait;
/**
* @var \DateTime
*
@ -92,41 +93,7 @@ class Event extends BaseEntity
return $this->location;
}
public function getTags() {
return $this->tags;
}
public function hasTag(Tag $tag) {
if ($this->tags instanceof PersistentCollection) {
return $this->tags->contains($tag);
} elseif (is_array($this->tags)) {
return in_array($tag,$this->tags);
} else {
return false;
}
}
public function addTag(Tag $tag) {
/** @var PersistentCollection $this->tags */
if (!$this->hasTag($tag)) {
$this->tags[] = $tag;
}
}
public function isValid() {
return true;
}
public function getTagsAsText() {
if (count($this->tags) > 0) {
$tags = [];
foreach ($this->tags as $tag) {
$tags[] = $tag->name;
}
return implode(',',$tags);
} else {
return '';
}
}
}

View file

@ -13,6 +13,8 @@ use Doctrine\ORM\PersistentCollection;
*/
class RepeatingEvent extends BaseEntity
{
use TagTrait;
/**
* @var \DateTime
*
@ -67,8 +69,8 @@ class RepeatingEvent extends BaseEntity
* @var array
*
* @ORM\ManyToMany(targetEntity="Tag")
* @ORM\JoinTable(name="repeat_events2tags",
* joinColumns={@ORM\JoinColumn(name="repeat_events_id", referencedColumnName="id")},
* @ORM\JoinTable(name="repeating_events2tags",
* joinColumns={@ORM\JoinColumn(name="repeating_events_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="tags_id", referencedColumnName="id")}
* )
*/

View file

@ -0,0 +1,61 @@
<?php
/**
* Created by PhpStorm.
* User: tim
* Date: 28.07.14
* Time: 21:00
*/
namespace Hackspace\Bundle\CalciferBundle\Entity;
use Doctrine\ORM\PersistentCollection;
use Doctrine\ORM\Mapping as ORM;
trait TagTrait
{
public function getTags()
{
return $this->tags;
}
public function clearTags()
{
if ($this->tags instanceof PersistentCollection) {
$this->tags->clear();
} elseif (is_array($this->tags)) {
$this->tags = [];
}
}
public function hasTag(Tag $tag)
{
if ($this->tags instanceof PersistentCollection) {
return $this->tags->contains($tag);
} elseif (is_array($this->tags)) {
return in_array($tag, $this->tags);
} else {
return false;
}
}
public function addTag(Tag $tag)
{
if (!$this->hasTag($tag)) {
$this->tags[] = $tag;
}
}
public function getTagsAsText()
{
if (count($this->tags) > 0) {
$tags = [];
foreach ($this->tags as $tag) {
$tags[] = $tag->name;
}
return implode(',', $tags);
} else {
return '';
}
}
}