LabelColumn   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 3
dl 0
loc 45
ccs 0
cts 11
cp 0
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A getDataCellValue() 0 15 2
1
<?php
2
3
/*
4
 * This file is part of the 2amigos/yii2-grid-view-library 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\grid\columns;
11
12
use yii\bootstrap\Html;
13
use yii\helpers\ArrayHelper;
14
15
class LabelColumn extends DataColumn
16
{
17
    /**
18
     * @var array $labels the configuration on how to display the different label values. Each array element key
19
     *            represents a label value which can be specified as a string or an array of the following structure:
20
     *
21
     * ```
22
     * 'active' => [
23
     *      'label' => 'subscribed', // string, the status label. If not provided, the key will be used as the label.
24
     *      'options' => [
25
     *          'style' => 'padding: 2px' // array, optional, the HTML attributes of the button.
26
     *      ]
27
     * ],
28
     * ```
29
     *
30
     * The key names of the array will be used to match against the value. If a match is found, will render a bootstrap
31
     * label with the options provided.
32
     *
33
     * @see http://getbootstrap.com/components/#labels
34
     */
35
    public $labels = [];
36
    /**
37
     * @var string forcely set the format to HTML.
38
     */
39
    public $format = 'html';
40
41
    /**
42
     * @inheritdoc
43
     */
44
    public function getDataCellValue($model, $key, $index)
45
    {
46
        $value = parent::getDataCellValue($model, $key, $index);
47
48
        if (isset($this->labels[$value])) {
49
            $text = ArrayHelper::getValue($this->labels, "$value.label", $value);
50
            $options = ArrayHelper::getValue($this->labels, "$value.options", ['class' => 'label-default']);
51
52
            Html::addCssClass($options, 'label');
53
54
            return Html::tag('span', $text, $options);
55
        }
56
57
        return $value;
58
    }
59
}
60