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

FormCollection::getHTML()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
declare(strict_types=1);
3
4
namespace SKien\Formgenerator;
5
6
/**
7
 * Base-class for all elements that can have child elements.
8
 *
9
 * @package Formgenerator
10
 * @author Stefanius <[email protected]>
11
 * @copyright MIT License - see the LICENSE file for details
12
 */
13
abstract class FormCollection extends FormElement
14
{
15
    /** @var FormElementInterface[] all direct child elements     */
16
    protected array $aChild = [];
17
    /** @var array the width information for the cols inside this element     */
18
    protected ?array $aColWidth = null;
19
    /** @var string dimension of the width values ('%', 'px', 'em')     */
20
    protected string $strWidthDim = '%';
21
    
22
    /**
23
     * @param int $wFlags
24
     */
25
    public function __construct(int $wFlags)
26
    {
27
        parent::__construct($wFlags);
28
    }
29
    
30
    /**
31
     * {@inheritDoc}
32
     * @see \SKien\Formgenerator\FormElement::readAdditionalXML()
33
     */
34
    public function readAdditionalXML(\DOMElement $oXMLElement) : void
35
    {
36
        $strWidthDim = self::getAttribString($oXMLElement, 'widthdim', '%');
37
        if (($aColWidth = self::getAttribIntArray($oXMLElement, 'colwidth')) !== null) {
38
            $this->setColWidth($aColWidth, $strWidthDim);
39
        }
40
    }
41
        
42
    /**
43
     * Add a child to this element.
44
     * @param FormElementInterface $oElement
45
     * @return FormElementInterface
46
     */
47
    public function add(FormElementInterface $oElement) : FormElementInterface
48
    {
49
        $oElement->setParent($this);
50
        $this->aChild[] = $oElement;
51
        $this->oFG->addElement($oElement);
52
        
53
        return $oElement;
54
    }
55
    
56
    /**
57
     * Set width for the cols included in this element.
58
     * @param array $aColWidth
59
     * @param string $strDim
60
     */
61
    public function setColWidth(array $aColWidth, string $strDim = '%') : void
62
    {
63
        $this->aColWidth = $aColWidth;
64
        $this->strWidthDim = $strDim;
65
    }
66
    
67
    /**
68
     * Get colwidth for requested element.
69
     * If no width set, we try to get the width throught the parent.
70
     * @param int $iCol  requested col, if -1 current col is used
71
     * @return string       colwidth including dimension
72
     */
73
    public function getColWidth(int $iCol = -1) : string
74
    {
75
        $strWidth = '';
76
        if ($iCol < 0) {
77
            $iCol = $this->iCol;
78
        }
79
        if ($this->aColWidth != null && $iCol < count($this->aColWidth) && $this->aColWidth[$iCol] >= 0) {
80
            $strWidth = $this->aColWidth[$iCol] . $this->strWidthDim;
81
        } else if ($this->oParent != null) {
82
            $strWidth = $this->oParent->getColWidth($iCol);
83
        }
84
        return $strWidth;
85
    }
86
    
87
    /**
88
     * Add a new div as child.
89
     * @param int $iWidth   width of the div in percent
90
     * @param int $iAlign   align (FormDiv::NONE, FormDiv::LEFT, FormDiv::RIGHT, FormDiv::CLEAR)
91
     * @param string $strID ID of the div
92
     * @return \SKien\Formgenerator\FormDiv created div element
93
     */
94
    public function addDiv(int $iWidth = 0, int $iAlign = FormDiv::CLEAR, string $strID = '') : FormDiv
95
    {
96
        $oDiv = new FormDiv($iWidth, $iAlign);
97
        $oDiv->SetID($strID);
98
        $this->add($oDiv);
99
        
100
        return $oDiv;
101
    }
102
    
103
    /**
104
     * Add a new fieldset to the element.
105
     * @param string $strLegend text or image of the legend
106
     * @param string $strID
107
     * @param int $iType type of the legend (FormFieldSet::TEXT or FormFieldSet::IMAGE)
108
     * @return \SKien\Formgenerator\FormFieldSet
109
     */
110
    public function addFieldSet(string $strLegend, string $strID = '', $iType = FormFieldSet::TEXT) : FormFieldSet
111
    {
112
        $oFS = new FormFieldSet($strLegend, $strID, $iType);
113
        $this->add($oFS);
114
        
115
        return $oFS;
116
    }
117
    
118
    /**
119
     * Add new line to this fieldset
120
     * @param string $strLabel (default: '&nbsp;')
121
     * @return \SKien\Formgenerator\FormLine
122
     */
123
    public function addLine(string $strLabel = '&nbsp;') : FormLine
124
    {
125
        $oFL = new FormLine($strLabel);
126
        $this->add($oFL);
127
        
128
        return $oFL;
129
    }
130
    
131
    /**
132
     * Build the HTML-notation for the element and/or all child elements.
133
     * @return string
134
     */
135
    public function getHTML() : string
136
    {
137
        $strHTML = '';
138
        $iCnt = count($this->aChild);
139
        for ($i = 0; $i < $iCnt; $i++) {
140
            $strHTML .= $this->aChild[$i]->GetHTML();
141
        }
142
        return $strHTML;
143
    }
144
    
145
    /**
146
     * Get styles from all child elements.
147
     * This method gives each element the chance to add special styles to the
148
     * current page.
149
     * @return string
150
     */
151
    public function getStyle() : string
152
    {
153
        $strStyle = '';
154
        $iCnt = count($this->aChild);
155
        for ($i = 0; $i < $iCnt; $i++) {
156
            $strStyle .= $this->aChild[$i]->getStyle();
157
        }
158
        return $strStyle;
159
    }
160
}
161
162