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

DataTransformer   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
c 2
b 0
f 0
lcom 0
cbo 1
dl 0
loc 41
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A transform() 0 8 2
A buildColumnByCollection() 0 13 4
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