ColumnsSummary::setRenderer()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
/**
4
 * @copyright   Copyright (c) 2015 ublaboo <[email protected]>
5
 * @author      Pavel Janda <[email protected]>
6
 * @package     Ublaboo
7
 */
8
9
namespace Ublaboo\DataGrid;
10
11
use Ublaboo\DataGrid\Column\ColumnNumber;
12
use Ublaboo\DataGrid\Column\Renderer;
13
use Ublaboo\DataGrid\Exception\DataGridColumnRendererException;
14
15
16
class ColumnsSummary
17
{
18
19
	/**
20
	 * @var DataGrid
21
	 */
22
	protected $datagrid;
23
24
	/**
25
	 * @var array
26
	 */
27
	protected $summary;
28
29
	/**
30
	 * @var array
31
	 */
32
	protected $format = [];
33
34
	/**
35
	 * @var NULL|callable
36
	 */
37
	protected $rowCallback;
38
39
	/**
40
	 * @var Renderer|NULL
41
	 */
42
	protected $renderer;
43
44
45
	/**
46
	 * @param DataGrid $datagrid
47
	 * @param array    $columns
48
	 */
49
	public function __construct(DataGrid $datagrid, array $columns, $rowCallback)
50
	{
51
		$this->summary = array_fill_keys(array_values($columns), 0);
52
		$this->datagrid = $datagrid;
53
		$this->rowCallback = $rowCallback;
54
55
		foreach ($this->summary as $key => $sum) {
56
			$column = $this->datagrid->getColumn($key);
57
58
			if ($column instanceof ColumnNumber) {
59
				$arg = $column->getFormat();
60
				array_unshift($arg, $key);
61
62
				call_user_func_array([$this, 'setFormat'], $arg);
63
			}
64
		}
65
	}
66
67
68
	/**
69
	 * Get value from column using Row::getValue() or custom callback
70
	 * @param Row    	    $row
71
	 * @param Column\Column $column
72
	 * @return bool
73
	 */
74
	private function getValue(Row $row, $column)
75
	{
76
		if (!$this->rowCallback) {
77
			return $row->getValue($column->getColumn());
78
		}
79
80
		return call_user_func_array($this->rowCallback, [$row->getItem(), $column->getColumn()]);
81
	}
82
83
84
	/**
85
	 * @param Row $row
86
	 */
87
	public function add(Row $row)
88
	{
89
		foreach ($this->summary as $key => $sum) {
90
			$column = $this->datagrid->getColumn($key);
91
92
			$value = $this->getValue($row, $column);
93
			$this->summary[$key] += $value;
94
		}
95
	}
96
97
98
	/**
99
	 * @param  string $key
100
	 * @return mixed
101
	 */
102
	public function render($key)
103
	{
104
		/**
105
		 * Renderer function may be used
106
		 */
107
		try {
108
			return $this->useRenderer($key);
109
		} catch (DataGridColumnRendererException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
110
			/**
111
			 * Do not use renderer
112
			 */
113
		}
114
115
		if (!isset($this->summary[$key])) {
116
			return null;
117
		}
118
119
		return number_format(
120
			$this->summary[$key],
121
			$this->format[$key][0],
122
			$this->format[$key][1],
123
			$this->format[$key][2]
124
		);
125
	}
126
127
128
	/**
129
	 * Try to render summary with custom renderer
130
	 * @param  string $key
131
	 * @return mixed
132
	 */
133
	public function useRenderer($key)
134
	{
135
		if (!isset($this->summary[$key])) {
136
			return null;
137
		}
138
139
		$renderer = $this->getRenderer();
140
141
		if (!$renderer) {
142
			throw new DataGridColumnRendererException;
143
		}
144
145
		return call_user_func_array($renderer->getCallback(), [$this->summary[$key], $key]);
146
	}
147
148
149
	/**
150
	 * Return custom renderer callback
151
	 * @return Renderer|null
152
	 */
153
	public function getRenderer()
154
	{
155
		return $this->renderer;
156
	}
157
158
159
	/**
160
	 * Set renderer callback
161
	 * @param callable $renderer
162
	 */
163
	public function setRenderer(callable $renderer)
164
	{
165
		$this->renderer = new Renderer($renderer, NULL);
166
167
		return $this;
168
	}
169
170
171
	/**
172
	 * Set number format
173
	 * @param string $key
174
	 * @param int    $decimals
175
	 * @param string $dec_point
176
	 * @param string $thousands_sep
177
	 */
178
	public function setFormat($key, $decimals = 0, $dec_point = '.', $thousands_sep = ' ')
179
	{
180
		$this->format[$key] = [$decimals, $dec_point, $thousands_sep];
181
182
		return $this;
183
	}
184
185
186
	/**
187
	 * @param  array  $columns
188
	 * @return bool
189
	 */
190
	public function someColumnsExist(array $columns)
191
	{
192
		foreach ($columns as $key => $column) {
193
			if (isset($this->summary[$key])) {
194
				return true;
195
			}
196
		}
197
198
		return false;
199
	}
200
}
201