Completed
Push — master ( f6f0d8...9b81b5 )
by Petro
02:38
created

Content::generateCommentBlock()   C

Complexity

Conditions 8
Paths 65

Size

Total Lines 30
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 8

Importance

Changes 0
Metric Value
dl 0
loc 30
ccs 17
cts 17
cp 1
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 18
nc 65
nop 1
crap 8
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 button
89
     * @param string $text
90
     * @param string $url
91
     * @param string $phone Phone number in RFC-3966 format
92
     * @param string|null $buttonColor Can be Text or HEX
93
     * @param string|null $textColor Can be Text or HEX
94
     * @param bool $isBoldText
95
     * @param bool $isDisabled
96
     * @return string
97
     */
98 12
    public static function button(string $text, string $url = '', string $phone = '',
99
                                  string $buttonColor = null, string $textColor = null,
100
                                  bool $isBoldText = false, bool $isDisabled = false): string
101
    {
102 12
        if (!$url && !$phone) {
103 3
            throw new \UnexpectedValueException('Please set url or phone number for button');
104
        }
105
106 9
        $formAction = $url ? $url : 'tel:' . $phone;
107 9
        $buttonColorString = $buttonColor ? 'data-background-color="' . $buttonColor . '"' : '';
108 9
        $textColorString   = $textColor   ? 'data-color="' . $textColor . '"' : '';
109 9
        $isBoldTextString  = $isBoldText  ? 'data-primary="true"' : '';
110 9
        $isDisabledString  = $isDisabled  ? 'disabled="true"' : '';
111
112
        return "<button
113 9
                    formaction=\"" . $formAction . "\"
114 9
                    " . $buttonColorString . "
115 9
                    " . $textColorString . "
116 9
                    " . $isBoldTextString . "
117 9
                    " . $isDisabledString . ">" . $text . "</button>";
118
    }
119
120
    /**
121
     * Generate comment block
122
     * @param string $url URL to comments page
123
     * @param array $comments multidimensional or one-dimensional array of comments,
0 ignored issues
show
Documentation introduced by
There is no parameter named $comments. Did you maybe mean $commentsArray?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
124
     * can has unlimited includes, example:
125
     * [
126
     *  [
127
     *      'author' => 'First Author Name',
128
     *      'avatar' => 'http://example.com/user1.jpg',
129
     *      'title' => 'Comment Title',
130
     *      'subtitle' => '2017-12-10',
131
     *      'content' => 'Somme comment text',
132
     *      'comments' => [
133
     *          [
134
     *              'author' => 'Third Author Name',
135
     *              'avatar' => 'http://example.com/user3.jpg',
136
     *              'title' => 'Comment Title',
137
     *              'subtitle' => '2017-12-12',
138
     *              'content' => 'Some answer text'
139
     *          ],
140
     *          [
141
     *              'author' => 'Another Author Name',
142
     *              'avatar' => 'http://example.com/user4.jpg',
143
     *              'title' => 'Comment Title',
144
     *              'subtitle' => '2017-12-13',
145
     *              'content' => 'Another answer text'
146
     *          ],
147
     *      ]
148
     *  ],
149
     *  [
150
     *      'author' => 'Second Author Name',
151
     *      'avatar' => 'http://example.com/user2.jpg',
152
     *      'title' => 'Comment Title',
153
     *      'subtitle' => '2017-12-11',
154
     *      'content' => 'Some comment text'
155
     *  ],
156
     * ]
157
     * @return string
158
     */
159 3
    public static function comment(string $url, array $commentsArray): string
160
    {
161 3
        $commentBlock = self::generateCommentBlock($commentsArray);
162
163 3
        return '<div data-block="comments" data-url="' . $url . '">' . $commentBlock . '</div>';
164
    }
165
166 3
    private static function generateCommentBlock(array $commentsArray)
167
    {
168 3
        $commentBlock = '';
169
170 3
        foreach ($commentsArray as $commentArray) {
171 3
            $author = isset($commentArray['author']) ? 'data-author="' . $commentArray['author'] . '"' : '';
172 3
            $avatar = isset($commentArray['avatar']) ? 'data-avatar-url="' . $commentArray['avatar'] . '"' : '';
173 3
            $subtitle = isset($commentArray['subtitle']) ? 'data-subtitle="' . $commentArray['subtitle'] . '"' : '';
174
175
            $commentBlock .= '<div
176
                        data-block="comment"
177 3
                        ' . $author . ' 
178 3
                        ' . $avatar . '
179 3
                        ' . $subtitle . '                         
180
                        ><div data-block="content">';
181
182 3
            $commentBlock .= isset($commentArray['title']) ? '<header>' . $commentArray['title'] . '</header>' : '';
183 3
            $commentBlock .= isset($commentArray['content']) ? '<p>' . $commentArray['content'] . '</p></div>' : '';
184
185 3
            if (isset($commentArray['comments'])) {
186 3
                $commentBlock .= '<div data-block="comments">';
187 3
                $commentBlock .= self::generateCommentBlock($commentArray['comments']);
188 3
                $commentBlock .= '</div>';
189
            }
190
191 3
            $commentBlock .= '</div>';
192
        }
193
194 3
        return $commentBlock;
195
    }
196
197
    /**
198
     * Generate header menu
199
     * @param array $menuArray array of arrays with pairs of url and title
200
     * [
201
     *     ['url' => 'http://example/page1.html', 'title' => 'Page title 1'],
202
     *     ['url' => 'http://example/page2.html', 'title' => 'Page title 2'],
203
     * ]
204
     * @return string
205
     */
206 3
    private static function generateMenu(array $menuArray)
207
    {
208 3
        $menuString = '';
209
210 3
        foreach ($menuArray as $menuItem) {
211 3
            $menuString .= '<a href="' . $menuItem['url'] . '">' . $menuItem['title'] . '</a>';
212
        }
213
214 3
        return '<menu>' . $menuString . '</menu>';
215
    }
216
}