Completed
Pull Request — master (#227)
by Martin
04:49 queued 01:55
created

ColumnsSummary::getValue()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 16
rs 9.4285
cc 3
eloc 8
nc 3
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\ColumnsSummary;
10
11
use Ublaboo\DataGrid\Column\Column;
12
use Ublaboo\DataGrid\Column\ColumnNumber;
13
use Ublaboo\DataGrid\Row;
14
15
class ColumnsSummary
16
{
17
	/**
18
	 * @var array
19
	 */
20
	protected $columns;
21
22
23
	/**
24
	 * @param array  $columns
25
	 */
26
	public function __construct(array $columns)
27
	{
28
		$this->columns = array_fill_keys(array_values($columns), 0);
29
	}
30
31
32
	/**
33
	 * @param  Column  $column
34
	 * @return float
35
	 */
36
	public function getValue(Column $column)
37
	{
38
		$key = $column->getColumnName();
39
40
		if (!isset($this->columns[$key])) {
41
			return NULL;
42
		}
43
44
		$value = $this->columns[$key];
45
46
		if ($column instanceof ColumnNumber) {
47
			$value = $column->formatValue($value);
48
		}
49
50
		return $value;
51
	}
52
53
54
	/**
55
	 * @param Row  $row
56
	 */
57
	public function summarize(Row $row)
58
	{
59
		foreach ($this->columns as $key => $value) {
60
			if (!is_numeric($value = $row->getValue($key))) {
61
				continue;
62
			}
63
64
			$this->columns[$key] += $value;
65
		}
66
	}
67
}
68