Nip_Form_Renderer_Table   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Test Coverage

Coverage 12.16%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 23
eloc 69
c 1
b 0
f 0
dl 0
loc 121
ccs 9
cts 74
cp 0.1216
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A addClassName() 0 4 1
A setTBodyAttrib() 0 4 1
A renderElements() 0 19 4
A setRowAttrib() 0 4 1
A __construct() 0 6 1
A setCols() 0 3 1
B renderRows() 0 45 11
A addCell() 0 6 2
A setTableAttrib() 0 4 1
1
<?php
2
3
use Nip\Form\Renderer\AbstractRenderer;
4
5
class Nip_Form_Renderer_Table extends AbstractRenderer
6
{
7
    protected $_table = [];
8
    protected $_tbody = [];
9
    protected $_data = [];
10
    protected $_rows = [];
11
    protected $_cols = [];
12
13 8
    public function __construct()
14
    {
15 8
        parent::__construct();
16 8
        $this->setTableAttrib('class', 'form horizontal');
17 8
        $this->setTableAttrib('cellspacing', '0');
18 8
        $this->setTableAttrib('cellpadding', '0');
19 8
    }
20
21 8
    public function setTableAttrib($type, $value)
22
    {
23 8
        $this->_table[$type] = $value;
24 8
        return $this;
25
    }
26
27
    public function setTBodyAttrib($type, $value)
28
    {
29
        $this->_tbody[$type] = $value;
30
        return $this;
31
    }
32
33
    public function setRowAttrib($idRow, $type, $value)
34
    {
35
        $this->_rows[$idRow][$type] = $value;
36
        return $this;
37
    }
38
39
    public function addClassName($name)
40
    {
41
        $this->_table['class'] .= ' ' . $name;
42
        return $this;
43
    }
44
45
    public function addCell($idRow, $idCol, $element, $type = 'text')
46
    {
47
        $this->_data[$idRow][$idCol]['element'] = $element;
48
        $this->_data[$idRow][$idCol]['type'] = $type;
49
        if (!in_array($idCol, $this->_cols)) {
50
            $this->_cols[] = $idCol;
51
        }
52
    }
53
54
    public function setCols()
55
    {
56
        $this->_cols = func_get_args();
57
    }
58
59
60
    public function renderElements()
61
    {
62
        $return = '<table';
63
        foreach ($this->_table as $attrib => $value) {
64
            $return .= ' ' . $attrib . '="' . $value . '"';
65
        }
66
        $return .= '>';
67
        $renderRows = $this->renderRows();
68
        $return .= '<tbody';
69
        foreach ($this->_tbody as $attrib => $value) {
70
            $return .= ' ' . $attrib . '="' . $value . '"';
71
        }
72
        $return .= '>';
73
        if ($renderRows) {
74
            $return .= $renderRows;
75
        }
76
        $return .= '</tbody>';
77
        $return .= '</table>';
78
        return $return;
79
    }
80
81
    public function renderRows()
82
    {
83
        $return = '';
84
        foreach ($this->_data as $idRow=>$row) {
85
            $cell = reset($row);
86
            $element = $cell['element'];
87
88
            if (!$element->isRendered()) {
89
                $return .= '<tr';
90
                if ($this->_rows[$idRow]) {
91
                    foreach ($this->_rows[$idRow] as $attrib => $value) {
92
                        $return .= ' ' . $attrib . '="' . $value . '"';
93
                    }
94
                }
95
96
                $return .= '>';
97
                foreach ($this->_cols as $idCol) {
98
                    $cell = $row[$idCol];
99
                    switch ($cell['type']) {
100
                        case 'label':
101
                            $return .= '<td class="label' . ($element->isError() ? ' error' : '') . '">';
102
                            $return .= $element->getLabel();
103
                            if ($element->isRequired()) {
104
                                $return .= '<span class="required">*</span>';
105
                            }
106
                            $return .= ":";
107
                            break;
108
109
                        case 'value':
110
                            $return .= '<td class="value">';
111
                            $return .= $this->renderElement($element);
112
                            $return .= '</td>';
113
                            break;
114
115
                        case 'text':
116
                        default:
117
                            $return .= '<td>';
118
                            $return .= $cell['element'];
119
                            $return .= '</td>';
120
                    }
121
                }
122
                $return .= '</tr>';
123
            }
124
        }
125
        return $return;
126
    }
127
}
128