Completed
Push — master ( ec0e2e...0caa35 )
by Nuno
01:57
created

TableColumn::isArrayAssociative()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
1
<?php
2
namespace Jupitern\Table;
3
4
Class TableColumn
5
{
6
	public $value;
7
8
	private $tableInstance;
9
	private $title;
10
	private $attrs;
11
	private $css;
12
	private $filter = false;
13
	private $filterData = null;
14
15
	/**
16
	 * create a new TableColumn instance
17
	 *
18
	 * @param Table $tableInstance
19
	 */
20
	public function __construct(Table &$tableInstance)
21
	{
22
		$this->tableInstance = $tableInstance;
23
		$this->attrs = ['header' => new Properties(), 'body' => new Properties()];
24
		$this->css = ['header' => new Properties(), 'body' => new Properties()];
25
	}
26
27
	public function __get( $prop ){
28
		return $this->$prop;
29
	}
30
31
	/**
32
	 * set column title
33
	 *
34
	 * @param $title
35
	 * @return $this
36
	 */
37
	public function title($title)
38
	{
39
		$this->title = $title;
40
		return $this;
41
	}
42
43
	/**
44
	 * bind colunm value. $value can be:
45
	 * integer index for none associative array or json
46
	 * string index for associative array, json, PDO or ORM result
47
	 * a closure that returns a string
48
	 *
49
	 * @param $value
50
	 * @return $this
51
	 */
52
	public function value($value)
53
	{
54
		$this->value = $value;
55
		return $this;
56
	}
57
58
	/**
59
	 * add a attribute to table <td> or <th>
60
	 *
61
	 * @param $attr
62
	 * @param $value
63
	 * @param bool $header
64
	 * @return $this
65
	 */
66
	public function attr($attr, $value, $header = false)
67
	{
68
		$this->attrs[$header ? 'header' : 'body']->add($attr, $value);
69
		return $this;
70
	}
71
72
	/**
73
	 * add css to table <td> or <th>
74
	 *
75
	 * @param $attr
76
	 * @param $value
77
	 * @param bool $header
78
	 * @return $this
79
	 */
80
	public function css($attr, $value, $header = false)
81
	{
82
		$this->css[$header ? 'header' : 'body']->add($attr, $value);
83
		return $this;
84
	}
85
86
	/**
87
	 * add a filter to this column.
88
	 * $data can be array (associative or not), json, PDO or ORM result
89
	 *
90
	 * @param null $data
91
	 * @return $this
92
	 */
93
	public function filter($data = null)
94
	{
95
		$this->filterData = $this->isJson($data) ? json_decode($data) : $data;
96
		$this->filter = true;
97
		$this->tableInstance->hasFilters = true;
98
		return $this;
99
	}
100
101
	/**
102
	 * add this column to the table
103
	 *
104
	 * @return Table
105
	 */
106
	public function add()
107
	{
108
		return $this->tableInstance;
109
	}
110
111
	/**
112
	 * render column header cell <th>
113
	 *
114
	 * @return mixed
115
	 */
116
	public function renderHeader()
117
	{
118
		$template = '<th {attrs} style="{css}">{title}</th>';
119
		$attrs = $this->attrs['header']->render('{prop}="{val}" ');
120
		$css = $this->css['header']->render('{prop}:{val}; ');
121
		return str_replace(['{attrs}', '{css}', '{title}'], [$attrs, $css, $this->title], $template);
122
	}
123
124
	/**
125
	 * render column filter
126
	 *
127
	 * @return string
128
	 */
129
	public function renderFilter()
130
	{
131
		$html = '';
132
		if ($this->filterData !== null) {
133
			$html .= '<select class="form-control input-sm" style="width: 99%"><option value=""></option>';
134
			foreach ($this->filterData as $option) {
135
				if (is_string($option)) {
136
					$option = [$option, $option];
137
				}
138
				elseif (is_object($option)) {
139
					$option = array_values(get_object_vars($option));
140
				}
141
				$html .= '<option value="'.$option[0].'">'.$option[1].'</option>';
142
			}
143
			$html .= '</select>';
144
		}
145
		elseif ($this->filter) {
146
			$html = '<input type="text" class="form-control input-sm"  style="width: 99%">';
147
		}
148
		return '<td>'.$html.'</td>';
149
	}
150
151
	/**
152
	 * render column body cell <td>
153
	 *
154
	 * @param $row
155
	 * @return mixed
156
	 */
157
	public function renderBody( &$row )
158
	{
159
		$template = '<td {attrs} style="{css}">{val}</td>';
160
		$attrs = $this->attrs['body']->render('{prop}="{val}" ');
161
		$css = $this->css['body']->render('{prop}:{val}; ');
162
163
		$val = "";
164
		if (is_callable($this->value)) {
165
			$val = $this->value;
166
			$val = $val($row);
167
		}
168
		elseif (is_object($row)) {
169
			$val = $row->{$this->value};
170
		}
171
		elseif (is_array($row)) {
172
			$val = $this->value !== null ? $row[$this->value] : '';
173
		}
174
		return str_replace(['{attrs}','{css}','{title}','{val}'], [$attrs, $css, $this->title, $val], $template);
175
	}
176
177
178
	/**
179
	 * Check if string if json
180
	 *
181
	 * @param $string
182
	 * @return bool
183
	 */
184
	private function isJson($string)
185
	{
186
		if (!is_string($string)) return false;
187
		json_decode($string);
188
		return (json_last_error() == JSON_ERROR_NONE);
189
	}
190
191
}