Completed
Push — master ( 0f1128...f5665a )
by Dmitry
25:23 queued 22:06
created

RadioButtonColumn   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 93.33%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 3
dl 0
loc 62
ccs 14
cts 15
cp 0.9333
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 7 2
B renderDataCellContent() 0 17 6
1
<?php
2
/**
3
 * @link http://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license http://www.yiiframework.com/license/
6
 */
7
8
namespace yii\grid;
9
10
use Closure;
11
use yii\base\InvalidConfigException;
12
use yii\helpers\Html;
13
14
/**
15
 * RadioButtonColumn displays a column of radio buttons in a grid view.
16
 *
17
 * To add a RadioButtonColumn to the [[GridView]], add it to the [[GridView::columns|columns]] configuration as follows:
18
 *
19
 * ```php
20
 * 'columns' => [
21
 *     // ...
22
 *     [
23
 *         'class' => 'yii\grid\RadioButtonColumn',
24
 *         'radioOptions' => function ($model) {
25
 *              return [
26
 *                  'value' => $model['value'],
27
 *                  'checked' => $model['value'] == 2
28
 *              ];
29
 *          }
30
 *     ],
31
 * ]
32
 * ```
33
 *
34
 * @author Kirk Hansen <[email protected]>
35
 * @since 2.0.11
36
 */
37
class RadioButtonColumn extends Column
38
{
39
    /**
40
     * @var string the name of the input radio button input fields.
41
     */
42
    public $name = 'radioButtonSelection';
43
    /**
44
     * @var array|\Closure the HTML attributes for the radio buttons. This can either be an array of
45
     * attributes or an anonymous function ([[Closure]]) returning such an array.
46
     *
47
     * The signature of the function should be as follows: `function ($model, $key, $index, $column)`
48
     * where `$model`, `$key`, and `$index` refer to the model, key and index of the row currently being rendered
49
     * and `$column` is a reference to the [[RadioButtonColumn]] object.
50
     *
51
     * A function may be used to assign different attributes to different rows based on the data in that row.
52
     * Specifically if you want to set a different value for the radio button you can use this option
53
     * in the following way (in this example using the `name` attribute of the model):
54
     *
55
     * ```php
56
     * 'radioOptions' => function ($model, $key, $index, $column) {
57
     *     return ['value' => $model->attribute];
58
     * }
59
     * ```
60
     *
61
     * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
62
     */
63
    public $radioOptions = [];
64
65
66
    /**
67
     * {@inheritdoc}
68
     * @throws \yii\base\InvalidConfigException if [[name]] is not set.
69
     */
70 5
    public function init()
71
    {
72 5
        parent::init();
73 5
        if (empty($this->name)) {
74 1
            throw new InvalidConfigException('The "name" property must be set.');
75
        }
76 4
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81 4
    protected function renderDataCellContent($model, $key, $index)
82
    {
83 4
        if ($this->content !== null) {
84 1
            return parent::renderDataCellContent($model, $key, $index);
85
        }
86
87 3
        if ($this->radioOptions instanceof Closure) {
88 2
            $options = call_user_func($this->radioOptions, $model, $key, $index, $this);
89
        } else {
90 1
            $options = $this->radioOptions;
91 1
            if (!isset($options['value'])) {
92
                $options['value'] = is_array($key) ? json_encode($key, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) : $key;
93
            }
94
        }
95 3
        $checked = isset($options['checked']) ? $options['checked'] : false;
96 3
        return Html::radio($this->name, $checked, $options);
97
    }
98
}
99