Completed
Pull Request — master (#320)
by
unknown
03:00
created

ColumnsSummary::getValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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