ColumnValueMapper::getColumnValue()   A
last analyzed

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 4
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\db\ActiveRecordInterface;
14
use yii\grid\ActionColumn;
15
use yii\grid\CheckboxColumn;
16
use yii\grid\Column;
17
use yii\grid\DataColumn;
18
use yii\helpers\ArrayHelper;
19
20
class ColumnValueMapper
21
{
22
    /**
23
     * @var array column definitions from GridView
24
     */
25
    protected $columns = [];
26
    /**
27
     * @var array the exportable column names
28
     */
29
    protected $exportableColumns = [];
30
    /**
31
     * @var bool whether we render HTML or not
32
     */
33
    protected $isHtml;
34
35
    /**
36
     * ColumnValueMapper constructor.
37
     *
38
     * @param array $columns
39
     * @param array $exportableColumns
40
     * @param bool $isHtml whether we need to render HTML or not
41
     */
42
    public function __construct(array $columns, array $exportableColumns = [], $isHtml = false)
43
    {
44
        $this->columns = $columns;
45
        $this->exportableColumns = $exportableColumns;
46
        $this->isHtml = $isHtml;
47
    }
48
49
    /**
50
     * Fetch data from the data provider and create the rows array
51
     *
52
     * @param mixed $model
53
     * @param $index
54
     *
55
     * @return array
56
     */
57
    public function map($model, $index)
58
    {
59
        $row = [];
60
        foreach ($this->columns as $column) {
61
            if ($this->isColumnExportable($column)) {
62
                /** @var DataColumn $column */
63
                $key = $model instanceof ActiveRecordInterface
64
                    ? $model->getPrimaryKey()
65
                    : $model[$column->attribute];
66
67
                $value = $this->getColumnValue($column, $model, $key, $index);
68
69
                $header = $this->getColumnHeader($column);
70
                $row[$header] = $value;
71
            }
72
        }
73
74
        return $row;
75
    }
76
77
    protected function getColumnValue($column, $model, $key, $index)
78
    {
79
        $value = $column->renderDataCell($model, $key, $index);
80
        if (!$this->isHtml) {
81
            $value = strip_tags($value);
82
        }
83
84
        return $value;
85
    }
86
87
    /**
88
     * Returns column headers
89
     *
90
     * @param $model
91
     *
92
     * @return array
93
     */
94
    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...
95
    {
96
        $headers = [];
97
        /** @var Column $column */
98
        foreach ($this->columns as $column) {
99
            if ($this->isColumnExportable($column)) {
100
                $headers[] = $this->getColumnHeader($column);
101
            }
102
        }
103
104
        return $headers;
105
    }
106
107
    /**
108
     * Checks whether the column is exportable or not
109
     *
110
     * @param Column $column
111
     *
112
     * @return bool
113
     */
114
    protected function isColumnExportable($column)
115
    {
116
        if ($column instanceof ActionColumn || $column instanceof CheckboxColumn) {
117
            return false;
118
        }
119
120
        if (empty($this->exportableColumns)) {
121
            return true;
122
        }
123
124
        return in_array($column->attribute, $this->exportableColumns);
125
    }
126
127
    /**
128
     * Gets columns header
129
     *
130
     * @param $column
131
     * @param $model
132
     *
133
     * @return string
134
     */
135
    protected function getColumnHeader($column)
136
    {
137
        $header = $column->renderHeaderCell();
138
        if (!$this->isHtml) {
139
            $header = strip_tags($header);
140
        }
141
142
        return $header;
143
    }
144
}
145