Test Failed
Push — develop ( fe93c2...a526c9 )
by nguereza
05:50
created

Html::br()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
c 0
b 0
f 0
nc 4
nop 2
dl 0
loc 10
rs 10
1
<?php
2
    defined('ROOT_PATH') || exit('Access denied');
3
    /**
4
     * TNH Framework
5
     *
6
     * A simple PHP framework using HMVC architecture
7
     *
8
     * This content is released under the MIT License (MIT)
9
     *
10
     * Copyright (c) 2017 TNH Framework
11
     *
12
     * Permission is hereby granted, free of charge, to any person obtaining a copy
13
     * of this software and associated documentation files (the "Software"), to deal
14
     * in the Software without restriction, including without limitation the rights
15
     * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16
     * copies of the Software, and to permit persons to whom the Software is
17
     * furnished to do so, subject to the following conditions:
18
     *
19
     * The above copyright notice and this permission notice shall be included in all
20
     * copies or substantial portions of the Software.
21
     *
22
     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
     * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
     * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25
     * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
     * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27
     * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28
     * SOFTWARE.
29
     */
30
31
    class Html {
32
33
        /**
34
         * Generate the html anchor link
35
         * 
36
         * @param  string $link       the href attribute value
37
         * @param  string $anchor     the displayed anchor
38
         * @param  array  $attributes the additional attributes to be added
39
         * @param boolean $return whether need return the generated html or just display it directly
40
         *
41
         * @return string|void the anchor link generated html if $return is true or display it if not
42
         */
43
        public static function anchor($link = '', $anchor = null, array $attributes = array(), $return = true) {
44
            $link = get_instance()->url->appUrl($link);
45
            if (!$anchor) {
46
                $anchor = $link;
47
            }
48
            $str = null;
49
            $str .= '<a href = "' . $link . '"';
50
            $str .= attributes_to_string($attributes);
51
            $str .= '>';
52
            $str .= $anchor;
53
            $str .= '</a>';
54
55
            if ($return) {
56
                return $str;
57
            }
58
            echo $str;
59
        }
60
		
61
        /**
62
         * Generate an mailto anchor link
63
         * 
64
         * @param  string $link       the email address 
65
         * @param  string $anchor     the displayed value of the link
66
         * @param  array  $attributes the additional attributes to be added
67
         * @param boolean $return whether need return the generated html or just display it directly
68
         *
69
         * @return string|void the generated html for mailto link if $return is true or display it if not
70
         */
71
        public static function mailto($link, $anchor = null, array $attributes = array(), $return = true) {
72
            if (!$anchor) {
73
                $anchor = $link;
74
            }
75
            $str = null;
76
            $str .= '<a href = "mailto:' . $link . '"';
77
            $str .= attributes_to_string($attributes);
78
            $str .= '>';
79
            $str .= $anchor;
80
            $str .= '</a>';
81
            if ($return) {
82
                return $str;
83
            }
84
            echo $str;
85
        }
86
87
        /**
88
         * Generate the html "br" tag  
89
         * 
90
         * @param  integer $nb the number of generated "<br />" tag
91
         * @param boolean $return whether need return the generated html or just display it directly
92
         *
93
         * @return string|void      the generated "br" html if $return is true or display it if not
94
         */
95
        public static function br($nb = 1, $return = true) {
96
            $nb = (int) $nb;
97
            $str = null;
98
            for ($i = 1; $i <= $nb; $i++) {
99
                $str .= '<br />';
100
            }
101
            if ($return) {
102
                return $str;
103
            }
104
            echo $str;
105
        }
106
107
        /**
108
         * Generate the html content for tag "hr"
109
         * 
110
         * @param integer $nb the number of generated "<hr />" tag
111
         * @param  array   $attributes the tag attributes
112
         * @param  boolean $return    whether need return the generated html or just display it directly
113
         *
114
         * @return string|void the generated "hr" html if $return is true or display it if not.
115
         */
116
        public static function hr($nb = 1, array $attributes = array(), $return = true) {
117
            $nb = (int) $nb;
118
            $str = null;
119
            for ($i = 1; $i <= $nb; $i++) {
120
                $str .= '<hr' . attributes_to_string($attributes) . ' />';
121
            }
122
            if ($return) {
123
                return $str;
124
            }
125
            echo $str;
126
        }
127
128
        /**
129
         * Generate the html content for tag like h1, h2, h3, h4, h5 and h6
130
         * 
131
         * @param  integer $type       the tag type 1 mean h1, 2 h2, etc,
132
         * @param  string  $text       the display text
133
         * @param integer $nb the number of generated "<h{1,2,3,4,5,6}>"
134
         * @param  array   $attributes the tag attributes
135
         * @param  boolean $return    whether need return the generated html or just display it directly
136
         *
137
         * @return string|void the generated header html if $return is true or display it if not.
138
         */
139
        public static function head($type = 1, $text = null, $nb = 1, array $attributes = array(), $return = true) {
140
            $nb = (int) $nb;
141
            $type = (int) $type;
142
            $str = null;
143
            for ($i = 1; $i <= $nb; $i++) {
144
                $str .= '<h' . $type . attributes_to_string($attributes) . '>' . $text . '</h' . $type . '>';
145
            }
146
            if ($return) {
147
                return $str;
148
            }
149
            echo $str;
150
        }
151
152
        /**
153
         * Generate the html "ul" tag
154
         * 
155
         * @param  array   $data the data to use for each "li" tag
156
         * @param  array   $attributes   the "ul" properties attribute use the array index below for each tag:
157
         *  for ul "ul", for li "li".
158
         * @param  boolean $return whether need return the generated html or just display it directly
159
         *
160
         * @return string|void the generated "ul" html  if $return is true or display it if not.
161
         */
162
        public static function ul(array $data = array(), array $attributes = array(), $return = true) {
163
            if ($return) {
164
                return self::buildUlOl($data, $attributes, true, 'ul');
165
            }
166
            self::buildUlOl($data, $attributes, false, 'ul');
167
        }
168
169
        /**
170
         * Generate the html "ol" tag
171
         * 
172
         * @param  array   $data the data to use for each "li" tag
173
         * @param  array   $attributes   the "ol" properties attribute use the array index below for each tag:
174
         *  for ol "ol", for li "li".
175
         * @param  boolean $return whether need return the generated html or just display it directly
176
         * 
177
         * @return string|void the generated "ol" html  if $return is true or display it if not.
178
         */
179
        public static function ol(array $data = array(), array $attributes = array(), $return = true) {
180
            if ($return) {
181
                return self::buildUlOl($data, $attributes, true, 'ol');
182
            }
183
            self::buildUlOl($data, $attributes, false, 'ol');
184
        }
185
186
187
        /**
188
         * Generate the html "table" tag
189
         * 
190
         * @param  array   $headers the table headers to use between (<thead>)
191
         * @param  array   $body the table body values between (<tbody>)
192
         * @param  array   $attributes   the table properties attribute use the array index below for each tag:
193
         *  for table "table", for thead "thead", for thead tr "thead_tr",
194
         *  for thead th "thead_th", for tbody "tbody", for tbody tr "tbody_tr", 
195
         *  for tbody td "tbody_td", for tfoot "tfoot",
196
         *  for tfoot tr "tfoot_tr", for tfoot th "tfoot_th".
197
         * @param boolean $useFooter whether need to generate table footer (<tfoot>) use the $headers values
198
         * @param  boolean $return whether need return the generated html or just display it directly
199
         * 
200
         * @return string|void the generated "table" html  if $return is true or display it if not.
201
         */
202
        public static function table(
203
                                    array $headers = array(), 
204
                                    array $body = array(), 
205
                                    array $attributes = array(), 
206
                                    $useFooter = false, 
207
                                    $return = true
208
                                ) {
209
            $str = null;
210
            $defaultAttributes = array();
211
            $defaultAttributes['table'] = array();
212
            $attributes = array_merge($defaultAttributes, $attributes);
213
            $tableAttributes = attributes_to_string($attributes['table']);
214
            $str .= '<table' . $tableAttributes . '>';
215
            $str .= self::buildTableHeader($headers, $attributes);
216
            $str .= self::buildTableBody($body, $attributes);
217
218
            if ($useFooter) {
219
                $str .= self::buildTableFooter($headers, $attributes);
220
            }
221
            $str .= '</table>';
222
            if ($return) {
223
                return $str;
224
            }
225
            echo $str;
226
        }
227
228
        /**
229
         * This method is used to build the header of the html table
230
         * 
231
         * @see  Html::table 
232
         * @return string|null
233
         */
234
        protected static function buildTableHeader(array $headers, array $attributes = array()) {
235
            $str = null;
236
            $defaultAttributes = array();
237
            $defaultAttributes['thead'] = array();
238
            $defaultAttributes['thead_tr'] = array();
239
            $defaultAttributes['thead_th'] = array();
240
            $attributes = array_merge($defaultAttributes, $attributes);
241
242
            $theadAttributes = attributes_to_string($attributes['thead']);
243
            $theadtrAttributes = attributes_to_string($attributes['thead_tr']);
244
            $thAttributes = attributes_to_string($attributes['thead_th']);
245
246
            $str .= '<thead' . $theadAttributes . '>';
247
            $str .= '<tr' . $theadtrAttributes . '>';
248
            foreach ($headers as $value) {
249
                $str .= '<th' . $thAttributes . '>' . $value . '</th>';
250
            }
251
            $str .= '</tr>';
252
            $str .= '</thead>';
253
            return $str;
254
        }
255
256
        /**
257
         * This method is used to build the body of the html table
258
         * 
259
         * @see  Html::table 
260
         * @return string|null
261
         */
262
        protected static function buildTableBody(array $body, array $attributes = array()) {
263
            $str = null;
264
            $defaultAttributes = array();
265
            $defaultAttributes['tbody'] = array();
266
            $defaultAttributes['tbody_tr'] = array();
267
            $defaultAttributes['tbody_td'] = array();
268
            $attributes = array_merge($defaultAttributes, $attributes);
269
            
270
            $tbodyAttributes = attributes_to_string($attributes['tbody']);
271
            $tbodytrAttributes = attributes_to_string($attributes['tbody_tr']);
272
            $tbodytdAttributes = attributes_to_string($attributes['tbody_td']);
273
            
274
            $str .= '<tbody' . $tbodyAttributes . '>';
275
            $str .= self::buildTableBodyContent($body, $tbodytrAttributes, $tbodytdAttributes);
276
            $str .= '</tbody>';
277
            return $str;
278
        }
279
280
        /**
281
         * This method is used to build the body content of the html table
282
         * 
283
         * @param  array  $body the table body data
284
         * @param  string $tbodytrAttributes the html attributes for each tr in tbody
285
         * @param  string $tbodytdAttributes the html attributes for each td in tbody
286
         * @return string                    
287
         */
288
        protected static function buildTableBodyContent(array $body, $tbodytrAttributes, $tbodytdAttributes) {
289
            $str = null;
290
            foreach ($body as $row) {
291
                if (is_array($row)) {
292
                    $str .= '<tr' . $tbodytrAttributes . '>';
293
                    foreach ($row as $value) {
294
                        $str .= '<td' . $tbodytdAttributes . '>' . $value . '</td>';	
295
                    }
296
                    $str .= '</tr>';
297
                }
298
            }
299
            return $str;
300
        }
301
302
        /**
303
         * This method is used to build the footer of the html table
304
         * 
305
         * @see  Html::table 
306
         * @return string|null
307
         */
308
        protected static function buildTableFooter(array $footers, array $attributes = array()) {
309
            $str = null;
310
            $defaultAttributes = array();
311
            $defaultAttributes['tfoot'] = array();
312
            $defaultAttributes['tfoot_tr'] = array();
313
            $defaultAttributes['tfoot_th'] = array();
314
            $attributes = array_merge($defaultAttributes, $attributes);
315
            
316
            $tfootAttributes = attributes_to_string($attributes['tfoot']);
317
            $tfoottrAttributes = attributes_to_string($attributes['tfoot_tr']);
318
            $thAttributes = attributes_to_string($attributes['tfoot_th']);
319
            
320
            $str .= '<tfoot' . $tfootAttributes . '>';
321
                $str .= '<tr' . $tfoottrAttributes . '>';
322
                foreach ($footers as $value) {
323
                    $str .= '<th' . $thAttributes . '>' . $value . '</th>';
324
                }
325
                $str .= '</tr>';
326
                $str .= '</tfoot>';
327
            return $str;
328
        }
329
330
        /**
331
         * Return the HTML content for ol or ul tags
332
         * 
333
         * @see  Html::ol
334
         * @see  Html::ul
335
         * @param  string  $olul   the type 'ol' or 'ul'
336
         * 
337
         * @return void|string
338
         */
339
        protected static function buildUlOl(
340
                                            array $data = array(), 
341
                                            array $attributes = array(), 
342
                                            $return = true, 
343
                                            $olul = 'ul'
344
                                        ) {
345
            $str = null;
346
            $defaultAttributes = array();
347
            $defaultAttributes[$olul] = array();
348
            $defaultAttributes['li'] = array();
349
            $attributes = array_merge($defaultAttributes, $attributes);
350
            
351
            $olulAttributes = attributes_to_string($attributes[$olul]);
352
            $liAttributes = attributes_to_string($attributes['li']);
353
            
354
            $str .= '<' . $olul . $olulAttributes . '>';
355
            foreach ($data as $row) {
356
                $str .= '<li' . $liAttributes . '>' . $row . '</li>';
357
            }
358
            $str .= '</' . $olul . '>';
359
            if ($return) {
360
                return $str;
361
            }
362
            echo $str;
363
        }
364
    }
365