Passed
Push — main ( 2edd53...9e1454 )
by Stefan
02:35
created

FormFieldSet::fromXML()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 13
nc 2
nop 2
dl 0
loc 17
rs 9.8333
c 0
b 0
f 0
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
     * {@inheritDoc}
46
     * @see \SKien\Formgenerator\FormElement::fromXML()
47
     */
48
    static public function fromXML(\DOMElement $oXMLElement, FormCollection $oFormParent) : ?FormElement
49
    {
50
        $strLegend = self::getAttribString($oXMLElement, 'legend', '');
51
        $strId = self::getAttribString($oXMLElement, 'id', '');
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_WARNING );
59
        }
60
        $oFormElement = new self($strLegend, $strId, $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
     */
71
    public function readAdditionalXML(\DOMElement $oXMLElement) : void
72
    {
73
        parent::readAdditionalXML($oXMLElement);
74
        if (($iImageHeight = self::getAttribInt($oXMLElement, 'imageheight')) !== null) {
75
            $this->setImageHeight($iImageHeight);
76
        }
77
    }
78
    
79
    /**
80
     * Set height of legend image in pixels.
81
     * The default-height is 12px.
82
     * @param int $iHeight
83
     */
84
    public function setImageHeight(int $iHeight) : void 
85
    {
86
        $this->iImageHeight = $iHeight;
87
    }
88
89
    /**
90
     * Build the HTML-notation for the fieldset.
91
     * @return string
92
     */
93
    public function getHTML() : string
94
    {
95
        $strHTML  = '';
96
        $strHTML .= '<fieldset';
97
        $strHTML .= $this->buildID();
98
        $strHTML .= $this->buildStyle();
99
        $strHTML .= $this->buildAttributes();
100
        $strHTML .= ">" . PHP_EOL;
101
        if (!empty($this->strLegend)) {
102
            $strHTML .= '   <legend>';
103
            if ($this->iType == self::TEXT) {
104
                $strHTML .= $this->strLegend;
105
            } else {
106
                // special case for style because legend is not treated as standalone element...
107
                $this->iImageHeight > 0 ? $strStyle = ' style="height: ' . $this->iImageHeight . 'px;"' : $strStyle = ''; 
108
                $strHTML .= '<img src="' . $this->strLegend . '" alt="Legend"' . $strStyle . '>';
109
            }
110
            $strHTML .= '</legend>' . PHP_EOL;
111
        }
112
        $iCnt = count($this->aChild);
113
        for ($i = 0; $i < $iCnt; $i++) {
114
            $strHTML .= $this->aChild[$i]->GetHTML();
115
        }
116
        $strHTML .= '</fieldset>' . PHP_EOL;
117
        return $strHTML;
118
    }
119
}
120