OpenGraphRenderer::create()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Novaway\Component\OpenGraph\View;
4
5
use Novaway\Component\OpenGraph\OpenGraphInterface;
6
use Novaway\Component\OpenGraph\OpenGraphTag;
7
use Novaway\Component\OpenGraph\OpenGraphTagInterface;
8
9
class OpenGraphRenderer implements OpenGraphRendererInterface
10
{
11
    /** @var string */
12
    protected static $tagTemplate = '<meta property="#property#" content="#content#" />';
13
14
15
    /**
16
     * Create renderer instance
17
     *
18
     * @return static
19
     */
20
    public static function create()
21
    {
22
        return new static();
23
    }
24
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function renderNamespaceAttributes(OpenGraphInterface $graph, $withTag = true)
29
    {
30
        $namespaces = $graph->getNamespaces();
31
        if (empty($namespaces)) {
32
            return '';
33
        }
34
35
        $attributes = '';
36
        foreach ($namespaces as $prefix => $uri) {
37
            if (!empty($attributes)) {
38
                $attributes .= ' ';
39
            }
40
41
            $attributes .= sprintf('%s: %s', $prefix, $uri);
42
        }
43
44
        if ($withTag) {
45
            $attributes = sprintf('prefix="%s"', $attributes);
46
        }
47
48
        return $attributes;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function render(OpenGraphInterface $graph, $tagSeparator = '')
55
    {
56
        $html = '';
57
58
        foreach ($graph->getTags() as $tag) {
59
            if (!empty($html)) {
60
                $html .= $tagSeparator;
61
            }
62
63
            $html .= $this->renderTag($tag);
64
        }
65
66
        return $html;
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function renderTag(OpenGraphTagInterface $tag)
73
    {
74
        return str_replace(
75
            ['#property#', '#content#'],
76
            [sprintf('%s:%s', $tag->getPrefix(), $tag->getProperty()), $tag->getContent()],
77
            self::$tagTemplate
78
        );
79
    }
80
}
81