Completed
Push — master ( a217e2...5061b6 )
by Richard
06:39 queued 06:36
created

Radio   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Test Coverage

Coverage 89.74%

Importance

Changes 0
Metric Value
eloc 39
dl 0
loc 68
ccs 35
cts 39
cp 0.8974
rs 10
c 0
b 0
f 0
wmc 11

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 3
B defaultRender() 0 35 8
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
                $ret .= "</label>\n</div>\n";
87
            }
88
        }
89 4
        if ($inline) {
90 4
            $ret .= '</div>';
91
        }
92 4
        return $ret;
93
    }
94
}
95