Completed
Pull Request — master (#19)
by
unknown
01:09
created

AbstractTaggable::hasTag()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Beelab\TagBundle\Entity;
4
5
use Beelab\TagBundle\Tag\TagInterface;
6
use Beelab\TagBundle\Tag\TaggableInterface;
7
use Doctrine\Common\Collections\ArrayCollection;
8
9
/**
10
 * Abstract Taggable class
11
 * You can extend this class in your Entity.
12
 */
13
abstract class AbstractTaggable implements TaggableInterface
14
{
15
    /**
16
     * @var ArrayCollection
17
     *
18
     * Override this property in your Entity with definition of ManyToMany relation.
19
     */
20
    protected $tags;
21
22
    /**
23
     * @var string
24
     */
25
    protected $tagsText;
26
27
    public function __construct()
28
    {
29
        $this->tags = new ArrayCollection();
30
    }
31
32
    public function addTag(TagInterface $tag): void
33
    {
34
        $this->tags[] = $tag;
35
    }
36
37
    public function removeTag(TagInterface $tag): void
38
    {
39
        $this->tags->removeElement($tag);
40
    }
41
42
    public function getTags(): iterable
43
    {
44
        return $this->tags;
45
    }
46
47
    public function getTagNames(): array
48
    {
49
        return empty($this->tagsText) ? [] : array_map('trim', explode(',', $this->tagsText));
50
    }
51
52
    /**
53
     * Override this method in your Entity and update a field here.
54
     *
55
     * @param string $tagsText
56
     */
57
    public function setTagsText(?string $tagsText): void
58
    {
59
        $this->tagsText = $tagsText;
60
    }
61
62
    public function getTagsText(): ?string
63
    {
64
        $this->tagsText = implode(', ', $this->tags->toArray());
65
66
        return $this->tagsText;
67
    }
68
}
69