Completed
Push — master ( 5ede24...37c137 )
by Arjay
08:09
created

DataArrayTransformer::decodeContent()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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