Completed
Push — master ( f9a971...920cd0 )
by Arjay
01:43
created

DataProcessor::process()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 1
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
1
<?php
2
3
namespace Yajra\DataTables\Processors;
4
5
use Illuminate\Support\Arr;
6
use Yajra\DataTables\Utilities\Helper;
7
8
class DataProcessor
9
{
10
    /**
11
     * @var int
12
     */
13
    protected $start;
14
15
    /**
16
     * Columns to escape value.
17
     *
18
     * @var array
19
     */
20
    protected $escapeColumns = [];
21
22
    /**
23
     * Processed data output.
24
     *
25
     * @var array
26
     */
27
    protected $output = [];
28
29
    /**
30
     * @var array
31
     */
32
    protected $appendColumns = [];
33
34
    /**
35
     * @var array
36
     */
37
    protected $editColumns = [];
38
39
    /**
40
     * @var array
41
     */
42
    protected $excessColumns = [];
43
44
    /**
45
     * @var mixed
46
     */
47
    protected $results;
48
49
    /**
50
     * @var array
51
     */
52
    protected $templates;
53
54
    /**
55
     * @var bool
56
     */
57
    protected $includeIndex;
58
59
    /**
60
     * @var array
61
     */
62
    protected $rawColumns;
63
64
    /**
65
     * @var array
66
     */
67
    protected $exceptions = ['DT_RowId', 'DT_RowClass', 'DT_RowData', 'DT_RowAttr'];
68
69
    /**
70
     * @param mixed $results
71
     * @param array $columnDef
72
     * @param array $templates
73
     * @param int   $start
74
     */
75
    public function __construct($results, array $columnDef, array $templates, $start)
76
    {
77
        $this->results       = $results;
78
        $this->appendColumns = $columnDef['append'];
79
        $this->editColumns   = $columnDef['edit'];
80
        $this->excessColumns = $columnDef['excess'];
81
        $this->onlyColumns   = $columnDef['only'];
0 ignored issues
show
Bug introduced by
The property onlyColumns does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
82
        $this->escapeColumns = $columnDef['escape'];
83
        $this->includeIndex  = $columnDef['index'];
84
        $this->rawColumns    = $columnDef['raw'];
85
        $this->templates     = $templates;
86
        $this->start         = $start;
87
    }
88
89
    /**
90
     * Process data to output on browser.
91
     *
92
     * @param bool $object
93
     * @return array
94
     */
95
    public function process($object = false)
96
    {
97
        $this->output = [];
98
        foreach ($this->results as $row) {
99
            $data           = $this->escapeRow(Helper::convertToArray($row));
100
            $value          = $this->addColumns($data, $row);
101
            $value          = $this->editColumns($value, $row);
102
            $value          = $this->setupRowVariables($value, $row);
103
            $value          = $this->selectOnlyNeededColumns($value);
104
            $value          = $this->removeExcessColumns($value);
105
            $value          = $this->addIndexColumn($value);
106
            $this->output[] = $object ? $value : $this->flatten($value);
107
        }
108
109
        return $this->output;
110
    }
111
112
    /**
113
     * Process add columns.
114
     *
115
     * @param mixed $data
116
     * @param mixed $row
117
     * @return array
118
     */
119 View Code Duplication
    protected function addColumns($data, $row)
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...
120
    {
121
        foreach ($this->appendColumns as $key => $value) {
122
            $value['content'] = Helper::compileContent($value['content'], $data, $row, $this->shouldEscapeColumn($key));
123
            $data             = Helper::includeInArray($value, $data);
124
        }
125
126
        return $data;
127
    }
128
129
    /**
130
     * Process add index column.
131
     *
132
     * @param mixed $data
133
     * @return array
134
     */
135
    protected function addIndexColumn($data)
136
    {
137
        $indexColumn  = config('datatables.index_column', 'DT_RowIndex');
138
139
        if ($this->includeIndex) {
140
            $data[$indexColumn] = ++$this->start;
141
        }
142
143
        return $data;
144
    }
145
146
    /**
147
     * Process edit columns.
148
     *
149
     * @param mixed $data
150
     * @param mixed $row
151
     * @return array
152
     */
153 View Code Duplication
    protected function editColumns($data, $row)
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...
154
    {
155
        foreach ($this->editColumns as $key => $value) {
156
            $value['content'] = Helper::compileContent($value['content'], $data, $row, $this->shouldEscapeColumn($key));
157
            Arr::set($data, $value['name'], $value['content']);
158
        }
159
160
        return $data;
161
    }
162
163
    /**
164
     * Setup additional DT row variables.
165
     *
166
     * @param mixed $data
167
     * @param mixed $row
168
     * @return array
169
     */
170
    protected function setupRowVariables($data, $row)
171
    {
172
        $processor = new RowProcessor($data, $row);
173
174
        return $processor
175
            ->rowValue('DT_RowId', $this->templates['DT_RowId'])
176
            ->rowValue('DT_RowClass', $this->templates['DT_RowClass'])
177
            ->rowData('DT_RowData', $this->templates['DT_RowData'])
178
            ->rowData('DT_RowAttr', $this->templates['DT_RowAttr'])
179
            ->getData();
180
    }
181
182
    /**
183
     * Get only needed columns.
184
     *
185
     * @param array $data
186
     * @return array
187
     */
188
    protected function selectOnlyNeededColumns(array $data)
189
    {
190
        if (is_null($this->onlyColumns)) {
191
            return $data;
192
        } else {
193
            return array_intersect_key($data, array_flip(array_merge($this->onlyColumns, $this->exceptions)));
194
        }
195
    }
196
197
    /**
198
     * Remove declared hidden columns.
199
     *
200
     * @param array $data
201
     * @return array
202
     */
203
    protected function removeExcessColumns(array $data)
204
    {
205
        foreach ($this->excessColumns as $value) {
206
            unset($data[$value]);
207
        }
208
209
        return $data;
210
    }
211
212
    /**
213
     * Flatten array with exceptions.
214
     *
215
     * @param array $array
216
     * @return array
217
     */
218
    public function flatten(array $array)
219
    {
220
        $return = [];
221
222
        foreach ($array as $key => $value) {
223
            if (in_array($key, $this->exceptions)) {
224
                $return[$key] = $value;
225
            } else {
226
                $return[] = $value;
227
            }
228
        }
229
230
        return $return;
231
    }
232
233
    /**
234
     * Escape all values of row.
235
     *
236
     * @param array $row
237
     * @return array
238
     */
239
    protected function escapeRow(array $row)
240
    {
241
        $arrayDot = array_filter(array_dot($row));
242
243
        foreach ($arrayDot as $key => $value) {
244
            if ($this->shouldEscapeColumn($key)) {
245
                $arrayDot[$key] = e($value);
246
            }
247
        }
248
249
        foreach ($arrayDot as $key => $value) {
250
            array_set($row, $key, $value);
251
        }
252
253
        return $row;
254
    }
255
256
    /**
257
     * Whether to escape column or no.
258
     *
259
     * @param string $key
260
     * @return bool
261
     */
262
    protected function shouldEscapeColumn($key)
263
    {
264
        if ($this->escapeColumns === '*') {
265
            return ! in_array($key, $this->rawColumns); // escape if is not a raw column
266
        } elseif (is_array($this->escapeColumns)) {
267
            return in_array($key, array_diff($this->escapeColumns, $this->rawColumns));
268
        } else {
269
            return false;
270
        }
271
    }
272
}
273