FormStatic   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 47
c 1
b 0
f 0
dl 0
loc 102
rs 10
wmc 15

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B getHTML() 0 32 7
A fromXML() 0 9 1
A buildClass() 0 14 5
A setLabelFor() 0 3 1
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
        return $oFormElement;
60
    }
61
62
    /**
63
     * Set the control for which a label should be created.
64
     * @param string $strLabelFor
65
     */
66
    public function setLabelFor(string $strLabelFor) : void
67
    {
68
        $this->strLabelFor = $strLabelFor;
69
    }
70
71
    /**
72
     * Build the HTML-notation for the static text
73
     * @return string
74
     * @internal
75
     */
76
    public function getHTML() : string
77
    {
78
        // no container div!
79
        $this->addStyle('float', 'left');
80
        $strWidth = ($this->oParent ? $this->oParent->getColWidth($this->iCol) : '');
81
        if (!empty($strWidth)) {
82
            $this->addStyle('width', $strWidth);
83
        }
84
        if ($this->oFlags->isSet(FormFlags::ALIGN_RIGHT)) {
85
            $this->addStyle('text-align', 'right');
86
        } else if ($this->oFlags->isSet(FormFlags::ALIGN_CENTER)) {
87
            $this->addStyle('text-align', 'center');
88
        }
89
        if ($this->oFlags->isSet(FormFlags::BOLD)) {
90
            $this->addStyle('font-weight', 'bold');
91
        }
92
93
        $strHTML  = '';
94
        $strHTML .= '<div';
95
        $strHTML .= $this->buildID();
96
        $strHTML .= $this->buildClass();
97
        $strHTML .= $this->buildStyle();
98
        $strHTML .= $this->buildAttributes();
99
        $strHTML .= '>';
100
        if (strlen($this->strLabelFor) > 0) {
101
            $strHTML .= '<label for="' . $this->strLabelFor . '">' . $this->strText . '</label>';
102
        } else {
103
            $strHTML .= $this->strText;
104
        }
105
        $strHTML .= '</div>' . PHP_EOL;
106
107
        return $strHTML;
108
    }
109
110
    /**
111
     * {@inheritDoc}
112
     * @see \SKien\Formgenerator\FormElement::buildClass()
113
     */
114
    protected function buildClass() : string
115
    {
116
        $strSep = '';
117
        if (strlen($this->strClass) !== 0) {
118
            $strSep = ' ';
119
        }
120
        if ($this->oFlags->isSet(FormFlags::ERROR)) {
121
            $this->strClass .= $strSep . 'error';
122
        } else if ($this->oFlags->isSet(FormFlags::HINT)) {
123
            $this->strClass = $strSep . 'hint';
124
        } else if ($this->oFlags->isSet(FormFlags::INFO)) {
125
            $this->strClass = $strSep . 'forminfo';
126
        }
127
        return parent::buildClass();
128
    }
129
}
130