Completed
Push — master ( e617aa...05ddb9 )
by Pavel
03:24
created

ColumnsSummary::getValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 2
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 Nette\Utils\Callback;
12
use Ublaboo\DataGrid\Column\ColumnNumber;
13
14
class ColumnsSummary
15
{
16
17
	/**
18
	 * @var DataGrid
19
	 */
20
	protected $datagrid;
21
22
	/**
23
	 * @var array
24
	 */
25
	protected $summary;
26
27
	/**
28
	 * @var array
29
	 */
30
	protected $format = [];
31
32
	/**
33
	 * @var NULL|callable
34
	 */
35
	protected $rowCallback;
36
37
38
	/**
39
	 * @param DataGrid $datagrid
40
	 * @param array    $columns
41
	 */
42
	public function __construct(DataGrid $datagrid, array $columns, $rowCallback)
43
	{
44
		$this->summary = array_fill_keys(array_values($columns), 0);
45
		$this->datagrid = $datagrid;
46
		$this->rowCallback = $rowCallback;
47
48
		foreach ($this->summary as $key => $sum) {
49
			$column = $this->datagrid->getColumn($key);
50
51
			if ($column instanceof ColumnNumber) {
52
				$arg = $column->getFormat();
53
				array_unshift($arg, $key);
54
55
				call_user_func_array([$this, 'setFormat'], $arg);
56
			}
57
		}
58
	}
59
60
61
	/**
62
	 * Get value from column using Row::getValue() or custom callback
63
	 * @param Row    	    $row
64
	 * @param Column\Column $column
65
	 * @return bool
66
	 */
67
	private function getValue(Row $row, $column)
68
	{
69
		if (!$this->rowCallback) {
70
			return $row->getValue($column->getColumn());
71
		}
72
73
		return call_user_func_array($this->rowCallback, [$row->getItem(), $column->getColumn()]);
74
	}
75
76
77
	/**
78
	 * @param Row $row
79
	 */
80
	public function add(Row $row)
81
	{
82
		foreach ($this->summary as $key => $sum) {
83
			$column = $this->datagrid->getColumn($key);
84
85
			$value = $this->getValue($row, $column);
86
			$this->summary[$key] += $value;
87
		}
88
	}
89
90
91
	/**
92
	 * @param  string $key
93
	 * @return mixed
94
	 */
95
	public function render($key)
96
	{
97
		if (!isset($this->summary[$key])) {
98
			return NULL;
99
		}
100
101
		return number_format(
102
			$this->summary[$key],
103
			$this->format[$key][0],
104
			$this->format[$key][1],
105
			$this->format[$key][2]
106
		);
107
	}
108
109
110
	/**
111
	 * Set number format
112
	 * @param string $key
113
	 * @param int    $decimals
114
	 * @param string $dec_point
115
	 * @param string $thousands_sep
116
	 */
117
	public function setFormat($key, $decimals = 0, $dec_point = '.', $thousands_sep = ' ')
118
	{
119
		$this->format[$key] = [$decimals, $dec_point, $thousands_sep];
120
121
		return $this;
122
	}
123
124
125
	/**
126
	 * @param  array  $columns
127
	 * @return bool
128
	 */
129
	public function someColumnsExist(array $columns)
130
	{
131
		foreach ($columns as $key => $column) {
132
			if (isset($this->summary[$key])) {
133
				return TRUE;
134
			}
135
		}
136
137
		return FALSE;
138
	}
139
140
}
141