Completed
Push — master ( 3f5e02...d6d2b2 )
by Petro
07:13
created

Content::adBlockPosition()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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
     * @throws \UnexpectedValueException
112
     */
113 24
    public static function rating($currentRating, $maxRating)
114
    {
115 24
        if (($currentRating > $maxRating) || ($maxRating <= 0) || ($currentRating < 0)) {
116 18
            throw new \UnexpectedValueException("Current rating can't be bigger than max value. And max value must be bigger than 0.");
117
        }
118
119
        return '<div itemscope="" itemtype="http://schema.org/Rating">
120 6
                       <meta itemprop="ratingValue" content="' . $currentRating . '" />
121 6
                       <meta itemprop="bestRating" content="' . $maxRating . '" />
122 3
                </div>';
123
    }
124
125
    /**
126
     * Generate button
127
     * @param string $text
128
     * @param string $url
129
     * @param string $phone Phone number in RFC-3966 format
130
     * @param string|null $buttonColor Can be Text or HEX
131
     * @param string|null $textColor Can be Text or HEX
132
     * @param bool $isBoldText
133
     * @param bool $isDisabled
134
     * @return string
135
     * @throws \UnexpectedValueException
136
     */
137 24
    public static function button($text, $url = '', $phone = '',
138
                                  $buttonColor = null, $textColor = null,
139
                                  $isBoldText = false, $isDisabled = false)
140
    {
141 24
        if (!$url && !$phone) {
142 6
            throw new \UnexpectedValueException('Please set url or phone number for button');
143
        }
144
145 18
        $formAction = $url ? $url : 'tel:' . $phone;
146 18
        $buttonColorString = $buttonColor ? 'data-background-color="' . $buttonColor . '"' : '';
147 18
        $textColorString   = $textColor   ? 'data-color="' . $textColor . '"' : '';
148 18
        $isBoldTextString  = $isBoldText  ? 'data-primary="true"' : '';
149 18
        $isDisabledString  = $isDisabled  ? 'disabled="true"' : '';
150
151
        return "<button
152 18
                    formaction=\"" . $formAction . "\"
153 18
                    " . $buttonColorString . "
154 18
                    " . $textColorString . "
155 18
                    " . $isBoldTextString . "
156 18
                    " . $isDisabledString . ">" . $text . "</button>";
157
    }
158
159
    /**
160
     * Generate comment block
161
     * @param string $url URL to comments page
162
     * @param array $commentsArray multidimensional or one-dimensional array of comments,
163
     * can has unlimited includes, example:
164
     * [
165
     *  [
166
     *      'author' => 'First Author Name',
167
     *      'avatar' => 'http://example.com/user1.jpg',
168
     *      'title' => 'Comment Title',
169
     *      'subtitle' => '2017-12-10',
170
     *      'content' => 'Somme comment text',
171
     *      'comments' => [
172
     *          [
173
     *              'author' => 'Third Author Name',
174
     *              'avatar' => 'http://example.com/user3.jpg',
175
     *              'title' => 'Comment Title',
176
     *              'subtitle' => '2017-12-12',
177
     *              'content' => 'Some answer text'
178
     *          ],
179
     *          [
180
     *              'author' => 'Another Author Name',
181
     *              'avatar' => 'http://example.com/user4.jpg',
182
     *              'title' => 'Comment Title',
183
     *              'subtitle' => '2017-12-13',
184
     *              'content' => 'Another answer text'
185
     *          ],
186
     *      ]
187
     *  ],
188
     *  [
189
     *      'author' => 'Second Author Name',
190
     *      'avatar' => 'http://example.com/user2.jpg',
191
     *      'title' => 'Comment Title',
192
     *      'subtitle' => '2017-12-11',
193
     *      'content' => 'Some comment text'
194
     *  ],
195
     * ]
196
     * @return string
197
     */
198 6
    public static function comment($url, array $commentsArray)
199
    {
200 6
        $commentBlock = self::generateCommentBlock($commentsArray);
201
202 6
        return '<div data-block="comments" data-url="' . $url . '">' . $commentBlock . '</div>';
203
    }
204
205
    /**
206
     * Generate accordion
207
     * @param array $accordionArray array accordion elements
208
     * [
209
     *     ['title' => 'Page title 1', 'text' => 'Text 1'],
210
     *     ['title' => 'Page title 2', 'text' => 'Text 2', 'expanded' => true],
211
     * ]
212
     * @return string
213
     */
214 6
    public static function accordion(array $accordionArray)
215
    {
216 6
        $accordionString = '<div data-block="accordion">';
217
218 6
        foreach ($accordionArray as $item) {
219 6
            $expanded = isset($item['expanded']) && $item['expanded'] ? ' data-expanded="true"' : '';
220 6
            $accordionString .= '<div data-block="item" data-title="' . $item['title'] . '"' . $expanded . '>' . $item['text'] . '</div>';
221 3
        }
222
223 6
        $accordionString .= '</div>';
224
225 6
        return $accordionString;
226
    }
227
228
    /**
229
     * Generate Ad block position element
230
     * @param string $turboAdId value of $turboAdId used in Channel() class
231
     * @return string
232
     *
233
     * @see Channel::$adTurboAdId
234
     */
235 6
    public static function adBlockPosition($turboAdId)
236
    {
237 6
        return '<figure data-turbo-ad-id="' . $turboAdId . '"></figure>';
238
    }
239
240 6
    private static function generateCommentBlock(array $commentsArray)
241
    {
242 6
        $commentBlock = '';
243
244 6
        foreach ($commentsArray as $commentArray) {
245 6
            $author = isset($commentArray['author']) ? 'data-author="' . $commentArray['author'] . '"' : '';
246 6
            $avatar = isset($commentArray['avatar']) ? 'data-avatar-url="' . $commentArray['avatar'] . '"' : '';
247 6
            $subtitle = isset($commentArray['subtitle']) ? 'data-subtitle="' . $commentArray['subtitle'] . '"' : '';
248
249
            $commentBlock .= '<div
250
                        data-block="comment"
251 6
                        ' . $author . ' 
252 6
                        ' . $avatar . '
253 6
                        ' . $subtitle . '                         
254 3
                        ><div data-block="content">';
255
256 6
            $commentBlock .= isset($commentArray['title']) ? '<header>' . $commentArray['title'] . '</header>' : '';
257 6
            $commentBlock .= isset($commentArray['content']) ? '<p>' . $commentArray['content'] . '</p></div>' : '';
258
259 6
            if (isset($commentArray['comments'])) {
260 6
                $commentBlock .= '<div data-block="comments">';
261 6
                $commentBlock .= self::generateCommentBlock($commentArray['comments']);
262 6
                $commentBlock .= '</div>';
263 3
            }
264
265 6
            $commentBlock .= '</div>';
266 3
        }
267
268 6
        return $commentBlock;
269
    }
270
271
    /**
272
     * Generate header menu
273
     * @param array $menuArray array of arrays with pairs of url and title
274
     * [
275
     *     ['url' => 'http://example/page1.html', 'title' => 'Page title 1'],
276
     *     ['url' => 'http://example/page2.html', 'title' => 'Page title 2'],
277
     * ]
278
     * @return string
279
     */
280 6
    private static function generateMenu(array $menuArray)
281
    {
282 6
        $menuString = '';
283
284 6
        foreach ($menuArray as $menuItem) {
285 6
            $menuString .= '<a href="' . $menuItem['url'] . '">' . $menuItem['title'] . '</a>';
286 3
        }
287
288 6
        return '<menu>' . $menuString . '</menu>';
289
    }
290
}