Completed
Pull Request — 2.x (#266)
by Maximilian
06:50 queued 01:48
created

StructuredDataExtension::getFunctions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace Sonata\SeoBundle\Twig\Extension;
4
5
use Sonata\SeoBundle\Seo\StructuredDataAwarePage;
6
7
/**
8
 * @author Maximilian Berghoff <[email protected]>
9
 */
10
class StructuredDataExtension extends \Twig_Extension
11
{
12
    /**
13
     * @var StructuredDataAwarePage
14
     */
15
    protected $page;
16
17
    /**
18
     * @var string
19
     */
20
    protected $encoding;
21
22
    /**
23
     * @param StructuredDataAwarePage $page
24
     */
25
    public function __construct(StructuredDataAwarePage $page)
26
    {
27
        $this->page = $page;
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function getFunctions()
34
    {
35
        return [
36
            new \Twig_SimpleFunction('sonata_seo_structured_data', [$this, 'getStructuredData'], ['is_safe' => ['html']]),
37
        ];
38
    }
39
40
    /**
41
     * Creates a script tag with type 'json-ld' and the JSON-LD string stored in page object.
42
     *
43
     * @return string
44
     */
45
    public function getStructuredData()
46
    {
47
        if (empty($this->page->getStructuredData())) {
48
            return '';
49
        }
50
51
        return sprintf("<script type=\"application/ld+json\">%s</script>\n", $this->page->getStructuredData());
52
    }
53
}
54