Completed
Push — master ( c8b15c...8eda6f )
by Petro
02:23
created

Content::accordion()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

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