Completed
Push — master ( 7c4e78...3f5e02 )
by Petro
11:56
created

Content::ownVideo()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 3
nc 4
nop 4
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 OWN_VIDEO_TYPE_MP4 = 'video/mp4';
15
16
    /**
17
     * Generate header element
18
     * @param string $h1
19
     * @param string|null $h2
20
     * @param string|null $imgUrl
21
     * @param string|null $imgCaption
22
     * @param array|null $menuArray array of arrays with pairs of url and content
23
     * [
24
     *     ['url' => 'http://example/page1.html', 'title' => 'Page title 1'],
25
     *     ['url' => 'http://example/page2.html', 'title' => 'Page title 2'],
26
     * ]
27
     * @return string
28
     */
29 18
    public static function header($h1, $h2 = null, $imgUrl = null,
30
        $imgCaption = null, array $menuArray = null)
31
    {
32 18
        $header = '<h1>' . $h1 . '</h1>';
33 18
        $header .= $h2 ? '<h2>' . $h2 . '</h2>' : '';
34 18
        $header .= $menuArray ? self::generateMenu($menuArray) : '';
35 18
        $header .= $imgUrl ? self::img($imgUrl, $imgCaption) : '';
36
37 18
        return '<header>' . $header . '</header>';
38
    }
39
40
    /**
41
     * Generate image element
42
     * @param string $imgUrl
43
     * @param string|null $imgCaption
44
     * @return string
45
     */
46 18
    public static function img($imgUrl, $imgCaption = null)
47
    {
48 18
        $imageString = '<img src="' . $imgUrl . '" />';
49
50 18
        $imageString .= $imgCaption ? '<figcaption>' . $imgCaption . '</figcaption>' : '';
51
52 18
        return '<figure>' . $imageString . '</figure>';
53
    }
54
55
    /**
56
     * Generate video element
57
     * @param string $videoUrl
58
     * @param string|null $videoCaption
59
     * @param string $imgUrl
60
     * @param string $type
61
     * @return string
62
     */
63 12
    public static function ownVideo($videoUrl, $videoCaption = null, $type = self::OWN_VIDEO_TYPE_MP4, $imgUrl = null)
64
    {
65 12
        $videoString = '<video><source src="' . $videoUrl . '" type="' . $type . '" /></video>';
66 12
        $videoString .= $imgUrl ? '<img src="' . $imgUrl . '" />' : '';
67 12
        $videoString .= $videoCaption ? '<figcaption>' . $videoCaption . '</figcaption>' : '';
68
69 12
        return '<figure>' . $videoString . '</figure>';
70
    }
71
72
    /**
73
     * Generate images gallery
74
     * @param array $imagesArray Array of images urls
75
     * ['http://example.com/image1.jpg', 'http://example.com/image2.jpg']
76
     * @param string|null $header
77
     * @return string
78
     */
79 12
    public static function gallery(array $imagesArray, $header = null)
80
    {
81 12
        $galleryString = $header ? '<header>' . $header . '</header>' : '';
82
83 12
        foreach ($imagesArray as $image) {
84 12
            $galleryString .= '<img src="' . $image . '" />';
85 6
        }
86
87 12
        return '<div data-block="gallery">' . $galleryString . '</div>';
88
    }
89
90
    /**
91
     * Generate share block
92
     * @param array|null $networks Array of network names
93
     * [Content::SHARE_TYPE_GOOGLE, Content::SHARE_TYPE_TWITTER]
94
     * Can be empty, in this way all possible network types will be showed.
95
     * @return string
96
     */
97 12
    public static function share(array $networks = null)
98
    {
99 6
        $networksString = $networks
100 9
            ? 'data-network="' . implode(',', $networks) . '"'
101 12
            : '';
102
103 12
        return '<div data-block="share" ' . $networksString . '></div>';
104
    }
105
106
    /**
107
     * Generate rating block
108
     * @param float $currentRating
109
     * @param float $maxRating
110
     * @return string
111
     */
112 24
    public static function rating($currentRating, $maxRating)
113
    {
114 24
        if (($currentRating > $maxRating) || ($maxRating <= 0) || ($currentRating < 0)) {
115 18
            throw new \UnexpectedValueException("Current rating can't be bigger than max value. And max value must be bigger than 0.");
116
        }
117
118
        return '<div itemscope="" itemtype="http://schema.org/Rating">
119 6
                       <meta itemprop="ratingValue" content="' . $currentRating . '" />
120 6
                       <meta itemprop="bestRating" content="' . $maxRating . '" />
121 3
                </div>';
122
    }
123
124
    /**
125
     * Generate button
126
     * @param string $text
127
     * @param string $url
128
     * @param string $phone Phone number in RFC-3966 format
129
     * @param string|null $buttonColor Can be Text or HEX
130
     * @param string|null $textColor Can be Text or HEX
131
     * @param bool $isBoldText
132
     * @param bool $isDisabled
133
     * @return string
134
     */
135 24
    public static function button($text, $url = '', $phone = '',
136
                                  $buttonColor = null, $textColor = null,
137
                                  $isBoldText = false, $isDisabled = false)
138
    {
139 24
        if (!$url && !$phone) {
140 6
            throw new \UnexpectedValueException('Please set url or phone number for button');
141
        }
142
143 18
        $formAction = $url ? $url : 'tel:' . $phone;
144 18
        $buttonColorString = $buttonColor ? 'data-background-color="' . $buttonColor . '"' : '';
145 18
        $textColorString   = $textColor   ? 'data-color="' . $textColor . '"' : '';
146 18
        $isBoldTextString  = $isBoldText  ? 'data-primary="true"' : '';
147 18
        $isDisabledString  = $isDisabled  ? 'disabled="true"' : '';
148
149
        return "<button
150 18
                    formaction=\"" . $formAction . "\"
151 18
                    " . $buttonColorString . "
152 18
                    " . $textColorString . "
153 18
                    " . $isBoldTextString . "
154 18
                    " . $isDisabledString . ">" . $text . "</button>";
155
    }
156
157
    /**
158
     * Generate comment block
159
     * @param string $url URL to comments page
160
     * @param array $commentsArray multidimensional or one-dimensional array of comments,
161
     * can has unlimited includes, example:
162
     * [
163
     *  [
164
     *      'author' => 'First Author Name',
165
     *      'avatar' => 'http://example.com/user1.jpg',
166
     *      'title' => 'Comment Title',
167
     *      'subtitle' => '2017-12-10',
168
     *      'content' => 'Somme comment text',
169
     *      'comments' => [
170
     *          [
171
     *              'author' => 'Third Author Name',
172
     *              'avatar' => 'http://example.com/user3.jpg',
173
     *              'title' => 'Comment Title',
174
     *              'subtitle' => '2017-12-12',
175
     *              'content' => 'Some answer text'
176
     *          ],
177
     *          [
178
     *              'author' => 'Another Author Name',
179
     *              'avatar' => 'http://example.com/user4.jpg',
180
     *              'title' => 'Comment Title',
181
     *              'subtitle' => '2017-12-13',
182
     *              'content' => 'Another answer text'
183
     *          ],
184
     *      ]
185
     *  ],
186
     *  [
187
     *      'author' => 'Second Author Name',
188
     *      'avatar' => 'http://example.com/user2.jpg',
189
     *      'title' => 'Comment Title',
190
     *      'subtitle' => '2017-12-11',
191
     *      'content' => 'Some comment text'
192
     *  ],
193
     * ]
194
     * @return string
195
     */
196 6
    public static function comment($url, array $commentsArray)
197
    {
198 6
        $commentBlock = self::generateCommentBlock($commentsArray);
199
200 6
        return '<div data-block="comments" data-url="' . $url . '">' . $commentBlock . '</div>';
201
    }
202
203
    /**
204
     * Generate accordion
205
     * @param array $accordionArray array accordion elements
206
     * [
207
     *     ['title' => 'Page title 1', 'text' => 'Text 1'],
208
     *     ['title' => 'Page title 2', 'text' => 'Text 2', 'expanded' => true],
209
     * ]
210
     * @return string
211
     */
212 6
    public static function accordion(array $accordionArray)
213
    {
214 6
        $accordionString = '<div data-block="accordion">';
215
216 6
        foreach ($accordionArray as $item) {
217 6
            $expanded = isset($item['expanded']) && $item['expanded'] ? ' data-expanded="true"' : '';
218 6
            $accordionString .= '<div data-block="item" data-title="' . $item['title'] . '"' . $expanded . '>' . $item['text'] . '</div>';
219 3
        }
220
221 6
        $accordionString .= '</div>';
222
223 6
        return $accordionString;
224
    }
225
226 6
    private static function generateCommentBlock(array $commentsArray)
227
    {
228 6
        $commentBlock = '';
229
230 6
        foreach ($commentsArray as $commentArray) {
231 6
            $author = isset($commentArray['author']) ? 'data-author="' . $commentArray['author'] . '"' : '';
232 6
            $avatar = isset($commentArray['avatar']) ? 'data-avatar-url="' . $commentArray['avatar'] . '"' : '';
233 6
            $subtitle = isset($commentArray['subtitle']) ? 'data-subtitle="' . $commentArray['subtitle'] . '"' : '';
234
235
            $commentBlock .= '<div
236
                        data-block="comment"
237 6
                        ' . $author . ' 
238 6
                        ' . $avatar . '
239 6
                        ' . $subtitle . '                         
240 3
                        ><div data-block="content">';
241
242 6
            $commentBlock .= isset($commentArray['title']) ? '<header>' . $commentArray['title'] . '</header>' : '';
243 6
            $commentBlock .= isset($commentArray['content']) ? '<p>' . $commentArray['content'] . '</p></div>' : '';
244
245 6
            if (isset($commentArray['comments'])) {
246 6
                $commentBlock .= '<div data-block="comments">';
247 6
                $commentBlock .= self::generateCommentBlock($commentArray['comments']);
248 6
                $commentBlock .= '</div>';
249 3
            }
250
251 6
            $commentBlock .= '</div>';
252 3
        }
253
254 6
        return $commentBlock;
255
    }
256
257
    /**
258
     * Generate header menu
259
     * @param array $menuArray array of arrays with pairs of url and title
260
     * [
261
     *     ['url' => 'http://example/page1.html', 'title' => 'Page title 1'],
262
     *     ['url' => 'http://example/page2.html', 'title' => 'Page title 2'],
263
     * ]
264
     * @return string
265
     */
266 6
    private static function generateMenu(array $menuArray)
267
    {
268 6
        $menuString = '';
269
270 6
        foreach ($menuArray as $menuItem) {
271 6
            $menuString .= '<a href="' . $menuItem['url'] . '">' . $menuItem['title'] . '</a>';
272 3
        }
273
274 6
        return '<menu>' . $menuString . '</menu>';
275
    }
276
}