Passed
Push — main ( e2b5f1...353913 )
by Stefan
02:09
created

FormFieldSet::getHTML()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 25
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 19
c 1
b 0
f 0
nc 8
nop 0
dl 0
loc 25
rs 9.3222
1
<?php
2
declare(strict_types=1);
3
4
namespace SKien\Formgenerator;
5
6
/**
7
 * Class to create fieldset as parent of lines.
8
 *
9
 * @package Formgenerator
10
 * @author Stefanius <[email protected]>
11
 * @copyright MIT License - see the LICENSE file for details
12
 */
13
class FormFieldSet extends FormCollection
14
{
15
    /** legend of the fieldset contains text    */
16
    const   TEXT    = 0;
17
    /** legend of the fieldset contains an image    */
18
    const   IMAGE   = 1;
19
    
20
    /** @var string text or image for the legend     */
21
    protected string $strLegend;
22
    /** @var int type of the legend (FormFieldSet::TXET or FormFieldSet::IMAGE)    */
23
    protected int $iType;
24
    /** @var int height of the legend image     */
25
    protected int $iImageHeight;
26
27
    /**
28
     * Creates a legend element.
29
     * If legend should display an image with height other than 12px, this height
30
     * must be set with setImageHeight().
31
     * @param string $strLegend text or image for the legend
32
     * @param string $strID
33
     * @param int $iType
34
     */
35
    public function __construct(string $strLegend, string $strID = '', int $iType = self::TEXT) 
36
    {
37
        parent::__construct(0);
38
        $this->strLegend = $strLegend;
39
        $this->strID = $strID;
40
        $this->iType = $iType;
41
        $this->iImageHeight = -1;
42
    }
43
44
    /**
45
     * Set height of legend image in pixels.
46
     * The default-height is 12px.
47
     * @param int $iHeight
48
     */
49
    public function setImageHeight(int $iHeight) : void 
50
    {
51
        $this->iImageHeight = $iHeight;
52
    }
53
54
    /**
55
     * Build the HTML-notation for the fieldset.
56
     * @return string
57
     */
58
    public function getHTML() : string
59
    {
60
        $strHTML  = '';
61
        $strHTML .= '<fieldset';
62
        $strHTML .= $this->buildID();
63
        $strHTML .= $this->buildStyle();
64
        $strHTML .= $this->buildAttributes();
65
        $strHTML .= ">" . PHP_EOL;
66
        if (!empty($this->strLegend)) {
67
            $strHTML .= '   <legend>';
68
            if ($this->iType == self::TEXT) {
69
                $strHTML .= $this->strLegend;
70
            } else {
71
                // special case for style because legend is not treated as standalone element...
72
                $this->iImageHeight > 0 ? $strStyle = ' style="height: ' . $this->iImageHeight . 'px;"' : $strStyle = ''; 
73
                $strHTML .= '<img src="' . $this->strLegend . '" alt="Legend"' . $strStyle . '>';
74
            }
75
            $strHTML .= '</legend>' . PHP_EOL;
76
        }
77
        $iCnt = count($this->aChild);
78
        for ($i = 0; $i < $iCnt; $i++) {
79
            $strHTML .= $this->aChild[$i]->GetHTML();
80
        }
81
        $strHTML .= '</fieldset>' . PHP_EOL;
82
        return $strHTML;
83
    }
84
}
85