Completed
Push — master ( d47983...d9cc9d )
by Markus
03:49
created

CTable::createRow()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
namespace donami\CTable;
3
4
/**
5
 * Module for creating HTML Tables
6
 */
7
class CTable implements \Anax\DI\IInjectionAware
8
{
9
    use \Anax\DI\TInjectable;
10
    private $data = array();
11
    private $html = '';
0 ignored issues
show
Unused Code introduced by
The property $html is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
12
    private $headings = '';
13
14
    private $tags = array(
15
      'table_start' => '<table>',
16
      'table_end' => '</table>',
17
      'tr_start' => '<tr>',
18
      'tr_end' => '</tr>',
19
      'td_start' => '<td>',
20
      'td_end' => '</td>',
21
      'th_start' => '<th>',
22
      'th_end' => '</th>'
23
    );
24
25
    /**
26
     * Set up custom tags for designing the table
27
     * @param  array  $data Array containing the tags to define
28
     * @return void
29
     */
30 1
    public function defineTags($data = array()) {
31 1
      foreach ($data as $key => $value) {
32 1
        $this->tags[$key] = $value;
33 1
      }
34 1
    }
35
36
    /**
37
     * Define the headings for the table
38
     * @param  array  $data Array containing the headings
39
     * @return void
40
     */
41 1
    public function defineHeadings($data = array()) {
42 1
      $html = $this->tags['tr_start'];
43 1
      foreach ($data as $value) {
44 1
        $html .= $this->tags['th_start'];
45 1
        $html .= $value;
46 1
        $html .= $this->tags['th_end'];
47 1
      }
48 1
      $html .= $this->tags['tr_end'];
49 1
      $this->headings = $html;
50 1
    }
51
52
    /**
53
     * Adds a row to the table one by one
54
     * @param  array  $data Array containing one key for each field
55
     * @return void
56
     */
57 1
    public function createRow($data = array()) {
58 1
      $this->data[] = $data;
59 1
    }
60
61
    /**
62
     * Generate the HTML for the table. Either using $this->data or data as argument
63
     * @param  array  $data Optional array to be used
64
     *         if no data has been set before through "create_row"
65
     * @return string The HTML to be returned
66
     */
67 6
    public function generate($data = array()) {
68 6
      $data = !empty($data)? $data : $this->data;
69
70 6
      $html = $this->tags['table_start'];
71
72 6
      $html .= $this->headings;
73
74 6
      foreach ($data as $key => $value) {
75 5
        $html .= $this->tags['tr_start'];;
76 5
        foreach ($value as $td) {
77 5
          $html .= $this->tags['td_start'];
78 5
          $html .= $td;
79 5
          $html .= $this->tags['td_end'];
80 5
        }
81 5
        $html .= $this->tags['tr_end'];
82 6
      }
83
84 6
      $html .= $this->tags['table_end'];
85
86 6
      return $html;
87
    }
88
}
89