1: <?php
2:
3: namespace Yajra\Datatables\Transformers;
4:
5: use Illuminate\Support\Collection;
6:
7: 8: 9: 10: 11: 12:
13: class DataTransformer
14: {
15: 16: 17: 18: 19: 20: 21: 22:
23: public function transform(array $row, $columns, $type = 'printable')
24: {
25: if ($columns instanceof Collection) {
26: return $this->buildColumnByCollection($row, $columns, $type);
27: }
28:
29: return array_only($row, $columns);
30: }
31:
32: 33: 34: 35: 36: 37: 38: 39:
40: protected function buildColumnByCollection(array $row, Collection $columns, $type = 'printable')
41: {
42: $results = [];
43: foreach ($columns->all() as $column) {
44: if ($column[$type]) {
45: $title = $column['title'];
46: $data = array_get($row, $column['data']);
47: if ($type == 'exportable') {
48: $data = $this->decodeContent($data);
49: $title = $this->decodeContent($title);
50: }
51:
52: $results[$title] = $data;
53: }
54: }
55:
56: return $results;
57: }
58:
59: 60: 61: 62: 63: 64:
65: protected function decodeContent($data)
66: {
67: try {
68: $decoded = html_entity_decode(strip_tags($data), ENT_QUOTES, 'UTF-8');
69:
70: return str_replace("\xc2\xa0", ' ', $decoded);
71: } catch (\Exception $e) {
72: return $data;
73: }
74: }
75: }
76: