RadioGroup::createValidators()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace mindplay\kissform\Fields;
4
5
use mindplay\kissform\Field;
6
use mindplay\kissform\InputModel;
7
use mindplay\kissform\InputRenderer;
8
use mindplay\kissform\Validators\CheckSelected;
9
10
/**
11
 * This class provides information about a group of `<input type="radio">` elements.
12
 *
13
 * Note that the markup deviates from Bootstrap standard markup, which isn't useful for styling.
14
 *
15
 * @link https://github.com/twbs/bootstrap/issues/19931
16
 * @link https://github.com/flatlogic/awesome-bootstrap-checkbox
17
 */
18
class RadioGroup extends Field
19
{
20
    /**
21
     * @var string[] map where option values map to option labels
22
     */
23
    protected $options;
24
25
    /**
26
     * @var string[] map of HTML attributes for the <input> tag
27
     */
28
    public $input_attr = [];
29
30
    /**
31
     * @var string[] map of HTML attributes for the <label> tag
32
     */
33
    public $label_attr = [];
34
35
    /**
36
     * @var string|null wrapper tag (e.g. "div", or NULL to disable wrapper-tags)
37
     */
38
    public $wrapper_tag = 'div';
39
40
    /**
41
     * @var string[] map of HTML attributes for the wrapper-tag around each checkbox group
42
     */
43
    public $wrapper_attr = ['class' => 'radio'];
44
45
    /**
46
     * @param string   $name    field name
47
     * @param string[] $options map where option values map to option labels
48
     */
49 2
    public function __construct($name, array $options)
50
    {
51 2
        parent::__construct($name);
52
53 2
        $this->options = $options;
54 2
    }
55
56
    /**
57
     * @inheritdoc
58
     */
59 1
    public function createValidators()
60
    {
61 1
        return [new CheckSelected(array_keys($this->options))];
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67 1
    public function renderInput(InputRenderer $renderer, InputModel $model, array $attr)
68
    {
69 1
        $selected = $model->getInput($this);
70
71 1
        $html = '';
72
73 1
        foreach ($this->options as $value => $label) {
74 1
            $checked = is_numeric($selected)
75 1
                ? $value == $selected // loose comparison works well for NULLs and numbers
76 1
                : $value === $selected; // strict comparison for everything else
77
78 1
            $id = $renderer->getId($this, $value);
79
80 1
            $group = $renderer->tag(
81 1
                'input',
82 1
                $renderer->mergeAttrs(
83
                    [
84 1
                        'type'    => 'radio',
85 1
                        'id'      => $id,
86 1
                        'name'    => $renderer->getName($this),
87 1
                        'value'   => $value,
88 1
                        'checked' => $checked,
89
                    ],
90 1
                    $this->input_attr,
91 1
                    $attr
92
                )
93
            );
94
95 1
            $group .= $renderer->tag('label', $this->label_attr + ['for' => $id], $renderer->softEscape($label));
96
            
97 1
            if ($this->wrapper_tag) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->wrapper_tag of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
98 1
                $group = $renderer->tag($this->wrapper_tag, $this->wrapper_attr, $group);
99
            }
100
101 1
            $html .= $group;
102
        }
103
104 1
        return $html;
105
    }
106
}
107