Passed
Push — main ( 2bf451...d45a63 )
by Stefan
02:14
created

FormStatic::getHTML()   B

Complexity

Conditions 7
Paths 48

Size

Total Lines 32
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 23
c 1
b 0
f 0
nc 48
nop 0
dl 0
loc 32
rs 8.6186
1
<?php
2
declare(strict_types=1);
3
4
namespace SKien\Formgenerator;
5
6
/**
7
 * Static Text element.
8
 *
9
 * The FormStatic element can be used for different purposes:
10
 * - just for the normal output of a text
11
 * - as a label that is assigned to a specific input element
12
 *   - set the `$strLabelFor` param in the constructor or use the `setLabelFor()` method
13
 * - As a hint
14
 *   - set the `FormFlags::HINT` flag
15
 * - To display an error message
16
 *   - set the `FormFlags::ERROR` flag
17
 * - As an information field
18
 *   - set the `FormFlags::INFO` flag
19
 *
20
 * @SKienImage FormStatic.png
21
 *
22
 * @package Formgenerator
23
 * @author Stefanius <[email protected]>
24
 * @copyright MIT License - see the LICENSE file for details
25
 */
26
class FormStatic extends FormElement
27
{
28
    /** @var string the text to display     */
29
    protected string $strText;
30
    /** @var string the if set, label with for attribute is created     */
31
    protected string $strLabelFor = '';
32
33
    /**
34
     * Create a static text element.
35
     * @param string $strText       text to display
36
     * @param int $wFlags           any combination of FormFlag constants
37
     * @param string $strLabelFor   crate label for this element
38
     */
39
    public function __construct(string $strText, int $wFlags = 0, string $strLabelFor = '')
40
    {
41
        $this->strText = $strText;
42
        $this->strLabelFor = $strLabelFor;
43
        parent::__construct($wFlags);
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
        $strText = self::getAttribString($oXMLElement, 'text', '');
54
        $wFlags = self::getAttribFlags($oXMLElement);
55
        $strLabelFor = self::getAttribString($oXMLElement, 'for', '');
56
        $oFormElement = new self($strText, $wFlags, $strLabelFor);
57
        $oFormParent->add($oFormElement);
58
        $oFormElement->readAdditionalXML($oXMLElement);
59
60
        return $oFormElement;
61
    }
62
63
    /**
64
     * Set the control for which a label should be created.
65
     * @param string $strLabelFor
66
     */
67
    public function setLabelFor(string $strLabelFor) : void
68
    {
69
        $this->strLabelFor = $strLabelFor;
70
    }
71
72
    /**
73
     * Build the HTML-notation for the static text
74
     * @return string
75
     * @internal
76
     */
77
    public function getHTML() : string
78
    {
79
        // no container div!
80
        $this->addStyle('float', 'left');
81
        $strWidth = ($this->oParent ? $this->oParent->getColWidth($this->iCol) : '');
82
        if (!empty($strWidth)) {
83
            $this->addStyle('width', $strWidth);
84
        }
85
        if ($this->oFlags->isSet(FormFlags::ALIGN_RIGHT)) {
86
            $this->addStyle('text-align', 'right');
87
        } else if ($this->oFlags->isSet(FormFlags::ALIGN_CENTER)) {
88
            $this->addStyle('text-align', 'center');
89
        }
90
        if ($this->oFlags->isSet(FormFlags::BOLD)) {
91
            $this->addStyle('font-weight', 'bold');
92
        }
93
94
        $strHTML  = '';
95
        $strHTML .= '<div';
96
        $strHTML .= $this->buildID();
97
        $strHTML .= $this->buildClass();
98
        $strHTML .= $this->buildStyle();
99
        $strHTML .= $this->buildAttributes();
100
        $strHTML .= '>';
101
        if (strlen($this->strLabelFor) > 0) {
102
            $strHTML .= '<label for="' . $this->strLabelFor . '">' . $this->strText . '</label>';
103
        } else {
104
            $strHTML .= $this->strText;
105
        }
106
        $strHTML .= '</div>' . PHP_EOL;
107
108
        return $strHTML;
109
    }
110
111
    /**
112
     * {@inheritDoc}
113
     * @see \SKien\Formgenerator\FormElement::buildClass()
114
     */
115
    protected function buildClass() : string
116
    {
117
        $strSep = '';
118
        if (strlen($this->strClass) !== 0) {
119
            $strSep = ' ';
120
        }
121
        if ($this->oFlags->isSet(FormFlags::ERROR)) {
122
            $this->strClass .= $strSep . 'error';
123
        } else if ($this->oFlags->isSet(FormFlags::HINT)) {
124
            $this->strClass = $strSep . 'hint';
125
        } else if ($this->oFlags->isSet(FormFlags::INFO)) {
126
            $this->strClass = $strSep . 'forminfo';
127
        }
128
        return parent::buildClass();
129
    }
130
}
131