TagBuilder::render()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.2
c 0
b 0
f 0
cc 4
eloc 12
nc 8
nop 0
1
<?php
2
3
namespace Leogout\Bundle\SeoBundle\Builder;
4
5
use Leogout\Bundle\SeoBundle\Factory\TagFactory;
6
use Leogout\Bundle\SeoBundle\Model\LinkTag;
7
use Leogout\Bundle\SeoBundle\Model\MetaTag;
8
use Leogout\Bundle\SeoBundle\Model\RenderableInterface;
9
use Leogout\Bundle\SeoBundle\Model\TitleTag;
10
11
/**
12
 * Description of TagBuilder.
13
 *
14
 * @author: leogout
15
 */
16
class TagBuilder implements RenderableInterface
17
{
18
    /**
19
     * @var TagFactory
20
     */
21
    protected $tagFactory;
22
23
    /**
24
     * @var TitleTag
25
     */
26
    protected $title;
27
28
    /**
29
     * @var MetaTag[]
30
     */
31
    protected $metas = [];
32
33
    /**
34
     * @var LinkTag[]
35
     */
36
    protected $links = [];
37
38
    /**
39
     * TagBuilder constructor.
40
     *
41
     * @param TagFactory $tagFactory
42
     */
43
    public function __construct(TagFactory $tagFactory)
44
    {
45
        $this->tagFactory = $tagFactory;
46
    }
47
48
    /**
49
     * @param string $title
50
     *
51
     * @return TitleTag
52
     */
53
    public function setTitle($title)
54
    {
55
        return $this->title = $this->tagFactory
56
            ->createTitle()
57
            ->setContent($title);
58
    }
59
60
    /**
61
     * @return TitleTag
62
     */
63
    public function getTitle()
64
    {
65
        return $this->title;
66
    }
67
68
    /**
69
     * @param string      $name
70
     * @param string|null $type
71
     * @param string|null $value
72
     * @param string|null $content
73
     *
74
     * @return MetaTag
75
     */
76
    public function addMeta($name, $type = MetaTag::NAME_TYPE, $value = null, $content = null)
77
    {
78
        return $this->metas[$name] = $this->tagFactory
79
            ->createMeta()
80
            ->setType($type)
81
            ->setValue($value)
82
            ->setContent($content);
83
    }
84
85
    /**
86
     * @param $name
87
     *
88
     * @return MetaTag|null
89
     */
90
    public function getMeta($name)
91
    {
92
        return isset($this->metas[$name]) ? $this->metas[$name] : null;
93
    }
94
95
    /**
96
     * @param string      $name
97
     * @param string|null $href
98
     * @param string|null $rel
99
     * @param string|null $type
100
     * @param string|null $title
101
     *
102
     * @return LinkTag
103
     */
104
    public function addLink($name, $href = null, $rel = null, $type = null, $title = null)
105
    {
106
        return $this->links[$name] = $this->tagFactory
107
            ->createLink()
108
            ->setHref($href)
109
            ->setRel($rel)
110
            ->setType($type)
111
            ->setTitle($title);
112
    }
113
114
    /**
115
     * @param string $name
116
     *
117
     * @return LinkTag|null
118
     */
119
    public function getLink($name)
120
    {
121
        return isset($this->links[$name]) ? $this->links[$name] : null;
122
    }
123
124
    /**
125
     * @return string
126
     */
127
    public function render()
128
    {
129
        $tags = [];
130
131
        if (null !== $this->title) {
132
            array_push($tags, $this->title);
133
        }
134
        if (count($this->metas) > 0) {
135
            $tags = array_merge($tags, $this->metas);
136
        }
137
        if (count($this->links) > 0) {
138
            $tags = array_merge($tags, $this->links);
139
        }
140
141
        return implode(PHP_EOL,
142
            array_map(function (RenderableInterface $tag) {
143
                return $tag->render();
144
            }, $tags)
145
        );
146
    }
147
148
    /**
149
     * @return string
150
     */
151
    public function __toString()
152
    {
153
        return $this->render();
154
    }
155
}
156