Completed
Pull Request — master (#271)
by Martin
38:47 queued 23:38
created

PrepareData::getRouter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
namespace ZfcDatagrid;
3
4
use Zend\I18n\Translator\Translator;
5
use Zend\Router\RouteStackInterface;
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
     * @var \Zend\Router\RouteStackInterface
33
     */
34
    private $router;
35
36
    /**
37
     * @param array $data
38
     * @param array $columns
39
     */
40
    public function __construct(array $data, array $columns)
41
    {
42
        $this->setData($data);
43
        $this->setColumns($columns);
44
    }
45
46
    /**
47
     * @param array $columns
48
     */
49
    public function setColumns(array $columns)
50
    {
51
        $this->columns = $columns;
52
    }
53
54
    /**
55
     * @return array
56
     */
57
    public function getColumns()
58
    {
59
        return $this->columns;
60
    }
61
62
    /**
63
     * @param array $data
64
     */
65
    public function setData(array $data)
66
    {
67
        $this->data = $data;
68
    }
69
70
    /**
71
     * @param bool $raw
72
     *
73
     * @return array
74
     */
75
    public function getData($raw = false)
76
    {
77
        if (true === $raw) {
78
            return $this->data;
79
        }
80
81
        $this->prepare();
82
83
        return $this->dataPrepared;
84
    }
85
86
    /**
87
     * @param string $name
88
     */
89
    public function setRendererName($name = null)
90
    {
91
        $this->rendererName = $name;
92
    }
93
94
    /**
95
     * @return string
96
     */
97
    public function getRendererName()
98
    {
99
        return $this->rendererName;
100
    }
101
102
    /**
103
     * @param Translator $translator
104
     *
105
     * @throws \InvalidArgumentException
106
     */
107 View Code Duplication
    public function setTranslator($translator)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
108
    {
109
        if (!$translator instanceof Translator && !$translator instanceof \Zend\I18n\Translator\TranslatorInterface) {
110
            throw new \InvalidArgumentException('Translator must be an instanceof "Zend\I18n\Translator\Translator" or "Zend\I18n\Translator\TranslatorInterface"');
111
        }
112
113
        $this->translator = $translator;
114
    }
115
116
    /**
117
     * @return \Zend\I18n\Translator\Translator
118
     */
119
    public function getTranslator()
120
    {
121
        return $this->translator;
122
    }
123
124
    /**
125
     * @param \Zend\Router\RouteStackInterface $router
126
     */
127
    public function setRouter(RouteStackInterface $router)
128
    {
129
        $this->router = $router;
130
    }
131
132
    /**
133
     * @return \Zend\Router\RouteStackInterface
134
     */
135
    public function getRouter()
136
    {
137
        return $this->router;
138
    }
139
140
    /**
141
     * Return true if preparing executed, false if already done!
142
     *
143
     * @throws \Exception
144
     *
145
     * @return bool
146
     */
147
    public function prepare()
148
    {
149
        if (is_array($this->dataPrepared)) {
150
            return false;
151
        }
152
153
        $data = $this->data;
154
155
        foreach ($data as $key => &$row) {
156
            $row = (array) $row;
157
158
            $ids = [];
159
160
            foreach ($this->getColumns() as $col) {
161
                /* @var $col \ZfcDatagrid\Column\AbstractColumn */
162
163
                if (isset($row[$col->getUniqueId()]) && $col->isIdentity() === true) {
164
                    $ids[] = $row[$col->getUniqueId()];
165
                }
166
167
                /*
168
                 * Maybe the data come not from another DataSource?
169
                 */
170
                if ($col instanceof Column\ExternalData) {
171
                    /* @var $col \ZfcDatagrid\Column\ExternalData */
172
                    // @todo improve the interface...
173
                    $dataPopulation = $col->getDataPopulation();
174
175
                    foreach ($dataPopulation->getObjectParametersColumn() as $parameter) {
176
                        $dataPopulation->setObjectParameter($parameter['objectParameterName'], $row[$parameter['column']->getUniqueId()]);
177
                    }
178
                    $row[$col->getUniqueId()] = $dataPopulation->toString();
179
                }
180
181
                if (!isset($row[$col->getUniqueId()])) {
182
                    $row[$col->getUniqueId()] = '';
183
                }
184
185
                /*
186
                 * Replace
187
                 */
188
                if ($col->hasReplaceValues() === true) {
189
                    $replaceValues = $col->getReplaceValues();
190
191
                    if (is_array($row[$col->getUniqueId()])) {
192
                        foreach ($row[$col->getUniqueId()] as &$value) {
193
                            if (isset($replaceValues[$value])) {
194
                                $value = $replaceValues[$value];
195
                            } elseif ($col->notReplacedGetEmpty() === true) {
196
                                $value = '';
197
                            }
198
                        }
199
                    } else {
200
                        if (isset($replaceValues[$row[$col->getUniqueId()]])) {
201
                            $row[$col->getUniqueId()] = $replaceValues[$row[$col->getUniqueId()]];
202
                        } elseif ($col->notReplacedGetEmpty() === true) {
203
                            $row[$col->getUniqueId()] = '';
204
                        }
205
                    }
206
                }
207
208
                /*
209
                 * Type converting
210
                 */
211
                if ($this->getRendererName() != 'PHPExcel') {
212
                    $row[$col->getUniqueId()] = $col->getType()->getUserValue($row[$col->getUniqueId()]);
213
                }
214
215
                /*
216
                 * Translate (nach typ convertierung -> PhpArray...)
217
                 */
218
                if ($col->isTranslationEnabled() === true) {
219
                    if (is_array($row[$col->getUniqueId()])) {
220
                        foreach ($row[$col->getUniqueId()] as &$value) {
221
                            if (is_array($value)) {
222
                                continue;
223
                            }
224
                            $value = $this->getTranslator()->translate($value);
225
                        }
226
                    } else {
227
                        $row[$col->getUniqueId()] = $this->getTranslator()->translate($row[$col->getUniqueId()]);
228
                    }
229
                }
230
231
                /*
232
                 * Trim the values
233
                 */
234
                if (is_array($row[$col->getUniqueId()])) {
235
                    array_walk_recursive($row[$col->getUniqueId()], function (&$value) {
236
                        if (!is_object($value)) {
237
                            $value = trim($value);
238
                        }
239
                    });
240
                } elseif (!is_object($row[$col->getUniqueId()])) {
241
                    $row[$col->getUniqueId()] = trim($row[$col->getUniqueId()]);
242
                }
243
244
                /*
245
                 * Custom formatter
246
                 */
247
                if ($col->hasFormatters() === true) {
248
                    foreach ($col->getFormatters() as $formatter) {
249
                        if ($formatter instanceof Column\Formatter\RouterInterface
250
                            && $this->getRouter() instanceof RouteStackInterface
251
                        ) {
252
                            /** @var Column\Formatter\RouterInterface */
253
                            $formatter->setRouter($this->getRouter());
254
                        }
255
                        $formatter->setRowData($row);
256
                        $formatter->setRendererName($this->getRendererName());
257
258
                        $row[$col->getUniqueId()] = $formatter->format($col);
259
                    }
260
                }
261
            }
262
263
            // Concat all identity columns
264
            if ($ids) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $ids of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
265
                $data[$key]['idConcated'] = implode('~', $ids);
266
            }
267
        }
268
269
        $this->dataPrepared = $data;
270
271
        return true;
272
    }
273
}
274