Completed
Push — master ( 52ae33...a6e2d9 )
by Petro
03:49
created

Content::generateCommentBlock()   B

Complexity

Conditions 8
Paths 65

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 8

Importance

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