Completed
Push — master ( 517487...35397e )
by Petro
10:10 queued 04:42
created

Content::gallery()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.9332
c 0
b 0
f 0
cc 3
nc 4
nop 2
crap 3
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 images gallery
81
     * @param array $imagesArray Array of images urls
82
     * ['http://example.com/image1.jpg', 'http://example.com/image2.jpg']
83
     * @param string|null $header
84
     * @return string
85
     */
86 12
    public static function gallery(array $imagesArray, $header = null)
87
    {
88 12
        $galleryString = $header ? '<header>' . $header . '</header>' : '';
89
90 12
        foreach ($imagesArray as $image) {
91 12
            $galleryString .= '<img src="' . $image . '" />';
92 6
        }
93
94 12
        return '<div data-block="gallery">' . $galleryString . '</div>';
95
    }
96
97
    /**
98
     * Generate share block
99
     * @param array|null $networks Array of network names
100
     * [Content::SHARE_TYPE_GOOGLE, Content::SHARE_TYPE_TWITTER]
101
     * Can be empty, in this way all possible network types will be showed.
102
     * @return string
103
     */
104 12
    public static function share(array $networks = null)
105
    {
106 6
        $networksString = $networks
107 9
            ? 'data-network="' . implode(',', $networks) . '"'
108 12
            : '';
109
110 12
        return '<div data-block="share" ' . $networksString . '></div>';
111
    }
112
113
    /**
114
     * Generate rating block
115
     * @param float $currentRating
116
     * @param float $maxRating
117
     * @return string
118
     * @throws \UnexpectedValueException
119
     */
120 24
    public static function rating($currentRating, $maxRating)
121
    {
122 24
        if (($currentRating > $maxRating) || ($maxRating <= 0) || ($currentRating < 0)) {
123 18
            throw new \UnexpectedValueException("Current rating can't be bigger than max value. And max value must be bigger than 0.");
124
        }
125
126
        return '<div itemscope="" itemtype="http://schema.org/Rating">
127 6
                       <meta itemprop="ratingValue" content="' . $currentRating . '" />
128 6
                       <meta itemprop="bestRating" content="' . $maxRating . '" />
129 3
                </div>';
130
    }
131
132
    /**
133
     * Generate button
134
     * @param string $text
135
     * @param string $url
136
     * @param string $phone Phone number in RFC-3966 format
137
     * @param string|null $buttonColor Can be Text or HEX
138
     * @param string|null $textColor Can be Text or HEX
139
     * @param bool $isBoldText
140
     * @param bool $isDisabled
141
     * @return string
142
     * @throws \UnexpectedValueException
143
     */
144 24
    public static function button($text, $url = '', $phone = '',
145
                                  $buttonColor = null, $textColor = null,
146
                                  $isBoldText = false, $isDisabled = false)
147
    {
148 24
        if (!$url && !$phone) {
149 6
            throw new \UnexpectedValueException('Please set url or phone number for button');
150
        }
151
152 18
        $formAction = $url ? $url : 'tel:' . $phone;
153 18
        $buttonColorString = $buttonColor ? 'data-background-color="' . $buttonColor . '"' : '';
154 18
        $textColorString   = $textColor   ? 'data-color="' . $textColor . '"' : '';
155 18
        $isBoldTextString  = $isBoldText  ? 'data-primary="true"' : '';
156 18
        $isDisabledString  = $isDisabled  ? 'disabled="true"' : '';
157
158
        return "<button
159 18
                    formaction=\"" . $formAction . "\"
160 18
                    " . $buttonColorString . "
161 18
                    " . $textColorString . "
162 18
                    " . $isBoldTextString . "
163 18
                    " . $isDisabledString . ">" . $text . "</button>";
164
    }
165
166
    /**
167
     * Generate comment block
168
     * @param string $url URL to comments page
169
     * @param array $commentsArray multidimensional or one-dimensional array of comments,
170
     * can has unlimited includes, example:
171
     * [
172
     *  [
173
     *      'author' => 'First Author Name',
174
     *      'avatar' => 'http://example.com/user1.jpg',
175
     *      'title' => 'Comment Title',
176
     *      'subtitle' => '2017-12-10',
177
     *      'content' => 'Somme comment text',
178
     *      'comments' => [
179
     *          [
180
     *              'author' => 'Third Author Name',
181
     *              'avatar' => 'http://example.com/user3.jpg',
182
     *              'title' => 'Comment Title',
183
     *              'subtitle' => '2017-12-12',
184
     *              'content' => 'Some answer text'
185
     *          ],
186
     *          [
187
     *              'author' => 'Another Author Name',
188
     *              'avatar' => 'http://example.com/user4.jpg',
189
     *              'title' => 'Comment Title',
190
     *              'subtitle' => '2017-12-13',
191
     *              'content' => 'Another answer text'
192
     *          ],
193
     *      ]
194
     *  ],
195
     *  [
196
     *      'author' => 'Second Author Name',
197
     *      'avatar' => 'http://example.com/user2.jpg',
198
     *      'title' => 'Comment Title',
199
     *      'subtitle' => '2017-12-11',
200
     *      'content' => 'Some comment text'
201
     *  ],
202
     * ]
203
     * @return string
204
     */
205 6
    public static function comment($url, array $commentsArray)
206
    {
207 6
        $commentBlock = self::generateCommentBlock($commentsArray);
208
209 6
        return '<div data-block="comments" data-url="' . $url . '">' . $commentBlock . '</div>';
210
    }
211
212
    /**
213
     * Generate accordion
214
     * @param array $accordionArray array accordion elements
215
     * [
216
     *     ['title' => 'Page title 1', 'text' => 'Text 1'],
217
     *     ['title' => 'Page title 2', 'text' => 'Text 2', 'expanded' => true],
218
     * ]
219
     * @return string
220
     */
221 6
    public static function accordion(array $accordionArray)
222
    {
223 6
        $accordionString = '<div data-block="accordion">';
224
225 6
        foreach ($accordionArray as $item) {
226 6
            $expanded = isset($item['expanded']) && $item['expanded'] ? ' data-expanded="true"' : '';
227 6
            $accordionString .= '<div data-block="item" data-title="' . $item['title'] . '"' . $expanded . '>' . $item['text'] . '</div>';
228 3
        }
229
230 6
        $accordionString .= '</div>';
231
232 6
        return $accordionString;
233
    }
234
235
    /**
236
     * Generate media slider
237
     * @param array $itemsArray Array of items with data
238
     * [
239
     *     ['url' => 'http://example.com/image1.jpg', 'title' => 'Image title 1', 'link' => ''],
240
     *     ['url' => 'http://example.com/image2.jpg', 'title' => 'Image title 2', 'link' => ''],
241
     *     ['url' => 'http://example.com/image3.jpg'],
242
     *     ['href' => 'http://example.com/page1.html', 'title' => 'Link title 1', 'text' => 'Link text 1']
243
     * ]
244
     * @param string|null $header
245
     * @param string $dataView
246
     * @param string $dataItemView
247
     * @return string
248
     */
249 12
    public static function slider(array $itemsArray, $header = null,
250
                                  $dataView = self::SLIDER_DATA_VIEW_SQUARE,
251
                                  $dataItemView = self::SLIDER_DATA_ITEM_VIEW_COVER)
252
    {
253 12
        $sliderString = $header ? '<header>' . $header . '</header>' : '';
254
255 12
        $sliderString .= self::generateSliderItemsBlock($itemsArray);
256
257 12
        return '<div data-block="slider" data-view="' . $dataView . '" data-item-view="'
258 12
            . $dataItemView . '">' . $sliderString . '</div>';
259
    }
260
261
    /**
262
     * Generate Ad block position element
263
     * @param string $turboAdId value of $turboAdId used in Channel() class
264
     * @return string
265
     *
266
     * @see Channel::$adTurboAdId
267
     */
268 6
    public static function adBlockPosition($turboAdId)
269
    {
270 6
        return '<figure data-turbo-ad-id="' . $turboAdId . '"></figure>';
271
    }
272
273
    /**
274
     * Generate content block for media slider
275
     * @param array $itemsArray Array of items with data
276
     * [
277
     *     ['url' => 'http://example.com/image1.jpg', 'title' => 'Image title 1', 'link' => ''],
278
     *     ['url' => 'http://example.com/image2.jpg', 'title' => 'Image title 2', 'link' => ''],
279
     *     ['url' => 'http://example.com/image3.jpg'],
280
     *     ['href' => 'http://example.com/page1.html', 'title' => 'Link title 1', 'text' => 'Link text 1']
281
     * ]
282
     * @return string
283
     */
284 12
    private static function generateSliderItemsBlock(array $itemsArray)
285
    {
286 12
        $sliderString = '';
287
288 12
        foreach ($itemsArray as $item) {
289 12
            $sliderString .= '<figure>';
290
291 12
            if (isset($item['title'])) {
292 12
                $sliderString .= '<figcaption>' . $item['title'] . '</figcaption>';
293 6
            }
294
295 12
            if (isset($item['url'])) {
296 12
                $sliderString .= '<img src="' . $item['url'] . '" />';
297 12
            } elseif (isset($item['href'])) {
298 12
                $sliderString .= '<a href="' . $item['href'] . '">' . $item['text'] . '</a>';
299 6
            }
300
301 12
            $sliderString .= '</figure>';
302 6
        }
303
304 12
        return $sliderString;
305
    }
306
307 6
    private static function generateCommentBlock(array $commentsArray)
308
    {
309 6
        $commentBlock = '';
310
311 6
        foreach ($commentsArray as $commentArray) {
312 6
            $author = isset($commentArray['author']) ? 'data-author="' . $commentArray['author'] . '"' : '';
313 6
            $avatar = isset($commentArray['avatar']) ? 'data-avatar-url="' . $commentArray['avatar'] . '"' : '';
314 6
            $subtitle = isset($commentArray['subtitle']) ? 'data-subtitle="' . $commentArray['subtitle'] . '"' : '';
315
316
            $commentBlock .= '<div
317
                        data-block="comment"
318 6
                        ' . $author . ' 
319 6
                        ' . $avatar . '
320 6
                        ' . $subtitle . '                         
321 3
                        ><div data-block="content">';
322
323 6
            $commentBlock .= isset($commentArray['title']) ? '<header>' . $commentArray['title'] . '</header>' : '';
324 6
            $commentBlock .= isset($commentArray['content']) ? '<p>' . $commentArray['content'] . '</p></div>' : '';
325
326 6
            if (isset($commentArray['comments'])) {
327 6
                $commentBlock .= '<div data-block="comments">';
328 6
                $commentBlock .= self::generateCommentBlock($commentArray['comments']);
329 6
                $commentBlock .= '</div>';
330 3
            }
331
332 6
            $commentBlock .= '</div>';
333 3
        }
334
335 6
        return $commentBlock;
336
    }
337
338
    /**
339
     * Generate header menu
340
     * @param array $menuArray array of arrays with pairs of url and title
341
     * [
342
     *     ['url' => 'http://example/page1.html', 'title' => 'Page title 1'],
343
     *     ['url' => 'http://example/page2.html', 'title' => 'Page title 2'],
344
     * ]
345
     * @return string
346
     */
347 6
    private static function generateMenu(array $menuArray)
348
    {
349 6
        $menuString = '';
350
351 6
        foreach ($menuArray as $menuItem) {
352 6
            $menuString .= '<a href="' . $menuItem['url'] . '">' . $menuItem['title'] . '</a>';
353 3
        }
354
355 6
        return '<menu>' . $menuString . '</menu>';
356
    }
357
}