Content::searchInput()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

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