1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ZfcDatagrid; |
4
|
|
|
|
5
|
|
|
use Zend\I18n\Translator\Translator; |
6
|
|
|
|
7
|
|
|
class PrepareData |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var array |
11
|
|
|
*/ |
12
|
|
|
private $columns = []; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var array |
16
|
|
|
*/ |
17
|
|
|
private $data = []; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var array null |
21
|
|
|
*/ |
22
|
|
|
private $dataPrepared; |
23
|
|
|
|
24
|
|
|
private $rendererName; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var Translator |
28
|
|
|
*/ |
29
|
|
|
private $translator; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param array $data |
33
|
|
|
* @param array $columns |
34
|
|
|
*/ |
35
|
|
|
public function __construct(array $data, array $columns) |
36
|
|
|
{ |
37
|
|
|
$this->setData($data); |
38
|
|
|
$this->setColumns($columns); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param array $columns |
43
|
|
|
*/ |
44
|
|
|
public function setColumns(array $columns) |
45
|
|
|
{ |
46
|
|
|
$this->columns = $columns; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return array |
51
|
|
|
*/ |
52
|
|
|
public function getColumns() |
53
|
|
|
{ |
54
|
|
|
return $this->columns; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param array $data |
59
|
|
|
*/ |
60
|
|
|
public function setData(array $data) |
61
|
|
|
{ |
62
|
|
|
$this->data = $data; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param bool $raw |
67
|
|
|
* |
68
|
|
|
* @return array |
69
|
|
|
*/ |
70
|
|
|
public function getData($raw = false) |
71
|
|
|
{ |
72
|
|
|
if (true === $raw) { |
73
|
|
|
return $this->data; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$this->prepare(); |
77
|
|
|
|
78
|
|
|
return $this->dataPrepared; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param string $name |
83
|
|
|
*/ |
84
|
|
|
public function setRendererName($name = null) |
85
|
|
|
{ |
86
|
|
|
$this->rendererName = $name; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
|
|
public function getRendererName() |
93
|
|
|
{ |
94
|
|
|
return $this->rendererName; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param Translator $translator |
99
|
|
|
* |
100
|
|
|
* @throws \InvalidArgumentException |
101
|
|
|
*/ |
102
|
|
View Code Duplication |
public function setTranslator($translator) |
|
|
|
|
103
|
|
|
{ |
104
|
|
|
if (!$translator instanceof Translator && !$translator instanceof \Zend\I18n\Translator\TranslatorInterface) { |
105
|
|
|
throw new \InvalidArgumentException('Translator must be an instanceof "Zend\I18n\Translator\Translator" or "Zend\I18n\Translator\TranslatorInterface"'); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$this->translator = $translator; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return \Zend\I18n\Translator\Translator |
113
|
|
|
*/ |
114
|
|
|
public function getTranslator() |
115
|
|
|
{ |
116
|
|
|
return $this->translator; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Return true if preparing executed, false if already done! |
121
|
|
|
* |
122
|
|
|
* @throws \Exception |
123
|
|
|
* |
124
|
|
|
* @return bool |
125
|
|
|
*/ |
126
|
|
|
public function prepare() |
127
|
|
|
{ |
128
|
|
|
if (is_array($this->dataPrepared)) { |
129
|
|
|
return false; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
$data = $this->data; |
133
|
|
|
|
134
|
|
|
foreach ($data as $key => &$row) { |
135
|
|
|
$row = (array) $row; |
136
|
|
|
|
137
|
|
|
$ids = []; |
138
|
|
|
|
139
|
|
|
foreach ($this->getColumns() as $col) { |
140
|
|
|
/* @var $col \ZfcDatagrid\Column\AbstractColumn */ |
141
|
|
|
|
142
|
|
|
if (isset($row[$col->getUniqueId()]) && $col->isIdentity() === true) { |
143
|
|
|
$ids[] = $row[$col->getUniqueId()]; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/* |
147
|
|
|
* Maybe the data come not from another DataSource? |
148
|
|
|
*/ |
149
|
|
|
if ($col instanceof Column\ExternalData) { |
150
|
|
|
/* @var $col \ZfcDatagrid\Column\ExternalData */ |
151
|
|
|
// @todo improve the interface... |
152
|
|
|
$dataPopulation = $col->getDataPopulation(); |
153
|
|
|
|
154
|
|
|
foreach ($dataPopulation->getObjectParametersColumn() as $parameter) { |
155
|
|
|
$dataPopulation->setObjectParameter($parameter['objectParameterName'], $row[$parameter['column']->getUniqueId()]); |
156
|
|
|
} |
157
|
|
|
$row[$col->getUniqueId()] = $dataPopulation->toString(); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
if (!isset($row[$col->getUniqueId()])) { |
161
|
|
|
$row[$col->getUniqueId()] = ''; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/* |
165
|
|
|
* Replace |
166
|
|
|
*/ |
167
|
|
|
if ($col->hasReplaceValues() === true) { |
168
|
|
|
$replaceValues = $col->getReplaceValues(); |
169
|
|
|
|
170
|
|
|
if (is_array($row[$col->getUniqueId()])) { |
171
|
|
|
foreach ($row[$col->getUniqueId()] as &$value) { |
172
|
|
|
if (isset($replaceValues[$value])) { |
173
|
|
|
$value = $replaceValues[$value]; |
174
|
|
|
} elseif ($col->notReplacedGetEmpty() === true) { |
175
|
|
|
$value = ''; |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
} else { |
179
|
|
|
if (isset($replaceValues[$row[$col->getUniqueId()]])) { |
180
|
|
|
$row[$col->getUniqueId()] = $replaceValues[$row[$col->getUniqueId()]]; |
181
|
|
|
} elseif ($col->notReplacedGetEmpty() === true) { |
182
|
|
|
$row[$col->getUniqueId()] = ''; |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/* |
188
|
|
|
* Type converting |
189
|
|
|
*/ |
190
|
|
|
if ($this->getRendererName() != 'PHPExcel') { |
191
|
|
|
$row[$col->getUniqueId()] = $col->getType()->getUserValue($row[$col->getUniqueId()]); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/* |
195
|
|
|
* Translate (nach typ convertierung -> PhpArray...) |
196
|
|
|
*/ |
197
|
|
|
if ($col->isTranslationEnabled() === true) { |
198
|
|
|
if (is_array($row[$col->getUniqueId()])) { |
199
|
|
|
foreach ($row[$col->getUniqueId()] as &$value) { |
200
|
|
|
if (is_array($value)) { |
201
|
|
|
continue; |
202
|
|
|
} |
203
|
|
|
$value = $this->getTranslator()->translate($value); |
204
|
|
|
} |
205
|
|
|
} else { |
206
|
|
|
$row[$col->getUniqueId()] = $this->getTranslator()->translate($row[$col->getUniqueId()]); |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/* |
211
|
|
|
* Trim the values |
212
|
|
|
*/ |
213
|
|
|
if (is_array($row[$col->getUniqueId()])) { |
214
|
|
|
array_walk_recursive($row[$col->getUniqueId()], function (&$value) { |
215
|
|
|
if (!is_object($value)) { |
216
|
|
|
$value = trim($value); |
217
|
|
|
} |
218
|
|
|
}); |
219
|
|
|
} elseif (!is_object($row[$col->getUniqueId()])) { |
220
|
|
|
$row[$col->getUniqueId()] = trim($row[$col->getUniqueId()]); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/* |
224
|
|
|
* Custom formatter |
225
|
|
|
*/ |
226
|
|
|
if ($col->hasFormatters() === true) { |
227
|
|
|
foreach ($col->getFormatters() as $formatter) { |
228
|
|
|
$formatter->setRowData($row); |
229
|
|
|
$formatter->setRendererName($this->getRendererName()); |
230
|
|
|
|
231
|
|
|
$row[$col->getUniqueId()] = $formatter->format($col); |
232
|
|
|
} |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
// Concat all identity columns |
237
|
|
|
if ($ids) { |
|
|
|
|
238
|
|
|
$data[$key]['idConcated'] = implode('~', $ids); |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
$this->dataPrepared = $data; |
243
|
|
|
|
244
|
|
|
return true; |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.