Passed
Push — main ( 4e0970...2bf451 )
by Stefan
09:05
created

FormLine::setLabelFor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace SKien\Formgenerator;
5
6
/**
7
 * Class to create a line starting with label.
8
 * - create as child of a FormField using FormFieldSet::addLine().
9
 * - create standalone as direct child of the form
10
 *
11
 * @package Formgenerator
12
 * @author Stefanius <[email protected]>
13
 * @copyright MIT License - see the LICENSE file for details
14
 */
15
class FormLine extends FormCollection
16
{
17
    /** line contains only HR - no further childs! */
18
    const HR = '<hr>';
19
20
    /** @var string text for the line label     */
21
    protected string $strLabel;
22
    /** @var int col count     */
23
    protected int $iCols = 0;
24
    /** @var string the if set, label with for attribute is created     */
25
    protected string $strLabelFor = '';
26
27
    /**
28
     * Create new line.
29
     * The label is allways the first child / col of the line.
30
     * If you want a line that contains any other element in the first col, you must
31
     * leave the label empty AND set the width of the first col of the line to 0
32
     * (`setColWidth([0, xx, xx, ...])`).
33
     * With `$strLabel = self::HR` a horizontal line is created.
34
     * @param string $strLabel      text for label
35
     * @param string $strLabelFor   crate label for this element
36
     */
37
    public function __construct(string $strLabel, string $strLabelFor = '')
38
    {
39
        parent::__construct(0);
40
        $this->strLabel = $strLabel;
41
        $this->strLabelFor = $strLabelFor;
42
        $this->iCol = 0;
43
        $this->strID = '';
44
    }
45
46
    /**
47
     * {@inheritDoc}
48
     * @see \SKien\Formgenerator\FormElement::fromXML()
49
     * @internal
50
     */
51
    static public function fromXML(\DOMElement $oXMLElement, FormCollection $oFormParent) : ?FormElement
52
    {
53
        if (self::getAttribString($oXMLElement, 'horzline') !== null) {
54
            $strLabel = self::HR;
55
        } else {
56
            $strLabel = self::getAttribString($oXMLElement, 'label', '&nbsp;');
57
        }
58
        $strLabelFor = self::getAttribString($oXMLElement, 'for', '');
59
        $oFormElement = new self($strLabel, $strLabelFor);
60
        $oFormParent->add($oFormElement);
61
        $oFormElement->readAdditionalXML($oXMLElement);
62
63
        return $oFormElement;
64
    }
65
66
    /**
67
     * Set the control for which a label should be created.
68
     * @param string $strLabelFor
69
     */
70
    public function setLabelFor(string $strLabelFor) : void
71
    {
72
        $this->strLabelFor = $strLabelFor;
73
    }
74
75
    /**
76
     * Add a child to the line.
77
     * Some properties of the element to add (parent, tabindex, ...) are changed/set
78
     * with the call of this method.
79
     * next col index is passed to the element and the col count is inkremented with
80
     * each element added to this line.
81
     * @param FormElementInterface $oElement element to add
82
     * @return FormElementInterface added element
83
     */
84
    public function add(FormElementInterface $oElement) : FormElementInterface
85
    {
86
        parent::add($oElement);
87
        $oElement->setCol(++$this->iCols);
88
89
        return $oElement;
90
    }
91
92
    /**
93
     * Build the HTML code for the element.
94
     * The line is 'abstract' and representet by a div. <br/>
95
     * All direct child elements are generated inside this div.
96
     * @return string
97
     * @internal
98
     */
99
    public function getHTML() : string
100
    {
101
        $strWidth = $this->getColWidth();
102
        if (!empty($strWidth)) {
103
            $this->addStyle('width', $strWidth);
104
        }
105
        if (!isset($this->aStyle['float'])) {
106
            $this->addStyle('float', 'left');
107
        }
108
109
        $strHTML  = '';
110
        $strHTML .= '   <div';
111
        $strHTML .= $this->buildID();
112
        $strHTML .= ">" . PHP_EOL;
113
        if (strtolower($this->strLabel) == self::HR) {
114
            $strHTML .= '<hr>';
115
        } else {
116
            if (strlen($this->strLabel) > 0 && $strWidth != '0%') {
117
                $strHTML .= '       <label';
118
                $strHTML .= $this->buildStyle();
119
                if (strlen($this->strLabelFor) > 0) {
120
                    $strHTML .= ' for="' . $this->strLabelFor . '"';
121
                }
122
                $strHTML .= '>' . $this->strLabel . '</label>' . PHP_EOL;
123
            }
124
            $iCnt = count($this->aChild);
125
            for ($i = 0; $i < $iCnt; $i++) {
126
                $strHTML .= $this->aChild[$i]->getHTML();
127
            }
128
            $strHTML .= '       <br style="clear:both;" />' . PHP_EOL;
129
        }
130
        $strHTML .= '   </div>' . PHP_EOL;
131
        return $strHTML;
132
    }
133
}
134