Completed
Branch master (740422)
by Mathieu
02:04
created

Page::addRss()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 3
dl 0
loc 6
ccs 0
cts 0
cp 0
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace Suricate;
3
4
class Page
5
{
6
    protected $title;
7
    protected $encoding       = 'utf-8';
8
    protected $language       = 'en_US';
9
    protected $stylesheets    = array();
10
    protected $metas          = array();
11
    protected $scripts        = array();
12
    protected $rss            = array();
13
    protected $htmlClass      = array();
14
15 1
    public function __construct()
16
    {
17
18 1
    }
19
20
21 1
    public function setLanguage($language)
22
    {
23 1
        $this->language = $language;
24
25 1
        return $this;
26
    }
27
28
    public function setEncoding($encoding)
29
    {
30
        $this->encoding = $encoding;
31
32
        return $this;
33
    }
34
35
    public function setTitle($title)
36
    {
37
        $this->title = $title;
38
39
        return $this;
40
    }
41
42
    //
43
    // Stylesheets
44
    //
45
    
46
    /**
47
     * Add a stylesheet
48
     * @param string $id  Unique stylesheet identifier
49
     * @param string $url Stylesheet URL
50
     * @param string $media Stylesheet media (default: all)
51
     */
52
    public function addStylesheet($id, $url, $media = 'all')
53
    {
54
        $this->stylesheets[$id] = array('url' => $url, 'media' => $media);
55
56
        return $this;
57
    }
58
59
    /**
60
     * Render stylesheets html tags
61
     * @return string Stylesheet HTML
62
     */
63
    protected function renderStylesheets()
64
    {
65
        $output = '';
66
        foreach ($this->stylesheets as $id => $stylesheet) {
67
            $output .= '<link rel="stylesheet"';
68
            $output .= ' id="' . $id . '"';
69
            $output .= ' href="' . $stylesheet['url'] . '"';
70
            $output .= ' type="text/css"';
71
            $output .= ' media="' . $stylesheet['media'] . '"';
72
            $output .= '/>' . "\n";
73
        }
74
75
        return $output;
76
    }
77
78
    public function addHtmlClass($className)
79
    {
80
        $this->htmlClass[$className] = true;
81
82
        return $this;
83
    }
84
85
     /**
86
     * Add a RSS Feed
87
     * @param string $id  Unique stylesheet identifier
88
     * @param string $url Feed URL
89
     * @param string $title Title of the feed
90
     */
91
    public function addRss($id, $url, $title)
92
    {
93
        $this->rss[$id] = array('url' => $url, 'title' => $title);
94
95
        return $this;
96
    }
97
98
    protected function renderRss()
99
    {
100
        $output = '';
101
        foreach ($this->rss as $id => $rss) {
102
            $output .= '<link rel="alternate"';
103
            $output .= ' id="' . $id . '"';
104
            $output .= ' href="' . $rss['url'] . '"';
105
            $output .= ' type="application/rss+xml"';
106
            $output .= ' media="' . htmlentities($rss['title'], ENT_COMPAT, $this->encoding) . '"';
107
            $output .= '/>' . "\n";
108
        }
109
        return $output;
110
    }
111
112
    //
113
    // Scripts
114
    //
115
    public function addScript($id, $url)
116
    {
117
        $this->scripts[$id] = $url;
118
119
        return $this;
120
    }
121
122
    protected function renderScripts()
123
    {
124
        $output = '';
125
        
126
        foreach ($this->scripts as $currentScriptUrl) {
127
            $output .= '<script type="text/javascript" src="' . $currentScriptUrl . '"></script>' . "\n";
128
        }
129
130
        return $output;
131
    }
132
133
    //
134
    // Metas
135
    //
136
    public function addMeta($name, $content)
137
    {
138
        $this->metas[$name] = array('content' => $content, 'type' => 'name');
139
140
        return $this;
141
    }
142
143
    public function addMetaProperty($name, $content)
144
    {
145
        $this->metas[$name] = array('content' => $content, 'type' => 'property');
146
    }
147
148
    public function addMetaLink($name, $type, $href)
149
    {
150
        $this->metas[$name] = array('href' => $href, 'type' => 'rel', 'relType' => $type);
151
    }
152
153
    protected function renderMetas()
154
    {
155
        $output = '';
156
        foreach ($this->metas as $name => $metaData) {
157
            if ($metaData['type'] == 'name') {
158
                $output .= '<meta name="' . $name . '" content="' . $metaData['content'] . '"/>' . "\n";
159
            } elseif ($metaData['type'] == 'property') {
160
                $output .= '<meta property="' . $name . '" content="' . $metaData['content'] . '"/>' . "\n";
161
            } elseif ($metaData['type'] == 'rel') {
162
                $output .= '<link rel="' . $metaData['relType'] . '" href="' . $metaData['href'] . '"/>'."\n";
163
            }
164
        }
165
166
        return $output;
167
    }
168
169
    public function render($content = '')
170
    {
171
        $htmlClass = count($this->htmlClass) ? ' class="' . implode(' ', array_keys($this->htmlClass)) .'"' : '';
172
        $output  = '<!DOCTYPE html>' . "\n";
173
        $output .= '<html lang="' . substr($this->language, 0, 2) . '"' . $htmlClass . '>' . "\n";
174
        $output .= '    <head>' . "\n";
175
        $output .= '        <title>' . $this->title . '</title>' . "\n";
176
        $output .= '        <meta http-equiv="Content-Type" content="text/html; charset=' . $this->encoding . '" />'."\n";
177
        $output .=          $this->renderMetas();
178
        $output .=          $this->renderStylesheets();
179
        $output .=          $this->renderScripts();
180
        $output .=          $this->renderRss();
181
        $output .= '    </head>' . "\n";
182
        $output .= '    <body>' . "\n";
183
        $output .= $content;
184
        $output .= '    </body>'."\n";
185
        $output .= '</html>' . "\n";
186
187
        return $output;
188
    }
189
}
190