DataArrayTransformer   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 0
loc 65
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A transform() 0 8 2
A buildColumnByCollection() 0 20 4
A decodeContent() 0 10 2
1
<?php
2
3
namespace Yajra\DataTables\Transformers;
4
5
use Illuminate\Support\Arr;
6
use Illuminate\Support\Collection;
7
8
class DataArrayTransformer
9
{
10
    /**
11
     * Transform row data by columns definition.
12
     *
13
     * @param array  $row
14
     * @param mixed  $columns
15
     * @param string $type
16
     * @return array
17
     */
18
    public function transform(array $row, $columns, $type = 'printable')
19
    {
20
        if ($columns instanceof Collection) {
21
            return $this->buildColumnByCollection($row, $columns, $type);
22
        }
23
24
        return Arr::only($row, $columns);
25
    }
26
27
    /**
28
     * Transform row column by collection.
29
     *
30
     * @param array                          $row
31
     * @param \Illuminate\Support\Collection $columns
32
     * @param string                         $type
33
     * @return array
34
     */
35
    protected function buildColumnByCollection(array $row, Collection $columns, $type = 'printable')
36
    {
37
        $results = [];
38
        foreach ($columns->all() as $column) {
39
            if ($column[$type]) {
40
                $title = $column['title'];
41
                $data  = Arr::get($row, $column['data']);
42
                if ($type == 'exportable') {
43
                    $title    = $this->decodeContent($title);
44
                    $dataType = gettype($data);
45
                    $data     = $this->decodeContent($data);
46
                    settype($data, $dataType);
47
                }
48
49
                $results[$title] = $data;
50
            }
51
        }
52
53
        return $results;
54
    }
55
56
    /**
57
     * Decode content to a readable text value.
58
     *
59
     * @param string $data
60
     * @return string
61
     */
62
    protected function decodeContent($data)
63
    {
64
        try {
65
            $decoded = html_entity_decode(strip_tags($data), ENT_QUOTES, 'UTF-8');
66
67
            return str_replace("\xc2\xa0", ' ', $decoded);
68
        } catch (\Exception $e) {
69
            return $data;
70
        }
71
    }
72
}
73