Completed
Push — master ( 6aed65...9d47e6 )
by Antonio
02:08
created

BooleanColumn::renderDataCell()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 0
cts 10
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 3
crap 6
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
14
class BooleanColumn extends DataColumn
15
{
16
    /**
17
     * @var string $onTrue the contents to display when value is true.
18
     */
19
    public $onTrue = '<span class="glyphicon glyphicon-ok text-success"></span>';
20
    /**
21
     * @var string $onFalse the contents to display when value is false.
22
     */
23
    public $onFalse = '<span class="glyphicon glyphicon-remove text-danger"></span>';
24
    /**
25
     * @inheritdoc
26
     */
27
    public $format = 'html';
28
    /**
29
     * @var bool whether to display empty values with the $onFalse contents.
30
     */
31
    public $treatEmptyAsFalse = false;
32
33
    /**
34
     * @inheritdoc
35
     */
36
    public function renderDataCell($model, $key, $index)
37
    {
38
        if ($this->contentOptions instanceof \Closure) {
39
            $options = call_user_func($this->contentOptions, $model, $key, $index, $this);
40
        } else {
41
            $options = $this->contentOptions;
42
        }
43
44
        Html::addCssClass($options, 'text-center');
45
        return Html::tag('td', $this->renderDataCellContent($model, $key, $index), $options);
46
    }
47
48
    /**
49
     * @inheritdoc
50
     */
51
    public function getDataCellValue($model, $key, $index)
52
    {
53
        $value = parent::getDataCellValue($model, $key, $index);
54
        if (!empty($value)) {
55
            return $value ? $this->onTrue : $this->onFalse;
56
        }
57
58
        return $this->treatEmptyAsFalse ? $this->onFalse : $value;
59
    }
60
}
61