-
Notifications
You must be signed in to change notification settings - Fork 0
/
Format.php
161 lines (139 loc) · 3.75 KB
/
Format.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
namespace Polinome\Trieur;
use Solire\Conf\Conf;
/**
* Format the data coming from the source.
*
* @author polinome <[email protected]>
* @license MIT http://mit-license.org/
*/
class Format
{
/**
* Constructor.
*
* @param Columns $columns Columns list
*/
public function __construct(Columns $columns)
{
$this->columns = $columns;
}
/**
* Formate a source data.
*
* @param array $data The source data
*
* @return array
*/
public function format($data)
{
$dataFormated = [];
foreach ($data as $row) {
$rowFormated = $this->formateRow($row);
if ($rowFormated) {
$dataFormated[] = $rowFormated;
}
}
return $dataFormated;
}
/**
* Formate a source row.
*
* @param array $row The source row
*
* @return type
*/
protected function formateRow($row)
{
$rowFormated = [];
foreach ($this->columns as $column) {
if (isset($column->hide) && $column->hide) {
continue;
}
$cellFormated = $this->formateCell($row, $column);
$rowFormated[$column->name] = $cellFormated;
}
return $rowFormated;
}
/**
* Formate a source cell.
*
* @param array $row The source row
* @param Conf $column The cell's column
*
* @return string
*/
protected function formateCell($row, Conf $column)
{
if (!isset($column->format)) {
return $this->getCell($row, $column);
}
if (!isset($column->format->class)) {
throw new Exception(
sprintf(
'Undefined format class for column [%s]',
$column->name
)
);
}
$className = $this->getFormatClassName($column->format->class);
if ($className === null) {
throw new Exception(
sprintf(
'Format class [%s] for column [%s] does not exist',
$column->format->class,
$column->name
)
);
}
$column->format->class = $className;
if (!is_subclass_of($column->format->class, '\Polinome\Trieur\AbstractFormat')) {
throw new Exception(
sprintf(
'Format class [%s] does not extend abstract class [%s]',
$column->format->class,
'\Polinome\Trieur\AbstractFormat'
)
);
}
$formatInstance = new $column->format->class($column->format, $row, $this->getCell($row, $column));
return $formatInstance->render();
}
/**
* Get a source cell by its row and column.
*
* @param array $row The source row
* @param Conf $column The cell's column
*
* @return string
*
* @throws Exception If the column index doesn't exist in the row
*/
protected function getCell($row, Conf $column)
{
if (!isset($row[$column->sourceName])) {
return '';
}
return $row[$column->sourceName];
}
/**
* Returns the name of the filter class.
*
* @param string $formatType The filter type (Contain, DateRange etc.)
*
* @return string
*/
private function getFormatClassName($formatType)
{
$className = $formatType;
if (class_exists($className)) {
return $className;
}
$r = new \ReflectionClass($this);
$className = $r->getName() . '\\' . $formatType;
if (class_exists($className)) {
return $className;
}
return null;
}
}