Completed
Push — master ( 34cd5e...81adef )
by Arjay
02:27
created

DataProcessor::escapeColumns()   B

Complexity

Conditions 5
Paths 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 10
nc 1
nop 1
dl 0
loc 16
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace Yajra\Datatables\Processors;
4
5
use Illuminate\Support\Arr;
6
use Illuminate\Support\Facades\Config;
7
use Yajra\Datatables\Helper;
8
9
/**
10
 * Class DataProcessor.
11
 *
12
 * @package Yajra\Datatables
13
 * @author  Arjay Angeles <[email protected]>
14
 */
15
class DataProcessor
16
{
17
    /**
18
     * @var int
19
     */
20
    protected $start;
21
22
    /**
23
     * Columns to escape value.
24
     *
25
     * @var array
26
     */
27
    protected $escapeColumns = [];
28
29
    /**
30
     * Processed data output
31
     *
32
     * @var array
33
     */
34
    protected $output = [];
35
36
    /**
37
     * @var array
38
     */
39
    protected $appendColumns = [];
40
41
    /**
42
     * @var array
43
     */
44
    protected $editColumns = [];
45
46
    /**
47
     * @var array
48
     */
49
    protected $excessColumns = [];
50
51
    /**
52
     * @var mixed
53
     */
54
    protected $results;
55
56
    /**
57
     * @var array
58
     */
59
    protected $templates;
60
61
    /**
62
     * @var bool
63
     */
64
    protected $includeIndex;
65
66
    /**
67
     * @var array
68
     */
69
    protected $rawColumns;
70
71
    /**
72
     * @param mixed $results
73
     * @param array $columnDef
74
     * @param array $templates
75
     * @param int $start
76
     */
77
    public function __construct($results, array $columnDef, array $templates, $start)
78
    {
79
        $this->results       = $results;
80
        $this->appendColumns = $columnDef['append'];
81
        $this->editColumns   = $columnDef['edit'];
82
        $this->excessColumns = $columnDef['excess'];
83
        $this->escapeColumns = $columnDef['escape'];
84
        $this->includeIndex  = $columnDef['index'];
85
        $this->rawColumns    = $columnDef['raw'];
86
        $this->templates     = $templates;
87
        $this->start         = $start;
88
    }
89
90
    /**
91
     * Process data to output on browser
92
     *
93
     * @param bool $object
94
     * @return array
95
     */
96
    public function process($object = false)
97
    {
98
        $this->output = [];
99
        $indexColumn  = Config::get('datatables.index_column', 'DT_Row_Index');
100
101
        foreach ($this->results as $row) {
102
            $data  = Helper::convertToArray($row);
103
            $value = $this->addColumns($data, $row);
104
            $value = $this->editColumns($value, $row);
105
            $value = $this->setupRowVariables($value, $row);
106
            $value = $this->removeExcessColumns($value);
107
108
            if ($this->includeIndex) {
109
                $value[$indexColumn] = ++$this->start;
110
            }
111
112
            $this->output[] = $object ? $value : $this->flatten($value);
113
        }
114
115
        return $this->escapeColumns($this->output);
116
    }
117
118
    /**
119
     * Process add columns.
120
     *
121
     * @param mixed $data
122
     * @param mixed $row
123
     * @return array
124
     */
125 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...
126
    {
127
        foreach ($this->appendColumns as $key => $value) {
128
            $value['content'] = Helper::compileContent($value['content'], $data, $row);
129
            $data             = Helper::includeInArray($value, $data);
130
        }
131
132
        return $data;
133
    }
134
135
    /**
136
     * Process edit columns.
137
     *
138
     * @param mixed $data
139
     * @param mixed $row
140
     * @return array
141
     */
142 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...
143
    {
144
        foreach ($this->editColumns as $key => $value) {
145
            $value['content'] = Helper::compileContent($value['content'], $data, $row);
146
            Arr::set($data, $value['name'], $value['content']);
147
        }
148
149
        return $data;
150
    }
151
152
    /**
153
     * Setup additional DT row variables.
154
     *
155
     * @param mixed $data
156
     * @param mixed $row
157
     * @return array
158
     */
159
    protected function setupRowVariables($data, $row)
160
    {
161
        $processor = new RowProcessor($data, $row);
162
163
        return $processor
164
            ->rowValue('DT_RowId', $this->templates['DT_RowId'])
165
            ->rowValue('DT_RowClass', $this->templates['DT_RowClass'])
166
            ->rowData('DT_RowData', $this->templates['DT_RowData'])
167
            ->rowData('DT_RowAttr', $this->templates['DT_RowAttr'])
168
            ->getData();
169
    }
170
171
    /**
172
     * Remove declared hidden columns.
173
     *
174
     * @param array $data
175
     * @return array
176
     */
177
    protected function removeExcessColumns(array $data)
178
    {
179
        foreach ($this->excessColumns as $value) {
180
            unset($data[$value]);
181
        }
182
183
        return $data;
184
    }
185
186
    /**
187
     * Flatten array with exceptions.
188
     *
189
     * @param array $array
190
     * @return array
191
     */
192
    public function flatten(array $array)
193
    {
194
        $return     = [];
195
        $exceptions = ['DT_RowId', 'DT_RowClass', 'DT_RowData', 'DT_RowAttr'];
196
197
        foreach ($array as $key => $value) {
198
            if (in_array($key, $exceptions)) {
199
                $return[$key] = $value;
200
            } else {
201
                $return[] = $value;
202
            }
203
        }
204
205
        return $return;
206
    }
207
208
    /**
209
     * Escape column values as declared.
210
     *
211
     * @param array $output
212
     * @return array
213
     */
214
    protected function escapeColumns(array $output)
215
    {
216
        return array_map(function ($row) {
217
            if ($this->escapeColumns == '*') {
218
                $row = $this->escapeRow($row);
219
            } else {
220
                foreach ($this->escapeColumns as $key) {
221
                    if (array_get($row, $key) && ! in_array($key, $this->rawColumns)) {
222
                        array_set($row, $key, e(array_get($row, $key)));
223
                    }
224
                }
225
            }
226
227
            return $row;
228
        }, $output);
229
    }
230
231
    /**
232
     * Escape all values of row.
233
     *
234
     * @param array $row
235
     * @return array
236
     */
237
    protected function escapeRow(array $row)
238
    {
239
        foreach ($row as $key => $value) {
240
            if (is_array($value)) {
241
                $row[$key] = $this->escapeRow($value);
242
            } else {
243
                if (! in_array($key, $this->rawColumns)) {
244
                    $row[$key] = e($value);
245
                }
246
            }
247
        }
248
249
        return $row;
250
    }
251
}
252