Passed
Branch 1.0.0-dev (958860)
by nguereza
06:24
created

Html::table()   F

Complexity

Conditions 19
Paths 1600

Size

Total Lines 43
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 19
eloc 32
nc 1600
nop 5
dl 0
loc 43
rs 0.3499
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 GNU GPL License (GPL)
9
	 *
10
	 * Copyright (C) 2017 Tony NGUEREZA
11
	 *
12
	 * This program is free software; you can redistribute it and/or
13
	 * modify it under the terms of the GNU General Public License
14
	 * as published by the Free Software Foundation; either version 3
15
	 * of the License, or (at your option) any later version.
16
	 *
17
	 * This program is distributed in the hope that it will be useful,
18
	 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
	 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
	 * GNU General Public License for more details.
21
	 *
22
	 * You should have received a copy of the GNU General Public License
23
	 * along with this program; if not, write to the Free Software
24
	 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25
	*/
26
27
	class Html{
28
29
		/**
30
		 * Generate the html anchor link
31
		 * @param  string $link       the href attribute value
32
		 * @param  string $anchor     the displayed anchor
33
		 * @param  array  $attributes the additional attributes to be added
34
		 * @param boolean $return whether need return the generated html or just display it directly
35
		 *
36
		 * @return string|void             the anchor link generated html if $return is true or display it if not
37
		 */
38
		public static function a($link = '', $anchor = null, array $attributes = array(), $return = true){
39
			if(! is_url($link)){
40
				$link = Url::site_url($link);
41
			}
42
			if(! $anchor){
43
				$anchor = $link;
44
			}
45
			$str = null;
46
			$str .= '<a href = "'.$link.'"';
47
			$str .= attributes_to_string($attributes);
48
			$str .= '>';
49
			$str .= $anchor;
50
			$str .= '</a>';
51
52
			if($return){
53
				return $str;
54
			}
55
			echo $str;
56
		}
57
		
58
		/**
59
		 * Generate an mailto anchor link
60
		 * @param  string $link       the email address 
61
		 * @param  string $anchor     the displayed value of the link
62
		 * @param  array  $attributes the additional attributes to be added
63
		 * @param boolean $return whether need return the generated html or just display it directly
64
		 *
65
		 * @return string|void             the generated html for mailto link if $return is true or display it if not
66
		 */
67
		public static function mailto($link, $anchor = null, array $attributes = array(), $return = true){
68
			if(! $anchor){
69
				$anchor = $link;
70
			}
71
			$str = null;
72
			$str .= '<a href = "mailto:'.$link.'"';
73
			$str .= attributes_to_string($attributes);
74
			$str .= '>';
75
			$str .= $anchor;
76
			$str .= '</a>';
77
78
			if($return){
79
				return $str;
80
			}
81
			echo $str;
82
		}
83
84
		/**
85
		 * Generate the html "br" tag  
86
		 * @param  integer $nb the number of generated "<br />" tag
87
		 * @param boolean $return whether need return the generated html or just display it directly
88
		 *
89
		 * @return string|void      the generated "br" html if $return is true or display it if not
90
		 */
91
		public static function br($nb = 1, $return = true){
92
			$nb = (int) $nb;
93
			$str = null;
94
			for ($i = 1; $i <= $nb; $i++) {
95
				$str .= '<br />';
96
			}
97
98
			if($return){
99
				return $str;
100
			}
101
			echo $str;
102
		}
103
104
		/**
105
		 * Generate the html content for tag "hr"
106
		 * @param integer $nb the number of generated "<hr />" tag
107
		 * @param  array   $attributes the tag attributes
108
		 * @param  boolean $return    whether need return the generated html or just display it directly
109
		 *
110
		 * @return string|void the generated "hr" html if $return is true or display it if not.
111
		 */
112
		public static function hr($nb = 1, array $attributes = array(), $return = true){
113
			$nb = (int) $nb;
114
			$str = null;
115
			for ($i = 1; $i <= $nb; $i++) {
116
				$str .= '<hr' .attributes_to_string($attributes). ' />';
117
			}
118
			if($return){
119
				return $str;
120
			}
121
			echo $str;
122
		}
123
124
		/**
125
		 * Generate the html content for tag like h1, h2, h3, h4, h5 and h6
126
		 * @param  integer $type       the tag type 1 mean h1, 2 h2, etc,
127
		 * @param  string  $text       the display text
128
		 * @param integer $nb the number of generated "<h{1,2,3,4,5,6}>"
129
		 * @param  array   $attributes the tag attributes
130
		 * @param  boolean $return    whether need return the generated html or just display it directly
131
		 *
132
		 * @return string|void the generated header html if $return is true or display it if not.
133
		 */
134
		public static function head($type = 1, $text = null, $nb = 1, array $attributes = array(), $return = true){
135
			$nb = (int) $nb;
136
			$type = (int) $type;
137
			if($type <= 0 || $type > 6){
138
				$type = 1;
139
			}
140
			$str = null;
141
			for ($i = 1; $i <= $nb; $i++) {
142
				$str .= '<h' . $type . attributes_to_string($attributes). '>' .$text. '</h' . $type . '>';
143
			}
144
			if($return){
145
				return $str;
146
			}
147
			echo $str;
148
		}
149
150
		/**
151
		 * Generate the html "ul" tag
152
		 * @param  array   $data the data to use for each "li" tag
153
		 * @param  array   $attributes   the "ul" properties attribute use the array index below for each tag:
154
		 *  for ul "ul", for li "li".
155
		 * @param  boolean $return whether need return the generated html or just display it directly
156
		 *
157
		 * @return string|void the generated "ul" html  if $return is true or display it if not.
158
		 */
159
		public static function ul($data = array(), $attributes = array(), $return = true){
160
			$data = (array) $data;
161
			$str = null;
162
			$str .= '<ul' . (! empty($attributes['ul']) ? attributes_to_string($attributes['ul']):'') . '>';
163
			foreach ($data as $row) {
164
				$str .= '<li' . (! empty($attributes['li']) ? attributes_to_string($attributes['li']):'') .'>' .$row. '</li>';
165
			}
166
			$str .= '</ul>';
167
			if($return){
168
				return $str;
169
			}
170
			echo $str;
171
		}
172
173
		/**
174
		 * Generate the html "ol" tag
175
		 * @param  array   $data the data to use for each "li" tag
176
		 * @param  array   $attributes   the "ol" properties attribute use the array index below for each tag:
177
		 *  for ol "ol", for li "li".
178
		 * @param  boolean $return whether need return the generated html or just display it directly
179
		 * @return string|void the generated "ol" html  if $return is true or display it if not.
180
		 */
181
		public static function ol($data = array(), $attributes = array(), $return = true){
182
			$data = (array) $data;
183
			$str = null;
184
			$str .= '<ol' . (!empty($attributes['ol']) ? attributes_to_string($attributes['ol']):'') . '>';
185
			foreach ($data as $row) {
186
				$str .= '<li' . (!empty($attributes['li']) ? attributes_to_string($attributes['li']):'') .'>' .$row. '</li>';
187
			}
188
			$str .= '</ol>';
189
			if($return){
190
				return $str;
191
			}
192
			echo $str;
193
		}
194
195
		/**
196
		 * Generate the html "table" tag
197
		 * @param  array   $headers            the table headers to use between (<thead>)
198
		 * @param  array   $body the table body values between (<tbody>)
199
		 * @param  array   $attributes   the table properties attribute use the array index below for each tag:
200
		 *  for table "table", for thead "thead", for thead tr "thead_tr",
201
		 *  for thead th "thead_th", for tbody "tbody", for tbody tr "tbody_tr", for tbody td "tbody_td", for tfoot "tfoot",
202
		 *  for tfoot tr "tfoot_tr", for tfoot th "tfoot_th".
203
		 * @param boolean $use_footer whether need to generate table footer (<tfoot>) use the $headers values
204
		 * @param  boolean $return whether need return the generated html or just display it directly
205
		 * @return string|void the generated "table" html  if $return is true or display it if not.
206
		 */
207
		public static function table($headers = array(), $body = array(), $attributes = array(), $use_footer = false, $return = true){
208
			$headers = (array) $headers;
209
			$body = (array) $body;
210
			$str = null;
211
			$str .= '<table' . (! empty($attributes['table']) ? attributes_to_string($attributes['table']):'') . '>';
212
			if(! empty($headers)){
213
				$str .= '<thead' . (! empty($attributes['thead']) ? attributes_to_string($attributes['thead']):'') .'>';
214
				$str .= '<tr' . (! empty($attributes['thead_tr']) ? attributes_to_string($attributes['thead_tr']):'') .'>';
215
				foreach ($headers as $value) {
216
					$str .= '<th' . (! empty($attributes['thead_th']) ? attributes_to_string($attributes['thead_th']):'') .'>' .$value. '</th>';
217
				}
218
				$str .= '</tr>';
219
				$str .= '</thead>';
220
			}
221
			else{
222
				//no need check for footer
223
				$use_footer = false;
224
			}
225
			$str .= '<tbody' . (! empty($attributes['tbody']) ? attributes_to_string($attributes['tbody']):'') .'>';
226
			foreach ($body as $row) {
227
				if(is_array($row)){
228
					$str .= '<tr' . (! empty($attributes['tbody_tr']) ? attributes_to_string($attributes['tbody_tr']):'') .'>';
229
					foreach ($row as $value) {
230
						$str .= '<td' . (! empty($attributes['tbody_td']) ? attributes_to_string($attributes['tbody_td']):'') .'>' .$value. '</td>';	
231
					}
232
					$str .= '</tr>';
233
				}
234
			}
235
			$str .= '</tbody>';
236
			if($use_footer){
237
				$str .= '<tfoot' . (! empty($attributes['tfoot']) ? attributes_to_string($attributes['tfoot']):'') .'>';
238
				$str .= '<tr' . (! empty($attributes['tfoot_tr']) ? attributes_to_string($attributes['tfoot_tr']):'') .'>';
239
				foreach ($headers as $value) {
240
					$str .= '<th' . (! empty($attributes['tfoot_th']) ? attributes_to_string($attributes['tfoot_th']):'') .'>' .$value. '</th>';
241
				}
242
				$str .= '</tr>';
243
				$str .= '</tfoot>';
244
			}
245
			$str .= '</table>';
246
			if($return){
247
				return $str;
248
			}
249
			echo $str;
250
		}
251
	}
252