Passed
Push — main ( 0e35c2...31d92c )
by Stefan
02:16
created

FormRadioGroup::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 6
rs 10
c 1
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace SKien\Formgenerator;
5
6
/**
7
 * Radiogroup input.
8
 *
9
 * @package Formgenerator
10
 * @author Stefanius <[email protected]>
11
 * @copyright MIT License - see the LICENSE file for details
12
 */
13
class FormRadioGroup extends FormInput
14
{
15
    /**
16
     * Create a radio group.
17
     * @param string $strName   name AND id of the element
18
     * @param int $wFlags    (default: 0)
19
     */
20
    public function __construct(string $strName, int $wFlags = 0) 
21
    {
22
        $this->oFlags = new FormFlags($wFlags);
23
        $this->strName = $strName;
24
        if ($this->oFlags->isSet(FormFlags::READ_ONLY | FormFlags::DISABLED)) {
25
            $this->addAttribute('disabled');
26
        }
27
    }
28
    
29
    /**
30
     * Build the HTML-markup for the checkbox.
31
     * Checkbox is a special case, as it behaves particularly with regard to the transferred 
32
     * value and does not support read-only mode.
33
     * There is special treatment for both cases, which is explained in more detail in the 
34
     * respective code areas bolow.  
35
     * @return string
36
     */
37
    public function getHTML() : string 
38
    {
39
        $this->processFlags();
40
        
41
        $strSelect = $this->oFG->getData()->getValue($this->strName);
42
        $aOptions = $this->oFG->getData()->getSelectOptions($this->strName);
43
        
44
        $strHTML = $this->buildContainerDiv();
45
        
46
        $iBtn = 0;
47
        $this->addStyle('float', 'left');
48
        foreach ($aOptions as $strName => $strValue) {
49
            if ($strName !== '') {
50
                $strHTML .= '<input type="radio"';
51
                $strHTML .= $this->buildStyle();
52
                $strHTML .= $this->buildAttributes();
53
                $strHTML .= $this->buildTabindex();
54
                $strHTML .= ' id="' . $this->strName . ++$iBtn . '"';
55
                $strHTML .= ' name="' . $this->strName . '"';
56
                if ($strSelect === $strValue) {
57
                    $strHTML .= ' checked';
58
                }
59
                $strHTML .= ' value="' . $strValue . '">';
60
                $strHTML .= ' <label for="' . $this->strName . ++$iBtn . '"';
61
                if ($this->oFlags->isSet(FormFlags::HORZ_ARRANGE)) {
62
                    $strHTML .= ' style="float: left;"';
63
                }
64
                $strHTML .= ' class="radio">' . $strName . '</label>' . PHP_EOL;
65
            }
66
        }
67
        
68
        $strHTML .= '</div>' . PHP_EOL;
69
70
        return $strHTML;
71
    }
72
}
73