Completed
Push — master ( a14092...b2153e )
by Antonio
9s
created

ColumnValueMapper::getColumnHeader()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 0
cts 8
cp 0
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 6
1
<?php
2
3
/*
4
 * This file is part of the 2amigos/yii2-exportable-widget project.
5
 * (c) 2amigOS! <http://2amigos.us/>
6
 * For the full copyright and license information, please view
7
 * the LICENSE file that was distributed with this source code.
8
 */
9
10
namespace dosamigos\exportable\mappers;
11
12
use yii\base\Model;
13
use yii\base\Object;
14
use yii\db\ActiveRecordInterface;
15
use yii\grid\ActionColumn;
16
use yii\grid\CheckboxColumn;
17
use yii\grid\Column;
18
use yii\grid\DataColumn;
19
use yii\helpers\ArrayHelper;
20
21
class ColumnValueMapper
22
{
23
    /**
24
     * @var array column definitions from GridView
25
     */
26
    protected $columns = [];
27
    /**
28
     * @var array the exportable column names
29
     */
30
    protected $exportableColumns = [];
31
    /**
32
     * @var bool whether we render HTML or not
33
     */
34
    protected $isHtml;
35
36
    /**
37
     * ColumnValueMapper constructor.
38
     *
39
     * @param array $columns
40
     * @param array $exportableColumns
41
     * @param bool $isHtml whether we need to render HTML or not
42
     */
43
    public function __construct(array $columns, array $exportableColumns = [], $isHtml = false)
44
    {
45
        $this->columns = $columns;
46
        $this->exportableColumns = $exportableColumns;
47
        $this->isHtml = $isHtml;
48
    }
49
50
    /**
51
     * Fetch data from the data provider and create the rows array
52
     *
53
     * @param mixed $model
54
     * @param $index
55
     *
56
     * @return array
57
     */
58
    public function map($model, $index)
59
    {
60
        $row = [];
61
        foreach ($this->columns as $column) {
62
            if ($this->isColumnExportable($column)) {
63
                /** @var DataColumn $column */
64
                $key = $model instanceof ActiveRecordInterface
65
                    ? $model->getPrimaryKey()
66
                    : $model[$column->attribute];
67
68
                $value = $this->getColumnValue($column, $model, $key, $index);
69
70
                $header = $this->getColumnHeader($column);
71
                $row[$header] = $value;
72
            }
73
        }
74
75
        return $row;
76
    }
77
78
    protected function getColumnValue($column, $model, $key, $index)
79
    {
80
        $value = $column->renderDataCell($model, $key, $index);
81
        if (!$this->isHtml) {
82
            $value = strip_tags($value);
83
        }
84
85
        return $value;
86
    }
87
88
    /**
89
     * Returns column headers
90
     *
91
     * @param $model
92
     *
93
     * @return array
94
     */
95
    public function getHeaders($model)
0 ignored issues
show
Unused Code introduced by
The parameter $model is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
96
    {
97
        $headers = [];
98
        /** @var Column $column */
99
        foreach ($this->columns as $column) {
100
            if ($this->isColumnExportable($column)) {
101
                $headers[] = $this->getColumnHeader($column);
102
            }
103
        }
104
105
        return $headers;
106
    }
107
108
    /**
109
     * Checks whether the column is exportable or not
110
     *
111
     * @param Column $column
112
     *
113
     * @return bool
114
     */
115
    protected function isColumnExportable($column)
116
    {
117
        if ($column instanceof ActionColumn || $column instanceof CheckboxColumn) {
118
            return false;
119
        }
120
121
        if (empty($this->exportableColumns)) {
122
            return true;
123
        }
124
125
        return in_array($column->attribute, $this->exportableColumns);
126
    }
127
128
    /**
129
     * Gets columns header
130
     *
131
     * @param $column
132
     * @param $model
133
     *
134
     * @return string
135
     */
136
    protected function getColumnHeader($column)
137
    {
138
        $header = $column->renderHeaderCell();
139
        if (!$this->isHtml) {
140
            $header = strip_tags($header);
141
        }
142
143
        return $header;
144
    }
145
}
146