Completed
Push — master ( 64f43c...6f76b9 )
by Arjay
02:59
created

DataTransformer::decodeContent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
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
                $title = $column['title'];
45
                $data  = array_get($row, $column['data']);
46
                if ($type == 'exportable') {
47
                    $data  = $this->decodeContent($data);
48
                    $title = $this->decodeContent($title);
49
                }
50
51
                $results[$title] = $data;
52
            }
53
        }
54
55
        return $results;
56
    }
57
58
    /**
59
     * Decode content to a readable text value.
60
     *
61
     * @param string $data
62
     * @return string
63
     */
64
    protected function decodeContent($data)
65
    {
66
        $decoded = html_entity_decode(strip_tags($data), ENT_QUOTES, 'UTF-8');
67
68
        return str_replace("\xc2\xa0", ' ', $decoded);
69
    }
70
}
71