-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimpleTag.Class.php
More file actions
executable file
·480 lines (452 loc) · 14.4 KB
/
Copy pathsimpleTag.Class.php
File metadata and controls
executable file
·480 lines (452 loc) · 14.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
<?php
/**
*===============================================================================
*
* MIT License
*
* Copyright (c) 2020 Lorand Veres (lorand.mast@gmail.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* ===============================================================================
*
*
*
* ABOUT simpleTag
* ---------------
* Easy to use and create simple HTML blocks, what are more often in use to format
* data from a given array using PHP.
*
* The intended purpose it is just to print the data in a very handy fashion without
* having to mess up the php code. simpleTag is not intended to manipulate any XML
* documents or fragments.
*
* The following Objects
*
* simpleTag -> inline_tag
* simpleTag -> single_tag
*
* Will be printed one per row without increasing the indenting deepness after
* subsequent inline tags. The tags listed in the arrays are not the ones specified
* until the HTML4 standard. These arrays were created to print out the tags in the
* desired way. At this time are still missing elements yet.
*
*
*/
class simpleTag {
private $Doc = '';
private $inline_tag = array('a', 'abbr', 'acronym', 'b', 'bdo', 'big', 'br', 'button', 'i', 'cite', 'code', 'dfn', 'dd', 'dt', 'em', 'hr', 'img', 'input', 'kbd', 'label', 'li', 'link', 'map', 'meta', 'object', 'option', 'q', 'script', 'samp', 'small', 'span', 'strong', 'sub', 'sup', 'textarea', 'time', 'tt', 'var');
private $block_tag = array('address', 'article', 'aside', 'blockquote', 'canvas', 'div', 'dl', 'fieldset', 'figcaption', 'figure', 'footer', 'form', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'header', 'main', 'nav', 'noscript', 'ol', 'p', 'pre', 'section', 'select', 'table', 'tfoot', 'ul', 'video');
private $single_tag = array('!DOCTYPE html', 'meta', 'link', 'br', 'img', 'hr', 'input', 'embed', 'bgsound', 'base', 'col', 'source');
private $indentNum = 0;
private $set_count = TRUE;
function __construct() {
}
/*
* Append one tag into another
* Use:
*
* simpleTag -> append_tag($into, $tag);
*
* @return void
* @author Lorand Veres
*
*/
public function append_tag(&$into, $tag) {
if (!array_key_exists(1, $into)) {
if (!array_push($into, $tag))
;
} else {
if (!array_push($into[1], $tag))
;
}
}
/*
* Create a tag. When single tags are printed like <input> the $txt variable
* value provided should be an empty string "". No need to add each attribute
* at once, it can take a string with multiple attributes concatenated like in
* the example.
* Use:
* $tag = simpleTag -> tag("input", 'type="text" name="fname" value="first name" style="width:100px"', '');
*
* @return string
* @author Lorand Veres
*/
public function tag($tagname, $attr, $txt) {
$o = "<";
$c = ">";
// output /
$e = chr(47);
// output space
$s = chr(32);
// output "
$q = chr(34);
// output tab
$t = chr(9);
// output =
$eq = chr(61);
// output new line
$n = chr(10);
$single = $this -> check_single_tag($tagname);
if (!$single) {
$tag = array();
$tag[] = $o . $tagname . $this -> setAttr($attr) . $c;
$tag[] = !empty($txt) ? array($txt) : array();
$tag[] = $o . $e . $tagname . $c;
} elseif ($single) {
$tag = ($o . $tagname . $this -> setAttr($attr) . $c . $n);
}
return $tag;
}
/*
* @parameter passed is a string. Returning TRUE if in a single_tag array
* Helper function inside docOutput(...)
*
* @return bool
* @author Lorand Veres
*/
private function check_single_tag($tag) {
in_array($tag, $this -> single_tag) ? $b = true : $b = false;
return $b;
}
/*
* @parameter passed is a string. Returning TRUE if if is an opening tag
* Helper function inside docOutput(...).
*
* @return bool
* @author Lorand Veres
*/
private function check_start_tag($value) {
substr($value, 0, 2) !== "</" ? $start_tag = true : $start_tag = false;
return $start_tag;
}
/*
* @parameter passed is a string. Returning TRUE if if is a plain text, it may contain
* inline elements embeded in to the text.
* Helper function inside docOutput(...).
*
* @return bool
* @author Lorand Veres
*/
private function check_plain_txt($value) {
substr($value, 0, 1) !== "<" & substr($value, -1, 1) !== ">" ? $start_tag = true : $start_tag = false;
return $start_tag;
}
/*
* Cheking the type of the tag.
* Helper function inside docOutput.
*
* @return bool
* @author Lorand Veres
*/
private function check_tag_type($tag, $array) {
$n = false;
$txt_tag;
if (is_array($tag)) {
if (is_string($tag[0]))
$txt_tag = $tag[0];
} elseif (is_string($tag)) {
$txt_tag = $tag;
}
$tag_array = explode(' ', $txt_tag);
$tag_array[0] = str_replace('<', '', $tag_array[0]);
$tag_array[0] = str_replace('/', '', $tag_array[0]);
$tag_array[0] = str_replace('>', '', $tag_array[0]);
in_array($tag_array[0], $array) ? $n = true : $n = false;
return $n;
}
/**
*
*
* Helper function inside docOutput.
*
* @return void
* @author Lorand Veres
*/
private function print_inline_tag($value) {
$tf = str_pad("", $this -> indentNum + 1, "\t");
printf("%s", $tf . $value[0] . $value[1][0] . $value[2] . "\n");
}
/**
* Helper function inside docOutput.
*
* @return void
* @author Lorand Veres
*/
private function print_text($value) {
$tf = str_pad("", $this -> indentNum + 1, "\t");
if ($this -> check_plain_txt($value[0]))
printf("%s", $tf . $value[0] . "\n");
}
/**
* Helper function inside docOutput.
*
* @return void
* @author Lorand Veres
*/
private function print_single_tag($value) {
$tf = str_pad("", $this -> indentNum + 1, "\t");
printf("%s", $tf . $value . "");
}
/**
* Helper function inside docOutput.
*
* @return void
* @author Lorand Veres
*/
private function print_block_tag($value) {
$tf = str_pad("", $this -> indentNum, "\t");
// checking for very first block tag if start indenting > 0
if ($this -> set_count & $this -> indentNum > 0) {
$value = $tf . $value . "\n";
$this -> set_count = FALSE;
printf("%s", $value);
}
// printing the block tags
$this -> check_tag_type($value, $this -> block_tag) ? printf("%s", $tf . $value . "\n") : '';
!$this -> check_start_tag($value) | $this -> check_tag_type($value, $this -> inline_tag) ? $this -> indentNum-- : '';
}
/*
* Helper function, appending the attributes, used inside simpleTag ->tag(..)
*
* @return void
* @author Lorand Veres
*/
private function setAttr($attr) {
$attributes = '';
if (is_array($attr) && !empty($attr)) {
foreach ($attr as $key => $value) {
$attributes .= ' ' . $key . '="' . $value . '"';
}
} elseif (is_string($attr) && !empty($attr)) {
$attributes .= ' ' . $attr;
}
return $attributes;
}
/*
* A recursive function doing the heavy printing.
* @argument array(). It is a document fragment created under the form of
* a multidimensional Array. See example .
*
* @return void
* @author Lorand Veres
*/
private function docOutput($argument) {
foreach ($argument as $key => $value) {
if (is_array($value) && !empty($value)) {
if (is_string($value[0])) {
if ($this -> check_tag_type($value, $this -> block_tag)) {
// increment the indenting if block tag
$this -> check_start_tag($value[0]) ? $this -> indentNum++ : '';
$this -> docOutput($value);
} elseif ($this -> check_tag_type($value, $this -> inline_tag) & count($value) === 3) {
$this -> print_inline_tag($value);
} elseif (count($value) === 1) {
$this -> print_text($value);
}
} else {
$this -> docOutput($value);
}
} elseif (is_string($value)) {
if ($this -> check_tag_type($value, $this -> single_tag)) {
$this -> print_single_tag($value);
} else {
$this -> print_block_tag($value);
}
}
}
}
/**
* Print the document and reset the internal objects to default values
*
* @param indentNum = int. Is optional, helpful if you would like to
* align the indentation in your HTML output to the existing position
* for improved human visibility.
* Use:
* simpleTag -> print_doc($doc_array, [$indentNum]);
*
* @return void
* @author Lorand Veres
*/
public function print_doc($arguments) {
$arg = func_get_args();
if (isset($arg[1]))
$this -> indentNum = $arg[1];
$this -> docOutput($arg[0]);
$this -> indentNum = 0;
$this -> set_count = TRUE;
}
/**
* Setting up the attributes array, if not provided one array with
* empty elements initialised
* Helper function in print_table
*
* @return $in_attr (array)
* @author Lorand Veres
*/
private function table_attr($my_data) {
if (isset($my_data['attr'])) {
$in_attr = $my_data['attr'];
} else {
for ($i = 0; $i < count($my_data['data'][0]); $i++) {
$in_attr[$i] = '';
}
}
return $in_attr;
}
/**
* Self deducting that the table head is generated
* Helper function in print_table
*
* @return $table (array)
* @author Lorand Veres
*/
private function table_head($my_data, $in_attr, $table) {
if (isset($my_data['th'])) {
$tr = $this -> tag('tr', '', '');
for ($i = 0; $i < count($my_data['th']); $i++) {
$this -> append_tag($tr, $this -> tag('th', $in_attr[$i], $my_data['th'][$i]));
}
$this -> append_tag($table, $tr);
}
return $table;
}
/**
* Numeric data will have cells style text-align:right
*
* @return string
* @author Lorand Veres
*/
private function cell_numeric_right($attr, $data){
if(is_numeric($data)){
if(strpos($attr, 'text-align:')){
str_replace('text-align:left', 'text-align: right', $attr) | str_replace('text-align: left', 'text-align: right', $$attr);
}else{
$attr .= 'text-align: right';
}
}
return $attr;
}
/**
* Self deducting that the table body is generated.
* Helper function in print_table
*
* @return $table (array)
* @author Lorand Veres
*/
private function table_body($my_data, $in_attr, $table) {
if (isset($my_data['data'])) {
for ($i = 0; $i < count($my_data['data']); $i++) {
$tr[$i] = $this -> tag('tr', '', '');
for ($j = 0; $j < count($my_data['data'][$i]); $j++) {
$in_attr[$j] = $this -> cell_numeric_right($in_attr[$j], $my_data['data'][$i][$j]);
$this -> append_tag($tr[$i], $this -> tag('td', $in_attr[$j], $my_data['data'][$i][$j]));
}
$this -> append_tag($table, $tr[$i]);
}
} else {
echo "simpleTag Fatal Error at line 360, no data suplied for simpleTag -> table";
die();
}
return $table;
}
/**
* Print the table. All array elemets are optinal but 'data' .
*
* @param array('tb' => string, 'th' => array(), 'attr' => array()), 'data' => array(array(), array()....))
* 'tb' = string table attributes
* 'th' = th elements text insertion
* 'attr' = optional attributes like style. it has no individual id posibility yet
* 'data' = the text data what will be inserted in each cell
*
* @param int, the indentation deepness optional
*
* @return void
* @author Lorand Veres
*/
public function print_table() {
$my_data = func_get_arg(0);
isset($my_data['tb']) ? $tb_attr = $my_data['tb'] : $tb_attr = '';
$table = $this -> tag('table', $tb_attr, '');
$in_attr = $this -> table_attr($my_data);
if (func_num_args() > 1)
$this -> indentNum = func_get_arg(1);
$table = $this -> table_head($my_data, $in_attr, $table);
$table = $this -> table_body($my_data, $in_attr, $table);
$this -> print_doc($table);
}
/**
* Print lists.
* @param First param can be string or aray to define the list type and
* maybe suply attributes
* @param Second param is an array of list elemts, or 2 dimensional array for definition lists
* @param Third param optional to increase deepness
*
* @param example array('item 1', 'ietm 2', 'item 3', 'item 4');
* Use:
* print_list('ul', array('item 1', 'ietm 2', 'item 3', 'item 4'), 3)
* print_list(array('ol', 'start="8"'), array('item 1', 'ietm 2', 'item 3', 'item 4'), 3)
*
* @return void
* @author Lorand Veres
*/
public function print_list() {
$my_list_type = func_get_arg(0);
$my_items = func_get_arg(1);
if (func_num_args() > 2)
$this -> indentNum = func_get_arg(2);
is_array($my_list_type) & isset($my_list_type[1]) ? $attr = $my_list_type[1][0] : $attr = '';
!is_string($my_list_type) ? $list_type = $my_list_type[0] : $list_type = $my_list_type;
$list = $this -> tag($list_type, $attr, '');
if ($list_type === 'ol' | $list_type === 'ul') {
foreach ($my_items as $key => $value) {
$this -> append_tag($list, $this -> tag('li', '', $value));
}
} elseif ($list_type === 'dl') {
foreach ($my_items as $value) {
$this -> append_tag($list, $this -> tag('dt', '', $value[0]));
$this -> append_tag($list, $this -> tag('dd', '', $value[1]));
}
}
$this -> print_doc($list);
}
/**
* Print out a select option group
* @param (string) $attr , representing select element attributes
* @param (array) $values = array( array('value="peach"' , 'Peach'), array('value="apple"' , 'Apple'))
* @param (int) the indent deepnes, optional
*
* @return void
* @author Lorand Veres
*/
public function print_select_option($attr, $values){
if(func_num_args() > 2)
$this -> indentNum = func_get_arg(2);
$select_el = $this -> tag('select', $attr, '');
for($i = 0; $i < count($values); $i++) {
if(is_array($values[$i])) {
$clean = array_values($values[$i]);
substr($clean[0], 0, 6) === 'value=' ? $clean[0] : $clean[0] = 'value="'.$clean[0].'"';
$this -> append_tag($select_el, $this -> tag('option', $clean[0], $clean[1]));
}
}
$this -> print_doc($select_el);
}
}// end of class
?>