TurboContentHeader::img()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace sokolnikov911\YandexTurboPages;
4
5
use DOMDocument;
6
7
/**
8
 * Class TurboContentHeader
9
 * @package sokolnikov911\YandexTurboPages
10
 * @deprecated 2.0.0 Use Content::header instead
11
 */
12
class TurboContentHeader implements TurboContentHeaderInterface
13
{
14
    /** @var string */
15
    protected $titleH1;
16
17
    /** @var string */
18
    protected $titleH2;
19
20
    /** @var string */
21
    protected $img;
22
23
    public function titleH1($titleH1)
24
    {
25
        $this->titleH1 = $titleH1;
26
        return $this;
27
    }
28
29
    public function titleH2($titleH2)
30
    {
31
        $this->titleH2 = $titleH2;
32
        return $this;
33
    }
34
35
    public function img($img)
36
    {
37
        $this->img = $img;
38
        return $this;
39
    }
40
41
    public function asHTML()
42
    {
43
        $DOMDoc = new DOMDocument('1.0', 'UTF-8');
44
45
        $titleH2 = $this->titleH2 ? $this->titleH2 : '';
46
47
        $headerDOMElement = $DOMDoc->createElement('header', $titleH2);
48
        $headerDOMElement = $DOMDoc->appendChild($headerDOMElement);
49
50
        if (!empty($this->titleH1)) {
51
            $h1DOMElement = $DOMDoc->createElement('h1', $this->titleH1);
52
            $headerDOMElement->appendChild($h1DOMElement);
53
        }
54
55
        if (!empty($this->img)) {
56
            $figureDOMElement = $DOMDoc->createElement('figure');
57
58
            $imgDOMElement = $DOMDoc->createElement('img');
59
            $imgDOMElement->setAttribute('src', $this->img);
60
            $figureDOMElement->appendChild($imgDOMElement);
61
62
            $headerDOMElement->appendChild($figureDOMElement);
63
        }
64
65
        return strval(html_entity_decode($DOMDoc->saveHTML()));
66
    }
67
}
68