Passed
Push — main ( fac3c3...502827 )
by Stefan
02:24
created

FormCollection::addFieldSet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
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
 * #### History
10
 * - *2021-01-18*   initial version
11
 *
12
 * @package Formgenerator
13
 * @version 1.1.0
14
 * @author Stefanius <[email protected]>
15
 * @copyright MIT License - see the LICENSE file for details
16
 */
17
abstract class FormCollection extends FormElement
18
{
19
    /** @var FormElement[] all direct child elements     */
20
    protected array $aChild = [];
21
    /** @var array the width information for the cols inside this element     */
22
    protected ?array $aColWidth = null;
23
    /** @var string dimension of the width values ('%', 'px', 'em')     */
24
    protected string $strWidthDim = '%';
25
    
26
    /**
27
     * @param int $wFlags
28
     */
29
    public function __construct(int $wFlags)
30
    {
31
        parent::__construct($wFlags);
32
    }
33
    
34
    /**
35
     * Add a child to this element.
36
     * @param FormElement $oElement
37
     * @return FormElement
38
     */
39
    public function add(FormElement $oElement) : FormElement
40
    {
41
        $oElement->setParent($this);
42
        $this->aChild[] = $oElement;
43
        $this->oFG->addElement($oElement);
44
        
45
        return $oElement;
46
    }
47
    
48
    /**
49
     * Set width for the cols included in this element.
50
     * @param array $aColWidth
51
     * @param string $strDim
52
     */
53
    public function setColWidth(array $aColWidth, string $strDim = '%') : void
54
    {
55
        $this->aColWidth = $aColWidth;
56
        $this->strWidthDim = $strDim;
57
    }
58
    
59
    /**
60
     * Get colwidth for requested element.
61
     * If no width set, we try to get the width throught the parent.
62
     * @param int $iCol  requested col, if -1 current col is used
63
     * @return string       colwidth including dimension
64
     */
65
    public function getColWidth(int $iCol = -1) : string
66
    {
67
        $strWidth = '';
68
        if ($iCol < 0) {
69
            $iCol = $this->iCol;
70
        }
71
        if ($this->aColWidth != null && $iCol < count($this->aColWidth) && $this->aColWidth[$iCol] >= 0) {
72
            $strWidth = $this->aColWidth[$iCol] . $this->strWidthDim;
73
        } else if ($this->oParent != null) {
74
            $strWidth = $this->oParent->getColWidth($iCol);
75
        }
76
        return $strWidth;
77
    }
78
    
79
    /**
80
     * Add a new div as child.
81
     * @param int $iWidth   width of the div in percent
82
     * @param int $iAlign   align (FormDiv::NONE, FormDiv::LEFT, FormDiv::RIGHT, FormDiv::CLEAR)
83
     * @param string $strID ID of the div
84
     * @return \SKien\Formgenerator\FormDiv created div element
85
     */
86
    public function addDiv(int $iWidth = 0, int $iAlign = FormDiv::CLEAR, string $strID = '') : FormDiv
87
    {
88
        $oDiv = new FormDiv($iWidth, $iAlign);
89
        $oDiv->SetID($strID);
90
        $this->add($oDiv);
91
        
92
        return $oDiv;
93
    }
94
    
95
    /**
96
     * Add a new fieldset to the element.
97
     * @param string $strLegend text or image of the legend
98
     * @param string $strID
99
     * @param int $iType type of the legend (FormFieldSet::TEXT or FormFieldSet::IMAGE)
100
     * @return \SKien\Formgenerator\FormFieldSet
101
     */
102
    public function addFieldSet(string $strLegend, string $strID = '', $iType = FormFieldSet::TEXT) : FormFieldSet
103
    {
104
        $oFS = new FormFieldSet($strLegend, $strID, $iType);
105
        $this->add($oFS);
106
        
107
        return $oFS;
108
    }
109
    
110
    /**
111
     * Build the HTML-notation for the element and/or all child elements.
112
     * @return string
113
     */
114
    public function getHTML() : string
115
    {
116
        $strHTML = '';
117
        $iCnt = count($this->aChild);
118
        for ($i = 0; $i < $iCnt; $i++) {
119
            $strHTML .= $this->aChild[$i]->GetHTML();
120
        }
121
        return $strHTML;
122
    }
123
}
124
125