Completed
Push — master ( 35397e...e3c112 )
by Petro
07:22
created

Content::externalVideo()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 7
cts 7
cp 1
rs 9.8666
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2
1
<?php
2
3
namespace sokolnikov911\YandexTurboPages\helpers;
4
5
class Content
6
{
7
    const SHARE_TYPE_FACEBOOK  = 'facebook';
8
    const SHARE_TYPE_GOOGLE    = 'google';
9
    const SHARE_TYPE_ODNOKLASSNIKI = 'odnoklassniki';
10
    const SHARE_TYPE_TELEGRAM  = 'telegram';
11
    const SHARE_TYPE_TWITTER   = 'twitter';
12
    const SHARE_TYPE_VKONTAKTE = 'vkontakte';
13
14
    const SLIDER_DATA_VIEW_SQUARE = 'square';
15
    const SLIDER_DATA_VIEW_PORTRAIT = 'portrait';
16
    const SLIDER_DATA_VIEW_LANDSCAPE = 'landscape';
17
18
    const SLIDER_DATA_ITEM_VIEW_COVER = 'cover';
19
    const SLIDER_DATA_ITEM_VIEW_CONTAIN = 'contain';
20
21
    const OWN_VIDEO_TYPE_MP4 = 'video/mp4';
22
23
    /**
24
     * Generate header element
25
     * @param string $h1
26
     * @param string|null $h2
27
     * @param string|null $imgUrl
28
     * @param string|null $imgCaption
29
     * @param array|null $menuArray array of arrays with pairs of url and content
30
     * [
31
     *     ['url' => 'http://example/page1.html', 'title' => 'Page title 1'],
32
     *     ['url' => 'http://example/page2.html', 'title' => 'Page title 2'],
33
     * ]
34
     * @return string
35
     */
36 18
    public static function header($h1, $h2 = null, $imgUrl = null,
37
        $imgCaption = null, array $menuArray = null)
38
    {
39 18
        $header = '<h1>' . $h1 . '</h1>';
40 18
        $header .= $h2 ? '<h2>' . $h2 . '</h2>' : '';
41 18
        $header .= $menuArray ? self::generateMenu($menuArray) : '';
42 18
        $header .= $imgUrl ? self::img($imgUrl, $imgCaption) : '';
43
44 18
        return '<header>' . $header . '</header>';
45
    }
46
47
    /**
48
     * Generate image element
49
     * @param string $imgUrl
50
     * @param string|null $imgCaption
51
     * @return string
52
     */
53 18
    public static function img($imgUrl, $imgCaption = null)
54
    {
55 18
        $imageString = '<img src="' . $imgUrl . '" />';
56
57 18
        $imageString .= $imgCaption ? '<figcaption>' . $imgCaption . '</figcaption>' : '';
58
59 18
        return '<figure>' . $imageString . '</figure>';
60
    }
61
62
    /**
63
     * Generate video element
64
     * @param string $videoUrl
65
     * @param string|null $videoCaption
66
     * @param string $imgUrl
67
     * @param string $type
68
     * @return string
69
     */
70 12
    public static function ownVideo($videoUrl, $videoCaption = null, $type = self::OWN_VIDEO_TYPE_MP4, $imgUrl = null)
71
    {
72 12
        $videoString = '<video><source src="' . $videoUrl . '" type="' . $type . '" /></video>';
73 12
        $videoString .= $imgUrl ? '<img src="' . $imgUrl . '" />' : '';
74 12
        $videoString .= $videoCaption ? '<figcaption>' . $videoCaption . '</figcaption>' : '';
75
76 12
        return '<figure>' . $videoString . '</figure>';
77
    }
78
79
    /**
80
     * Generate video element for external video
81
     * @param string $videoUrl
82
     * @param array $options Options Array with next variables: width, height, frameborder, allowfullscreen,
83
     * referrerpolicy, sandbox, hd. Options example:
84
     * [
85
     *  'width' => 640,
86
     *  'height' => 480,
87
     *  'frameborder' => 1,
88
     *  'allowfullscreen' => 'true',
89
     *  'referrerpolicy' => 'unsafe-url',
90
     *  'sandbox' => 'allow-forms allow-modals',
91
     *  'hd' => 3
92
     * ]
93
     * @return string
94
     */
95 12
    public static function externalVideo($videoUrl, array $options = [])
96
    {
97 12
        $videoString = '<iframe src="' . $videoUrl . '"';
98
99 12
        foreach ($options as $key => $value) {
100 6
            $videoString .= ' ' . $key . '="' . $value . '"';
101 6
        }
102
103 12
        $videoString .= '></iframe>';
104
105 12
        return $videoString;
106
    }
107
108
    /**
109
     * Generate images gallery
110
     * @param array $imagesArray Array of images urls
111
     * ['http://example.com/image1.jpg', 'http://example.com/image2.jpg']
112
     * @param string|null $header
113
     * @return string
114
     */
115 12
    public static function gallery(array $imagesArray, $header = null)
116
    {
117 12
        $galleryString = $header ? '<header>' . $header . '</header>' : '';
118
119 12
        foreach ($imagesArray as $image) {
120 12
            $galleryString .= '<img src="' . $image . '" />';
121 6
        }
122
123 12
        return '<div data-block="gallery">' . $galleryString . '</div>';
124
    }
125
126
    /**
127
     * Generate share block
128
     * @param array|null $networks Array of network names
129
     * [Content::SHARE_TYPE_GOOGLE, Content::SHARE_TYPE_TWITTER]
130
     * Can be empty, in this way all possible network types will be showed.
131
     * @return string
132
     */
133 12
    public static function share(array $networks = null)
134
    {
135 6
        $networksString = $networks
136 9
            ? 'data-network="' . implode(',', $networks) . '"'
137 12
            : '';
138
139 12
        return '<div data-block="share" ' . $networksString . '></div>';
140
    }
141
142
    /**
143
     * Generate rating block
144
     * @param float $currentRating
145
     * @param float $maxRating
146
     * @return string
147
     * @throws \UnexpectedValueException
148
     */
149 24
    public static function rating($currentRating, $maxRating)
150
    {
151 24
        if (($currentRating > $maxRating) || ($maxRating <= 0) || ($currentRating < 0)) {
152 18
            throw new \UnexpectedValueException("Current rating can't be bigger than max value. And max value must be bigger than 0.");
153
        }
154
155
        return '<div itemscope="" itemtype="http://schema.org/Rating">
156 6
                       <meta itemprop="ratingValue" content="' . $currentRating . '" />
157 6
                       <meta itemprop="bestRating" content="' . $maxRating . '" />
158 3
                </div>';
159
    }
160
161
    /**
162
     * Generate button
163
     * @param string $text
164
     * @param string $url
165
     * @param string $phone Phone number in RFC-3966 format
166
     * @param string|null $buttonColor Can be Text or HEX
167
     * @param string|null $textColor Can be Text or HEX
168
     * @param bool $isBoldText
169
     * @param bool $isDisabled
170
     * @return string
171
     * @throws \UnexpectedValueException
172
     */
173 24
    public static function button($text, $url = '', $phone = '',
174
                                  $buttonColor = null, $textColor = null,
175
                                  $isBoldText = false, $isDisabled = false)
176
    {
177 24
        if (!$url && !$phone) {
178 6
            throw new \UnexpectedValueException('Please set url or phone number for button');
179
        }
180
181 18
        $formAction = $url ? $url : 'tel:' . $phone;
182 18
        $buttonColorString = $buttonColor ? 'data-background-color="' . $buttonColor . '"' : '';
183 18
        $textColorString   = $textColor   ? 'data-color="' . $textColor . '"' : '';
184 18
        $isBoldTextString  = $isBoldText  ? 'data-primary="true"' : '';
185 18
        $isDisabledString  = $isDisabled  ? 'disabled="true"' : '';
186
187
        return "<button
188 18
                    formaction=\"" . $formAction . "\"
189 18
                    " . $buttonColorString . "
190 18
                    " . $textColorString . "
191 18
                    " . $isBoldTextString . "
192 18
                    " . $isDisabledString . ">" . $text . "</button>";
193
    }
194
195
    /**
196
     * Generate comment block
197
     * @param string $url URL to comments page
198
     * @param array $commentsArray multidimensional or one-dimensional array of comments,
199
     * can has unlimited includes, example:
200
     * [
201
     *  [
202
     *      'author' => 'First Author Name',
203
     *      'avatar' => 'http://example.com/user1.jpg',
204
     *      'title' => 'Comment Title',
205
     *      'subtitle' => '2017-12-10',
206
     *      'content' => 'Somme comment text',
207
     *      'comments' => [
208
     *          [
209
     *              'author' => 'Third Author Name',
210
     *              'avatar' => 'http://example.com/user3.jpg',
211
     *              'title' => 'Comment Title',
212
     *              'subtitle' => '2017-12-12',
213
     *              'content' => 'Some answer text'
214
     *          ],
215
     *          [
216
     *              'author' => 'Another Author Name',
217
     *              'avatar' => 'http://example.com/user4.jpg',
218
     *              'title' => 'Comment Title',
219
     *              'subtitle' => '2017-12-13',
220
     *              'content' => 'Another answer text'
221
     *          ],
222
     *      ]
223
     *  ],
224
     *  [
225
     *      'author' => 'Second Author Name',
226
     *      'avatar' => 'http://example.com/user2.jpg',
227
     *      'title' => 'Comment Title',
228
     *      'subtitle' => '2017-12-11',
229
     *      'content' => 'Some comment text'
230
     *  ],
231
     * ]
232
     * @return string
233
     */
234 6
    public static function comment($url, array $commentsArray)
235
    {
236 6
        $commentBlock = self::generateCommentBlock($commentsArray);
237
238 6
        return '<div data-block="comments" data-url="' . $url . '">' . $commentBlock . '</div>';
239
    }
240
241
    /**
242
     * Generate accordion
243
     * @param array $accordionArray array accordion elements
244
     * [
245
     *     ['title' => 'Page title 1', 'text' => 'Text 1'],
246
     *     ['title' => 'Page title 2', 'text' => 'Text 2', 'expanded' => true],
247
     * ]
248
     * @return string
249
     */
250 6
    public static function accordion(array $accordionArray)
251
    {
252 6
        $accordionString = '<div data-block="accordion">';
253
254 6
        foreach ($accordionArray as $item) {
255 6
            $expanded = isset($item['expanded']) && $item['expanded'] ? ' data-expanded="true"' : '';
256 6
            $accordionString .= '<div data-block="item" data-title="' . $item['title'] . '"' . $expanded . '>' . $item['text'] . '</div>';
257 3
        }
258
259 6
        $accordionString .= '</div>';
260
261 6
        return $accordionString;
262
    }
263
264
    /**
265
     * Generate media slider
266
     * @param array $itemsArray Array of items with data
267
     * [
268
     *     ['url' => 'http://example.com/image1.jpg', 'title' => 'Image title 1', 'link' => ''],
269
     *     ['url' => 'http://example.com/image2.jpg', 'title' => 'Image title 2', 'link' => ''],
270
     *     ['url' => 'http://example.com/image3.jpg'],
271
     *     ['href' => 'http://example.com/page1.html', 'title' => 'Link title 1', 'text' => 'Link text 1']
272
     * ]
273
     * @param string|null $header
274
     * @param string $dataView
275
     * @param string $dataItemView
276
     * @return string
277
     */
278 12
    public static function slider(array $itemsArray, $header = null,
279
                                  $dataView = self::SLIDER_DATA_VIEW_SQUARE,
280
                                  $dataItemView = self::SLIDER_DATA_ITEM_VIEW_COVER)
281
    {
282 12
        $sliderString = $header ? '<header>' . $header . '</header>' : '';
283
284 12
        $sliderString .= self::generateSliderItemsBlock($itemsArray);
285
286 12
        return '<div data-block="slider" data-view="' . $dataView . '" data-item-view="'
287 12
            . $dataItemView . '">' . $sliderString . '</div>';
288
    }
289
290
    /**
291
     * Generate Ad block position element
292
     * @param string $turboAdId value of $turboAdId used in Channel() class
293
     * @return string
294
     *
295
     * @see Channel::$adTurboAdId
296
     */
297 6
    public static function adBlockPosition($turboAdId)
298
    {
299 6
        return '<figure data-turbo-ad-id="' . $turboAdId . '"></figure>';
300
    }
301
302
    /**
303
     * Generate content block for media slider
304
     * @param array $itemsArray Array of items with data
305
     * [
306
     *     ['url' => 'http://example.com/image1.jpg', 'title' => 'Image title 1', 'link' => ''],
307
     *     ['url' => 'http://example.com/image2.jpg', 'title' => 'Image title 2', 'link' => ''],
308
     *     ['url' => 'http://example.com/image3.jpg'],
309
     *     ['href' => 'http://example.com/page1.html', 'title' => 'Link title 1', 'text' => 'Link text 1']
310
     * ]
311
     * @return string
312
     */
313 12
    private static function generateSliderItemsBlock(array $itemsArray)
314
    {
315 12
        $sliderString = '';
316
317 12
        foreach ($itemsArray as $item) {
318 12
            $sliderString .= '<figure>';
319
320 12
            if (isset($item['title'])) {
321 12
                $sliderString .= '<figcaption>' . $item['title'] . '</figcaption>';
322 6
            }
323
324 12
            if (isset($item['url'])) {
325 12
                $sliderString .= '<img src="' . $item['url'] . '" />';
326 12
            } elseif (isset($item['href'])) {
327 12
                $sliderString .= '<a href="' . $item['href'] . '">' . $item['text'] . '</a>';
328 6
            }
329
330 12
            $sliderString .= '</figure>';
331 6
        }
332
333 12
        return $sliderString;
334
    }
335
336 6
    private static function generateCommentBlock(array $commentsArray)
337
    {
338 6
        $commentBlock = '';
339
340 6
        foreach ($commentsArray as $commentArray) {
341 6
            $author = isset($commentArray['author']) ? 'data-author="' . $commentArray['author'] . '"' : '';
342 6
            $avatar = isset($commentArray['avatar']) ? 'data-avatar-url="' . $commentArray['avatar'] . '"' : '';
343 6
            $subtitle = isset($commentArray['subtitle']) ? 'data-subtitle="' . $commentArray['subtitle'] . '"' : '';
344
345
            $commentBlock .= '<div
346
                        data-block="comment"
347 6
                        ' . $author . ' 
348 6
                        ' . $avatar . '
349 6
                        ' . $subtitle . '                         
350 3
                        ><div data-block="content">';
351
352 6
            $commentBlock .= isset($commentArray['title']) ? '<header>' . $commentArray['title'] . '</header>' : '';
353 6
            $commentBlock .= isset($commentArray['content']) ? '<p>' . $commentArray['content'] . '</p></div>' : '';
354
355 6
            if (isset($commentArray['comments'])) {
356 6
                $commentBlock .= '<div data-block="comments">';
357 6
                $commentBlock .= self::generateCommentBlock($commentArray['comments']);
358 6
                $commentBlock .= '</div>';
359 3
            }
360
361 6
            $commentBlock .= '</div>';
362 3
        }
363
364 6
        return $commentBlock;
365
    }
366
367
    /**
368
     * Generate header menu
369
     * @param array $menuArray array of arrays with pairs of url and title
370
     * [
371
     *     ['url' => 'http://example/page1.html', 'title' => 'Page title 1'],
372
     *     ['url' => 'http://example/page2.html', 'title' => 'Page title 2'],
373
     * ]
374
     * @return string
375
     */
376 6
    private static function generateMenu(array $menuArray)
377
    {
378 6
        $menuString = '';
379
380 6
        foreach ($menuArray as $menuItem) {
381 6
            $menuString .= '<a href="' . $menuItem['url'] . '">' . $menuItem['title'] . '</a>';
382 3
        }
383
384 6
        return '<menu>' . $menuString . '</menu>';
385
    }
386
}