Completed
Pull Request — master (#2)
by
unknown
08:45
created

HtmlPage::setCanonical()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 0
cts 4
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * Yii2 Pages Module
4
 *
5
 * @link      https://github.com/hiqdev/yii2-module-pages
6
 * @package   yii2-module-pages
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2016-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\yii2\modules\pages\models;
12
13
class HtmlPage extends AbstractPage
14
{
15
    /** @var null|string */
16
    private $featuredImageUrl;
17
18
    /** @var null|string */
19
    private $slug;
20
21
    /** @var null|string */
22
    private $keywords;
23
24
    /** @var null|string */
25
    private $description;
26
27
    /** @var null|string */
28
    private $canonical;
29
30
    const META_DATA = ['keywords', 'description', 'canonical'];
31
32
    /**
33
     * @param array $params
34
     * @return string
35
     */
36
    public function render(array $params = []): string
37
    {
38
        $this->setMetaData();
39
40
        return $this->text;
41
    }
42
43
    /**
44
     * Renders miniature version of page for list
45
     * @return string
46
     */
47
    public function renderMiniature(): string
48
    {
49
        $img = $this->featuredImageUrl ?
50
               "<img src=\"$this->featuredImageUrl\" alt=\"$this->slug\">" :
51
               '';
52
53
        return <<<HTML
54
<h1 class="post-title"><a href="$this->url">$this->title</a></h1>
55
$img
56
$this->text
57
HTML;
58
    }
59
60
    private function setMetaData(): void
61
    {
62
        foreach (self::META_DATA as $tag) {
63
            if (is_null($this->{$tag})) {
64
                continue;
65
            }
66
            $this->view->registerMetaTag([
67
                'name' => $tag,
68
                'content' => $this->{$tag},
69
            ]);
70
        }
71
    }
72
73
    /**
74
     * @param null|string $slug
75
     */
76
    public function setSlug(?string $slug): void
77
    {
78
        $this->slug = $slug;
79
    }
80
81
    /**
82
     * @param null|string $keywords
83
     */
84
    public function setKeywords(?string $keywords): void
85
    {
86
        $this->keywords = $keywords;
87
    }
88
89
    /**
90
     * @param null|string $description
91
     */
92
    public function setDescription(?string $description): void
93
    {
94
        $this->description = $description;
95
    }
96
97
    /**
98
     * @param null|string $featuredImageUrl
99
     */
100
    public function setFeaturedImageUrl(?string $featuredImageUrl): void
101
    {
102
        $this->featuredImageUrl = $featuredImageUrl;
103
    }
104
105
    /**
106
     * @param null|string $canonical
107
     */
108
    public function setCanonical(?string $canonical): void
109
    {
110
        $this->canonical = $canonical;
111
    }
112
}
113