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

FormCollection::getStyle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 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
     * Add a child to this element.
32
     * @param FormElementInterface $oElement
33
     * @return FormElementInterface
34
     */
35
    public function add(FormElementInterface $oElement) : FormElementInterface
36
    {
37
        $oElement->setParent($this);
38
        $this->aChild[] = $oElement;
39
        $this->oFG->addElement($oElement);
40
        
41
        return $oElement;
42
    }
43
    
44
    /**
45
     * Set width for the cols included in this element.
46
     * @param array $aColWidth
47
     * @param string $strDim
48
     */
49
    public function setColWidth(array $aColWidth, string $strDim = '%') : void
50
    {
51
        $this->aColWidth = $aColWidth;
52
        $this->strWidthDim = $strDim;
53
    }
54
    
55
    /**
56
     * Get colwidth for requested element.
57
     * If no width set, we try to get the width throught the parent.
58
     * @param int $iCol  requested col, if -1 current col is used
59
     * @return string       colwidth including dimension
60
     */
61
    public function getColWidth(int $iCol = -1) : string
62
    {
63
        $strWidth = '';
64
        if ($iCol < 0) {
65
            $iCol = $this->iCol;
66
        }
67
        if ($this->aColWidth != null && $iCol < count($this->aColWidth) && $this->aColWidth[$iCol] >= 0) {
68
            $strWidth = $this->aColWidth[$iCol] . $this->strWidthDim;
69
        } else if ($this->oParent != null) {
70
            $strWidth = $this->oParent->getColWidth($iCol);
71
        }
72
        return $strWidth;
73
    }
74
    
75
    /**
76
     * Add a new div as child.
77
     * @param int $iWidth   width of the div in percent
78
     * @param int $iAlign   align (FormDiv::NONE, FormDiv::LEFT, FormDiv::RIGHT, FormDiv::CLEAR)
79
     * @param string $strID ID of the div
80
     * @return \SKien\Formgenerator\FormDiv created div element
81
     */
82
    public function addDiv(int $iWidth = 0, int $iAlign = FormDiv::CLEAR, string $strID = '') : FormDiv
83
    {
84
        $oDiv = new FormDiv($iWidth, $iAlign);
85
        $oDiv->SetID($strID);
86
        $this->add($oDiv);
87
        
88
        return $oDiv;
89
    }
90
    
91
    /**
92
     * Add a new fieldset to the element.
93
     * @param string $strLegend text or image of the legend
94
     * @param string $strID
95
     * @param int $iType type of the legend (FormFieldSet::TEXT or FormFieldSet::IMAGE)
96
     * @return \SKien\Formgenerator\FormFieldSet
97
     */
98
    public function addFieldSet(string $strLegend, string $strID = '', $iType = FormFieldSet::TEXT) : FormFieldSet
99
    {
100
        $oFS = new FormFieldSet($strLegend, $strID, $iType);
101
        $this->add($oFS);
102
        
103
        return $oFS;
104
    }
105
    
106
    /**
107
     * Add new line to this fieldset
108
     * @param string $strLabel (default: '&nbsp;')
109
     * @return \SKien\Formgenerator\FormLine
110
     */
111
    public function addLine(string $strLabel = '&nbsp;') : FormLine
112
    {
113
        $oFL = new FormLine($strLabel);
114
        $this->add($oFL);
115
        
116
        return $oFL;
117
    }
118
    
119
    /**
120
     * Build the HTML-notation for the element and/or all child elements.
121
     * @return string
122
     */
123
    public function getHTML() : string
124
    {
125
        $strHTML = '';
126
        $iCnt = count($this->aChild);
127
        for ($i = 0; $i < $iCnt; $i++) {
128
            $strHTML .= $this->aChild[$i]->GetHTML();
129
        }
130
        return $strHTML;
131
    }
132
    
133
    /**
134
     * Get styles from all child elements.
135
     * This method gives each element the chance to add special styles to the
136
     * current page.
137
     * @return string
138
     */
139
    public function getStyle() : string
140
    {
141
        $strStyle = '';
142
        $iCnt = count($this->aChild);
143
        for ($i = 0; $i < $iCnt; $i++) {
144
            $strStyle .= $this->aChild[$i]->getStyle();
145
        }
146
        return $strStyle;
147
    }
148
}
149
150