Completed
Push — master ( 928d8a...2f7023 )
by Arjay
07:15
created

DataProcessor::escapeColumns()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 1
nop 1
dl 0
loc 15
rs 9.2
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
     * @param mixed $results
66
     * @param array $columnDef
67
     * @param array $templates
68
     * @param int   $start
69
     */
70
    public function __construct($results, array $columnDef, array $templates, $start)
71
    {
72
        $this->results       = $results;
73
        $this->appendColumns = $columnDef['append'];
74
        $this->editColumns   = $columnDef['edit'];
75
        $this->excessColumns = $columnDef['excess'];
76
        $this->escapeColumns = $columnDef['escape'];
77
        $this->includeIndex  = $columnDef['index'];
78
        $this->rawColumns    = $columnDef['raw'];
79
        $this->templates     = $templates;
80
        $this->start         = $start;
81
    }
82
83
    /**
84
     * Process data to output on browser.
85
     *
86
     * @param bool $object
87
     * @return array
88
     */
89
    public function process($object = false)
90
    {
91
        $this->output = [];
92
        $indexColumn  = config('datatables.index_column', 'DT_Row_Index');
93
94
        foreach ($this->results as $row) {
95
            $data  = Helper::convertToArray($row);
96
            $value = $this->addColumns($data, $row);
97
            $value = $this->editColumns($value, $row);
98
            $value = $this->setupRowVariables($value, $row);
99
            $value = $this->removeExcessColumns($value);
100
101
            if ($this->includeIndex) {
102
                $value[$indexColumn] = ++$this->start;
103
            }
104
105
            $this->output[] = $object ? $value : $this->flatten($value);
106
        }
107
108
        return $this->escapeColumns($this->output);
109
    }
110
111
    /**
112
     * Process add columns.
113
     *
114
     * @param mixed $data
115
     * @param mixed $row
116
     * @return array
117
     */
118 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...
119
    {
120
        foreach ($this->appendColumns as $key => $value) {
121
            $value['content'] = Helper::compileContent($value['content'], $data, $row);
122
            $data             = Helper::includeInArray($value, $data);
123
        }
124
125
        return $data;
126
    }
127
128
    /**
129
     * Process edit columns.
130
     *
131
     * @param mixed $data
132
     * @param mixed $row
133
     * @return array
134
     */
135 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...
136
    {
137
        foreach ($this->editColumns as $key => $value) {
138
            $value['content'] = Helper::compileContent($value['content'], $data, $row);
139
            Arr::set($data, $value['name'], $value['content']);
140
        }
141
142
        return $data;
143
    }
144
145
    /**
146
     * Setup additional DT row variables.
147
     *
148
     * @param mixed $data
149
     * @param mixed $row
150
     * @return array
151
     */
152
    protected function setupRowVariables($data, $row)
153
    {
154
        $processor = new RowProcessor($data, $row);
155
156
        return $processor
157
            ->rowValue('DT_RowId', $this->templates['DT_RowId'])
158
            ->rowValue('DT_RowClass', $this->templates['DT_RowClass'])
159
            ->rowData('DT_RowData', $this->templates['DT_RowData'])
160
            ->rowData('DT_RowAttr', $this->templates['DT_RowAttr'])
161
            ->getData();
162
    }
163
164
    /**
165
     * Remove declared hidden columns.
166
     *
167
     * @param array $data
168
     * @return array
169
     */
170
    protected function removeExcessColumns(array $data)
171
    {
172
        foreach ($this->excessColumns as $value) {
173
            unset($data[$value]);
174
        }
175
176
        return $data;
177
    }
178
179
    /**
180
     * Flatten array with exceptions.
181
     *
182
     * @param array $array
183
     * @return array
184
     */
185
    public function flatten(array $array)
186
    {
187
        $return     = [];
188
        $exceptions = ['DT_RowId', 'DT_RowClass', 'DT_RowData', 'DT_RowAttr'];
189
190
        foreach ($array as $key => $value) {
191
            if (in_array($key, $exceptions)) {
192
                $return[$key] = $value;
193
            } else {
194
                $return[] = $value;
195
            }
196
        }
197
198
        return $return;
199
    }
200
201
    /**
202
     * Escape column values as declared.
203
     *
204
     * @param array $output
205
     * @return array
206
     */
207
    protected function escapeColumns(array $output)
208
    {
209
        return array_map(function ($row) {
210
            if ($this->escapeColumns == '*') {
211
                $row = $this->escapeRow($row);
212
            } elseif (is_array($this->escapeColumns)) {
213
                $columns = array_diff_key($this->escapeColumns, $this->rawColumns);
214
                foreach ($columns as $key) {
215
                    array_set($row, $key, e(array_get($row, $key)));
216
                }
217
            }
218
219
            return $row;
220
        }, $output);
221
    }
222
223
    /**
224
     * Escape all values of row.
225
     *
226
     * @param array $row
227
     * @return array
228
     */
229
    protected function escapeRow(array $row)
230
    {
231
        $arrayDot = array_filter(array_dot($row));
232
        foreach ($arrayDot as $key => $value) {
233
            if (!in_array($key, $this->rawColumns)) {
234
                $arrayDot[$key] = e($value);
235
            }
236
        }
237
238
        foreach ($arrayDot as $key => $value) {
239
            array_set($row, $key, $value);
240
        }
241
242
        return $row;
243
    }
244
}
245