AbstractTaggable::getTagsText()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Beelab\TagBundle\Entity;
4
5
use Beelab\TagBundle\Tag\TaggableInterface;
6
use Beelab\TagBundle\Tag\TagInterface;
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
     * Override this property in your Entity with definition of ManyToMany relation.
17
     *
18
     * @var ArrayCollection
19
     */
20
    protected $tags;
21
22
    /**
23
     * @var string|null
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 hasTag(TagInterface $tag): bool
43
    {
44
        return $this->tags->contains($tag);
45
    }
46
47
    public function getTags(): iterable
48
    {
49
        return $this->tags;
50
    }
51
52
    public function getTagNames(): array
53
    {
54
        return empty($this->tagsText) ? [] : \array_map('trim', \explode(',', $this->tagsText));
55
    }
56
57
    /**
58
     * Override this method in your Entity and update a field here.
59
     */
60
    public function setTagsText(?string $tagsText): void
61
    {
62
        $this->tagsText = $tagsText;
63
    }
64
65
    public function getTagsText(): ?string
66
    {
67
        $this->tagsText = \implode(', ', $this->tags->toArray());
68
69
        return $this->tagsText;
70
    }
71
}
72