Passed
Pull Request — master (#15)
by Jitendra
01:44
created

HtmlHelper::tableStart()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 2
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the HTMLUP package.
5
 *
6
 * (c) Jitendra Adhikari <[email protected]>
7
 *     <https://github.com/adhocore>
8
 *
9
 * Licensed under MIT license.
10
 */
11
12
namespace Ahc;
13
14
trait HtmlHelper
15
{
16
    public function escape($input)
17
    {
18
        return \htmlspecialchars($input);
19
    }
20
21
    public function h($level, $line)
22
    {
23
        if (\is_string($level)) {
24
            $level = \trim($level, '- ') === '' ? 2 : 1;
25
        }
26
27
        if ($level < 7) {
28
            return "\n<h{$level}>" . \ltrim(\ltrim($line, '# ')) . "</h{$level}>";
29
        }
30
31
        return '';
32
    }
33
34
    public function tableStart($line, $delim = '|')
35
    {
36
        $table = "<table>\n<thead>\n<tr>\n";
37
38
        foreach (\explode($delim, \trim($line, $delim)) as $hdr) {
39
            $table .= '<th>' . \trim($hdr) . "</th>\n";
40
        }
41
42
        $table .= "</tr>\n</thead>\n<tbody>\n";
43
44
        return $table;
45
    }
46
47
    public function tableRow($line, $colCount, $delim = '|')
48
    {
49
        $row = "<tr>\n";
50
51
        foreach (\explode($delim, \trim($line, $delim)) as $i => $col) {
52
            if ($i > $colCount) {
53
                break;
54
            }
55
56
            $col  = \trim($col);
57
            $row .= "<td>{$col}</td>\n";
58
        }
59
60
        $row .= "</tr>\n";
61
62
        return $row;
63
    }
64
}
65