Completed
Push — master ( 4cc8d8...e56a52 )
by Arjay
04:43
created

DataProcessor::escapeColumns()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 16
rs 9.2
cc 4
eloc 10
nc 1
nop 1
1
<?php
2
3
namespace Yajra\Datatables\Processors;
4
5
use Yajra\Datatables\Helper;
6
7
/**
8
 * Class DataProcessor
9
 *
10
 * @package Yajra\Datatables
11
 */
12
class DataProcessor
13
{
14
    /**
15
     * Columns to escape value.
16
     *
17
     * @var array
18
     */
19
    private $escapeColumns = [];
20
21
    /**
22
     * Processed data output
23
     *
24
     * @var array
25
     */
26
    private $output = [];
27
28
    /**
29
     * @var array
30
     */
31
    private $appendColumns = [];
32
33
    /**
34
     * @var array
35
     */
36
    private $editColumns = [];
37
38
    /**
39
     * @var array
40
     */
41
    private $excessColumns = [];
42
43
    /**
44
     * @var mixed
45
     */
46
    private $results;
47
48
    /**
49
     * @var array
50
     */
51
    private $templates;
52
53
    /**
54
     * @param mixed $results
55
     * @param array $columnDef
56
     * @param array $templates
57
     */
58
    public function __construct($results, array $columnDef, array $templates)
59
    {
60
        $this->results       = $results;
61
        $this->appendColumns = $columnDef['append'];
62
        $this->editColumns   = $columnDef['edit'];
63
        $this->excessColumns = $columnDef['excess'];
64
        $this->escapeColumns = $columnDef['escape'];
65
        $this->templates     = $templates;
66
    }
67
68
    /**
69
     * Process data to output on browser
70
     *
71
     * @param bool $object
72
     * @return array
73
     */
74
    public function process($object = false)
75
    {
76
        $this->output = [];
77
        foreach ($this->results as $row) {
78
            $data  = Helper::convertToArray($row);
79
            $value = $this->addColumns($data, $row);
80
            $value = $this->editColumns($value, $row);
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type null; however, Yajra\Datatables\Process...rocessor::editColumns() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
81
            $value = $this->setupRowVariables($value, $row);
82
            $value = $this->removeExcessColumns($value);
83
84
            $this->output[] = $object ? $value : $this->flatten($value);
85
        }
86
87
        return $this->escapeColumns($this->output);
88
    }
89
90
    /**
91
     * Process add columns.
92
     *
93
     * @param array $data
94
     * @param mixed $row
95
     * @return array
96
     */
97
    protected function addColumns(array $data, $row)
98
    {
99
        foreach ($this->appendColumns as $key => $value) {
100
            $value['content'] = Helper::compileContent($value['content'], $data, $row);
0 ignored issues
show
Bug introduced by
It seems like $data defined by \Yajra\Datatables\Helper...eInArray($value, $data) on line 101 can also be of type null; however, Yajra\Datatables\Helper::compileContent() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
101
            $data             = Helper::includeInArray($value, $data);
102
        }
103
104
        return $data;
105
    }
106
107
    /**
108
     * Process edit columns.
109
     *
110
     * @param array $data
111
     * @param mixed $row
112
     * @return array
113
     */
114
    protected function editColumns(array $data, $row)
115
    {
116
        foreach ($this->editColumns as $key => $value) {
117
            $value['content']     = Helper::compileContent($value['content'], $data, $row);
118
            $data[$value['name']] = $value['content'];
119
        }
120
121
        return $data;
122
    }
123
124
    /**
125
     * Setup additional DT row variables.
126
     *
127
     * @param mixed $data
128
     * @param mixed $row
129
     * @return array
130
     */
131
    protected function setupRowVariables($data, $row)
132
    {
133
        $processor = new RowProcessor($data, $row);
134
135
        return $processor
136
            ->rowValue('DT_RowId', $this->templates['DT_RowId'])
137
            ->rowValue('DT_RowClass', $this->templates['DT_RowClass'])
138
            ->rowData('DT_RowData', $this->templates['DT_RowData'])
139
            ->rowData('DT_RowAttr', $this->templates['DT_RowAttr'])
140
            ->getData();
141
    }
142
143
    /**
144
     * Remove declared hidden columns.
145
     *
146
     * @param array $data
147
     * @return array
148
     */
149
    protected function removeExcessColumns(array $data)
150
    {
151
        foreach ($this->excessColumns as $value) {
152
            unset($data[$value]);
153
        }
154
155
        return $data;
156
    }
157
158
    /**
159
     * Flatten array with exceptions.
160
     *
161
     * @param array $array
162
     * @return array
163
     */
164
    public function flatten(array $array)
165
    {
166
        $return     = [];
167
        $exceptions = ['DT_RowId', 'DT_RowClass', 'DT_RowData', 'DT_RowAttr'];
168
169
        foreach ($array as $key => $value) {
170
            if (in_array($key, $exceptions)) {
171
                $return[$key] = $value;
172
            } else {
173
                $return[] = $value;
174
            }
175
        }
176
177
        return $return;
178
    }
179
180
    /**
181
     * Escape column values as declared.
182
     *
183
     * @param array $output
184
     * @return array
185
     */
186
    protected function escapeColumns(array $output)
187
    {
188
        return array_map(function ($row) {
189
            if ($this->escapeColumns == '*') {
190
                $row = $this->escapeRow($row, $this->escapeColumns);
191
            } else {
192
                foreach ($this->escapeColumns as $key) {
193
                    if (array_get($row, $key)) {
194
                        array_set($row, $key, e(array_get($row, $key)));
195
                    }
196
                }
197
            }
198
199
            return $row;
200
        }, $output);
201
    }
202
203
    /**
204
     * Escape all values of row.
205
     *
206
     * @param array $row
207
     * @param string|array $escapeColumns
208
     * @return array
209
     */
210
    protected function escapeRow(array $row, $escapeColumns)
211
    {
212
        foreach ($row as $key => $value) {
213
            if (is_array($value)) {
214
                $row[$key] = $this->escapeRow($value, $escapeColumns);
215
            } else {
216
                $row[$key] = e($value);
217
            }
218
        }
219
220
        return $row;
221
    }
222
}
223