CsvDataModel   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 68.42%

Importance

Changes 0
Metric Value
wmc 8
c 0
b 0
f 0
lcom 1
cbo 1
dl 0
loc 81
rs 10
ccs 13
cts 19
cp 0.6842

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getSimpleData() 0 14 3
A getHeader() 0 10 2
A getRow() 0 10 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;
12
13 1
class CsvDataModel
14
{
15
16
	/**
17
	 * @var array
18
	 */
19
	protected $data;
20
21
	/**
22
	 * @var Column\Column[]
23
	 */
24
	protected $columns;
25
26
	/** @var Nette\Localization\ITranslator */
27
	protected $translator;
28
29
30
	/**
31
	 * @param array $data
32
	 * @param array $columns
33
	 */
34
	public function __construct(array $data, array $columns, Nette\Localization\ITranslator $translator)
35
	{
36 1
		$this->data = $data;
37 1
		$this->columns = $columns;
38 1
		$this->translator = $translator;
39 1
	}
40
41
42
	/**
43
	 * Get data with header and "body"
44
	 * @return array
45
	 */
46
	public function getSimpleData($include_header = true)
47
	{
48 1
		$return = [];
49
50 1
		if ($include_header) {
51 1
			$return[] = $this->getHeader();
52
		}
53
54 1
		foreach ($this->data as $item) {
55
			$return[] = $this->getRow($item);
56
		}
57
58 1
		return $return;
59
	}
60
61
62
	/**
63
	 * Get data header
64
	 * @return array
65
	 */
66
	public function getHeader()
67
	{
68 1
		$header = [];
69
70 1
		foreach ($this->columns as $column) {
71
			$header[] = $this->translator->translate($column->getName());
0 ignored issues
show
Bug introduced by
Consider using $column->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
72
		}
73
74 1
		return $header;
75
	}
76
77
78
	/**
79
	 * Get item values saved into row
80
	 * @param  mixed $item
81
	 * @return array
82
	 */
83
	public function getRow($item)
84
	{
85
		$row = [];
86
87
		foreach ($this->columns as $column) {
88
			$row[] = strip_tags($column->render($item));
89
		}
90
91
		return $row;
92
	}
93
}
94