FormFieldSet::getHTML()   A
last analyzed

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     ID of the fieldset
33
     * @param int $iType        (FormFieldSet::TXET or FormFieldSet::IMAGE)
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
     * {@inheritDoc}
46
     * @see \SKien\Formgenerator\FormElement::fromXML()
47
     * @internal
48
     */
49
    static public function fromXML(\DOMElement $oXMLElement, FormCollection $oFormParent) : ?FormElement
50
    {
51
        $strLegend = self::getAttribString($oXMLElement, 'legend');
52
        $iType = self::TEXT;
53
        $strType = self::getAttribString($oXMLElement, 'type', 'TEXT');
54
        $strConstName = 'self::' . strtoupper($strType);
55
        if (defined($strConstName)) {
56
            $iType = constant($strConstName);
57
        } else {
58
            trigger_error('Unknown Constant [' . $strConstName . '] for the FieldSet-Type property!', E_USER_ERROR);
59
        }
60
        $oFormElement = new self($strLegend, '', $iType);
61
        $oFormParent->add($oFormElement);
62
        $oFormElement->readAdditionalXML($oXMLElement);
63
64
        return $oFormElement;
65
    }
66
67
    /**
68
     * {@inheritDoc}
69
     * @see \SKien\Formgenerator\FormElement::readAdditionalXML()
70
     * @internal
71
     */
72
    public function readAdditionalXML(\DOMElement $oXMLElement) : void
73
    {
74
        parent::readAdditionalXML($oXMLElement);
75
        if (self::hasAttrib($oXMLElement, 'imageheight')) {
76
            $this->setImageHeight(self::getAttribInt($oXMLElement, 'imageheight'));
77
        }
78
    }
79
80
    /**
81
     * Set height of legend image in pixels.
82
     * The default-height is 12px.
83
     * @param int $iHeight
84
     */
85
    public function setImageHeight(int $iHeight) : void
86
    {
87
        $this->iImageHeight = $iHeight;
88
    }
89
90
    /**
91
     * Build the HTML-notation for the fieldset.
92
     * @return string
93
     * @internall
94
     */
95
    public function getHTML() : string
96
    {
97
        $strHTML  = '';
98
        $strHTML .= '<fieldset';
99
        $strHTML .= $this->buildID();
100
        $strHTML .= $this->buildStyle();
101
        $strHTML .= $this->buildAttributes();
102
        $strHTML .= ">" . PHP_EOL;
103
        if (!empty($this->strLegend)) {
104
            $strHTML .= '   <legend>';
105
            if ($this->iType == self::TEXT) {
106
                $strHTML .= $this->strLegend;
107
            } else {
108
                // special case for style because legend is not treated as standalone element...
109
                $this->iImageHeight > 0 ? $strStyle = ' style="height: ' . $this->iImageHeight . 'px;"' : $strStyle = '';
110
                $strHTML .= '<img src="' . $this->strLegend . '" alt="Legend"' . $strStyle . '>';
111
            }
112
            $strHTML .= '</legend>' . PHP_EOL;
113
        }
114
        $iCnt = count($this->aChild);
115
        for ($i = 0; $i < $iCnt; $i++) {
116
            $strHTML .= $this->aChild[$i]->GetHTML();
117
        }
118
        $strHTML .= '</fieldset>' . PHP_EOL;
119
        return $strHTML;
120
    }
121
}
122