Completed
Push — master ( 1ecf18...ecd912 )
by Nuno
02:06 queued 44s
created

TableColumn::camelToTitle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
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
		if (empty($this->title) && !is_callable($this->value)) {
119
			if ($this->tableInstance->titlesMode == 'underscore') $this->title = $this->underscoreToTitle($this->value);
120
			elseif ($this->tableInstance->titlesMode == 'camelcase') $this->title = $this->camelToTitle($this->value);
121
		}
122
123
		$template = '<th {attrs} style="{css}">{title}</th>';
124
		$attrs = $this->attrs['header']->render('{prop}="{val}" ');
125
		$css = $this->css['header']->render('{prop}:{val}; ');
126
		return str_replace(['{attrs}', '{css}', '{title}'], [$attrs, $css, $this->title], $template);
127
	}
128
129
	/**
130
	 * render column filter
131
	 *
132
	 * @return string
133
	 */
134
	public function renderFilter()
135
	{
136
		$html = '';
137
		if ($this->filterData !== null) {
138
			$html .= '<select class="form-control input-sm" style="width: 99%"><option value=""></option>';
139
			foreach ($this->filterData as $option) {
140
				if (is_string($option)) {
141
					$option = [$option, $option];
142
				}
143
				elseif (is_object($option)) {
144
					$option = array_values(get_object_vars($option));
145
				}
146
				$html .= '<option value="'.$option[0].'">'.$option[1].'</option>';
147
			}
148
			$html .= '</select>';
149
		}
150
		elseif ($this->filter) {
151
			$html = '<input type="text" class="form-control input-sm"  style="width: 99%">';
152
		}
153
		return '<td>'.$html.'</td>';
154
	}
155
156
	/**
157
	 * render column body cell <td>
158
	 *
159
	 * @param $row
160
	 * @return mixed
161
	 */
162
	public function renderBody( &$row )
163
	{
164
		$template = '<td {attrs} style="{css}">{val}</td>';
165
		$attrs = $this->attrs['body']->render('{prop}="{val}" ');
166
		$css = $this->css['body']->render('{prop}:{val}; ');
167
168
		$val = "";
169
		if (is_callable($this->value)) {
170
			$val = $this->value;
171
			$val = $val($row);
172
		}
173
		elseif (is_object($row)) {
174
			$val = $row->{$this->value};
175
		}
176
		elseif (is_array($row)) {
177
			$val = $this->value !== null ? $row[$this->value] : '';
178
		}
179
		return str_replace(['{attrs}','{css}','{title}','{val}'], [$attrs, $css, $this->title, $val], $template);
180
	}
181
182
183
	/**
184
	 * Check if string if json
185
	 *
186
	 * @param $string
187
	 * @return bool
188
	 */
189
	private function isJson($string)
190
	{
191
		if (!is_string($string)) return false;
192
		json_decode($string);
193
		return (json_last_error() == JSON_ERROR_NONE);
194
	}
195
196
197
	/**
198
	 * @param string $str
199
	 * @return mixed
200
	 */
201
	private function camelToTitle($str)
202
	{
203
		$intermediate = preg_replace('/(?!^)([[:upper:]][[:lower:]]+)/', ' $0', $str);
204
		$titleStr = preg_replace('/(?!^)([[:lower:]])([[:upper:]])/', '$1 $2', $intermediate);
205
206
		return $titleStr;
207
	}
208
209
210
	/**
211
	 * @param string $str
212
	 * @return string
213
	 */
214
	private function underscoreToTitle($str)
215
	{
216
		$str = ucwords(str_replace("_", " ", $str));
217
218
		return $str;
219
	}
220
221
222
}
223