Completed
Pull Request — master (#270)
by
unknown
27:04 queued 11:59
created

PrepareData::getData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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