Passed
Pull Request — master (#7)
by Dante
02:14
created

HtmlHelper::metaTwitter()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 1
dl 0
loc 14
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * BEdita, API-first content management framework
4
 * Copyright 2018 ChannelWeb Srl, Chialab Srl
5
 *
6
 * This file is part of BEdita: you can redistribute it and/or modify
7
 * it under the terms of the GNU Lesser General Public License as published
8
 * by the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details.
12
 */
13
14
namespace BEdita\WebTools\View\Helper;
15
16
use Cake\Core\Configure;
17
use Cake\Utility\Inflector;
18
use Cake\View\Helper\HtmlHelper as CakeHtmlHelper;
19
20
/**
21
 * Html helper.
22
 * It extends {@see \Cake\View\Helper\HtmlHelper} Cake Html Helper
23
 */
24
class HtmlHelper extends CakeHtmlHelper
25
{
26
    /**
27
     * Title for template pages
28
     * If `_title` view var is set, return it
29
     * Otherwise return controller name (and action name if set)
30
     *
31
     * @return string
32
     */
33
    public function title() : string
34
    {
35
        if (isset($this->getView()->viewVars['_title'])) {
36
            return $this->getView()->viewVars['_title'];
37
        }
38
        $title = Inflector::humanize($this->getView()->request->getParam('controller', ''));
39
        $suffix = Inflector::humanize($this->getView()->request->getParam('action', ''));
40
        if (empty($title)) {
41
            $title = $suffix;
42
        } elseif (!empty($suffix)) {
43
            $title .= sprintf(' - %s', $suffix);
44
        }
45
46
        return $title;
47
    }
48
49
    /**
50
     * Html meta: description, content, author, css, generator
51
     *
52
     * @param array $data Data for meta: 'description', 'author', 'docType', 'project', 'theme-color'
53
     * @return string
54
     * @see HtmlHelper
55
     */
56
    public function metaAll(array $data) : string
57
    {
58
        $html = '';
59
60
        // description
61
        if (!empty($data['description'])) {
62
            $html .= $this->metaDescription($data['description']);
63
        }
64
65
        // author
66
        if (!empty($data['author'])) {
67
            $html .= $this->metaAuthor($data['author']);
68
        }
69
70
        // viewport, msapplication-TileColor, theme-color
71
        foreach (['viewport', 'msapplication-TileColor', 'theme-color'] as $attribute) {
72
            if (!empty($data[$attribute])) {
73
                $html .= $this->meta([
74
                    'name' => $attribute,
75
                    'content' => $data[$attribute],
76
                ]);
77
            }
78
        }
79
80
        // css
81
        $docType = '';
82
        if (!empty($data['docType'])) {
83
            $docType = $data['docType'];
84
        } else {
85
            $docType = Configure::read('docType');
86
            if (empty($docType)) {
87
                $docType = 'xhtml-strict';
88
            }
89
        }
90
        $html .= $this->metaCss($docType);
91
92
        // generator
93
        $project = [];
94
        if (!empty($data['project'])) {
95
            $project = $data['project'];
96
        }
97
        $html .= $this->metaGenerator($project);
98
99
        return $html;
100
    }
101
102
    /**
103
     * Return html meta description tag for passed description argument
104
     *
105
     * @param string|null $description The description
106
     * @return string
107
     */
108
    public function metaDescription($description) : string
109
    {
110
        if (empty($description)) {
111
            return '';
112
        }
113
        $html = $this->meta('description', h(strip_tags($description)));
114
        if ($html == null) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $html of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
115
            $html = '';
116
        }
117
118
        return $html;
119
    }
120
121
    /**
122
     * Return html meta author tag for passed creator argument
123
     *
124
     * @param string|null $creator The content creator
125
     * @return string
126
     */
127
    public function metaAuthor(?string $creator) : string
128
    {
129
        if (empty($creator)) {
130
            return '';
131
        }
132
        $html = $this->meta([
133
            'name' => 'author',
134
            'content' => h($creator),
135
        ]);
136
        if ($html == null) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $html of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
137
            $html = '';
138
        }
139
140
        return $html;
141
    }
142
143
    /**
144
     * Return html meta css tag for passed doc type
145
     *
146
     * @param string $docType The doc type
147
     * @return string
148
     */
149
    public function metaCss(string $docType) : string
150
    {
151
        if ($docType === 'html5') {
152
            return '';
153
        }
154
        $html = $this->meta([
155
            'http-equiv' => 'Content-Style-Type',
156
            'content' => 'text/css',
157
        ]);
158
        if ($html == null) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $html of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
159
            $html = '';
160
        }
161
162
        return $html;
163
    }
164
165
    /**
166
     * Return html meta for generator by project name and version passed
167
     *
168
     * @param array $project The project data ('name', 'version')
169
     * @return string
170
     */
171
    public function metaGenerator(array $project) : string
172
    {
173
        if (empty($project) || empty($project['name'])) {
174
            return '';
175
        }
176
        $version = '';
177
        if (!empty($project['version'])) {
178
            $version = $project['version'];
179
        }
180
        $html = $this->meta([
181
            'name' => 'generator',
182
            'content' => trim(sprintf('%s %s', $project['name'], $version)),
183
        ]);
184
        if ($html == null) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $html of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
185
            $html = '';
186
        }
187
188
        return $html;
189
    }
190
191
    /**
192
     * Return html meta for opengraph / facebook
193
     * OG fields:
194
     *
195
     *  - og:title
196
     *  - og:type
197
     *  - og:url
198
     *  - og:image
199
     *
200
     * OG optional fields:
201
     *
202
     *  - og:audio
203
     *  - og:description
204
     *  - og:determiner
205
     *  - og:locale
206
     *  - og:locale:alternate
207
     *  - og:site_name
208
     *  - og:video
209
     *
210
     * OG structured fields:
211
     *
212
     *  - og:image:url // identical to og:image
213
     *  - og:image:secure_url
214
     *  - og:image:type
215
     *  - og:image:width
216
     *  - og:image:height
217
     *  - og:image:alt
218
     *  - og:video:url // identical to og:video
219
     *  - og:video:secure_url
220
     *  - og:video:type
221
     *  - og:video:width
222
     *  - og:video:height
223
     *  - og:audio
224
     *  - og:secure_url
225
     *  - og:type
226
     *
227
     * For details @see http://ogp.me
228
     *
229
     * @param array $data The data ('title', 'type', 'image', 'url')
230
     * @return string
231
     */
232
    public function metaOpenGraph(array $data) : string
233
    {
234
        $html = '';
235
        foreach ($data as $attribute => $val) {
236
            $tmp = $this->meta([
237
                'property' => sprintf('og:%s', $attribute),
238
                'content' => $val,
239
            ]);
240
            if ($tmp != null) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $tmp of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
241
                $html .= $tmp;
242
            }
243
        }
244
245
        return $html;
246
    }
247
248
    /**
249
     * Return html meta for twitter
250
     * twitter fields:
251
     *
252
     *  - twitter:card
253
     *  - twitter:site
254
     *  - twitter:site:id
255
     *  - twitter:creator
256
     *  - twitter:creator:id
257
     *  - twitter:description
258
     *  - twitter:title
259
     *  - twitter:image
260
     *  - twitter:image:alt
261
     *  - twitter:player
262
     *  - twitter:player:width
263
     *  - twitter:player:height
264
     *  - twitter:player:stream
265
     *  - twitter:app:name:iphone
266
     *  - twitter:app:id:iphone
267
     *  - twitter:app:url:iphone
268
     *  - twitter:app:name:ipad
269
     *  - twitter:app:id:ipad
270
     *  - twitter:app:url:ipad
271
     *  - twitter:app:name:googleplay
272
     *  - twitter:app:id:googleplay
273
     *  - twitter:app:url:googleplay
274
     *
275
     * For details @see https://developer.twitter.com/en/docs/tweets/optimize-with-cards/overview/markup.html
276
     *
277
     * @param array $data The data ('card', 'site', 'creator', 'title', 'description', 'image')
278
     * @return string
279
     */
280
    public function metaTwitter(array $data) : string
281
    {
282
        $html = '';
283
        foreach ($data as $attribute => $val) {
284
            $tmp = $this->meta([
285
                'property' => sprintf('twitter:%s', $attribute),
286
                'content' => $val,
287
            ]);
288
            if ($tmp != null) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $tmp of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
289
                $html .= $tmp;
290
            }
291
        }
292
293
        return $html;
294
    }
295
}
296