Completed
Push — master ( 9ccddf...64f43c )
by Arjay
02:46
created

DataTransformer::buildColumnByCollection()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
rs 9.2
cc 4
eloc 7
nc 4
nop 3
1
<?php
2
3
namespace Yajra\Datatables\Transformers;
4
5
use Illuminate\Support\Collection;
6
7
/**
8
 * Class DataTransformer
9
 *
10
 * @package Yajra\Datatables\Transformers
11
 */
12
class DataTransformer
13
{
14
    /**
15
     * Transform row data by columns definition.
16
     *
17
     * @param array $row
18
     * @param mixed $columns
19
     * @param string $type
20
     * @return array
21
     */
22
    public function transform(array $row, $columns, $type = 'printable')
23
    {
24
        if ($columns instanceof Collection) {
25
            return $this->buildColumnByCollection($row, $columns, $type);
26
        }
27
28
        return array_only($row, $columns);
29
    }
30
31
    /**
32
     * Transform row column by collection.
33
     *
34
     * @param array $row
35
     * @param \Illuminate\Support\Collection $columns
36
     * @param string $type
37
     * @return array
38
     */
39
    protected function buildColumnByCollection(array  $row, Collection $columns, $type = 'printable')
40
    {
41
        $results = [];
42
        foreach ($columns->all() as $column) {
43
            if ($column[$type]) {
44
                $data = array_get($row, $column['data']);
45
46
                $results[$column['title']] = $type == 'exportable' ? strip_tags($data) : $data;
47
            }
48
        }
49
50
        return $results;
51
    }
52
}
53