From 4eb53a8ceb4c33bb9abc2612326ccd721e541b72 Mon Sep 17 00:00:00 2001 From: Tim Schumacher Date: Mon, 28 Jul 2014 22:11:02 +0200 Subject: [PATCH] Extracted the tag functions into a trait, so it can be used by events and repeating events. --- .../Bundle/CalciferBundle/Entity/Event.php | 37 +---------- .../CalciferBundle/Entity/RepeatingEvent.php | 6 +- .../Bundle/CalciferBundle/Entity/TagTrait.php | 61 +++++++++++++++++++ 3 files changed, 67 insertions(+), 37 deletions(-) create mode 100755 src/Hackspace/Bundle/CalciferBundle/Entity/TagTrait.php diff --git a/src/Hackspace/Bundle/CalciferBundle/Entity/Event.php b/src/Hackspace/Bundle/CalciferBundle/Entity/Event.php index 88049ed..baf4b4a 100755 --- a/src/Hackspace/Bundle/CalciferBundle/Entity/Event.php +++ b/src/Hackspace/Bundle/CalciferBundle/Entity/Event.php @@ -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 ''; - } - } } diff --git a/src/Hackspace/Bundle/CalciferBundle/Entity/RepeatingEvent.php b/src/Hackspace/Bundle/CalciferBundle/Entity/RepeatingEvent.php index 75675be..4dc64fe 100755 --- a/src/Hackspace/Bundle/CalciferBundle/Entity/RepeatingEvent.php +++ b/src/Hackspace/Bundle/CalciferBundle/Entity/RepeatingEvent.php @@ -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")} * ) */ diff --git a/src/Hackspace/Bundle/CalciferBundle/Entity/TagTrait.php b/src/Hackspace/Bundle/CalciferBundle/Entity/TagTrait.php new file mode 100755 index 0000000..952d990 --- /dev/null +++ b/src/Hackspace/Bundle/CalciferBundle/Entity/TagTrait.php @@ -0,0 +1,61 @@ +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 ''; + } + } +} \ No newline at end of file