Completed
Pull Request — master (#588)
by
unknown
14:32
created

Radio::defaultRender()   B

Complexity

Conditions 8
Paths 40

Size

Total Lines 35
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 8.0786

Importance

Changes 0
Metric Value
cc 8
eloc 28
nc 40
nop 0
dl 0
loc 35
ccs 25
cts 28
cp 0.8929
crap 8.0786
rs 8.4444
c 0
b 0
f 0
1
<?php
2
/*
3
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
*/
11
12
namespace Xoops\Form;
13
14
/**
15
 * Radio - radio button element
16
 *
17
 * @category  Xoops\Form\Radio
18
 * @package   Xoops\Form
19
 * @author    Kazumi Ono <[email protected]>
20
 * @author    Taiwen Jiang <[email protected]>
21
 * @copyright 2001-2016 XOOPS Project (http://xoops.org)
22
 * @license   GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
23
 * @link      http://xoops.org
24
 */
25
class Radio extends OptionElement
26
{
27
    /**
28
     * __construct
29
     *
30
     * @param mixed   $caption Caption or array of all attributes
31
     *                          Control attributes:
32
     *                              :inline true to render with inline style
33
     * @param string  $name    name attribute
34
     * @param string  $value   Pre-selected value
35
     * @param boolean $inline  true to display inline
36
     */
37 9
    public function __construct($caption, $name = null, $value = null, $inline = true)
38
    {
39 9
        if (is_array($caption)) {
40 1
            parent::__construct($caption);
41
        } else {
42 9
            parent::__construct([]);
43 9
            $this->setWithDefaults('caption', $caption, '');
44 9
            $this->setWithDefaults('name', $name, 'name_error');
45 9
            $this->set('value', $value);
46 9
            if ($inline) {
47 9
                $this->set(':inline');
48
            }
49
        }
50 9
        $this->set('type', 'radio');
51 9
    }
52
53
    /**
54
     * defaultRender
55
     *
56
     * @return string rendered form element
57
     */
58 4
    public function defaultRender()
59
    {
60 4
        $ele_options = $this->getOptions();
61 4
        $ele_value = $this->getValue();
62 4
        $ele_name = $this->getName();
63 4
        $extra = ($this->getExtra() != '' ? " " . $this->getExtra() : '');
64 4
        $ret = "";
65 4
        $inline = $this->has(':inline');
66 4
        if ($inline) {
67 4
            $ret .= '<div class="input-group">';
68
        }
69 4
        static $id_ele = 0;
70 4
        foreach ($ele_options as $value => $buttonCaption) {
71 3
            $this->remove('checked');
72 3
            if (isset($ele_value) && $value == $ele_value) {
73 1
                $this->set('checked');
74
            }
75 3
            $this->set('value', $value);
76 3
            ++$id_ele;
77 3
            $this->set('id', $ele_name . $id_ele);
78 3
            if ($inline) {
79 3
                $ret .= '<label class="radio-inline">';
80 3
                $ret .= '<input ' . $this->renderAttributeString() . $extra . ">" . $buttonCaption . "\n";
81 3
                $ret .= "</label>\n";
82
            } else {
83
                $ret .= "<div class=\"radio\">\n<label>";
84
                $ret .= '<input ' . $this->renderAttributeString() . $extra . '>' . "\n";
85
                $ret .= $buttonCaption . "\n";
86 3
                $ret .= "</label>\n</div>\n";
87
            }
88
        }
89 4
        if ($inline) {
90 4
            $ret .= '</div>';
91
        }
92 4
        return $ret;
93
    }
94
}
95