FormTextArea::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 5
dl 0
loc 6
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace SKien\Formgenerator;
5
6
/**
7
 * Text Area element.
8
 *
9
 * @package Formgenerator
10
 * @author Stefanius <[email protected]>
11
 * @copyright MIT License - see the LICENSE file for details
12
 */
13
class FormTextArea extends FormInput
14
{
15
    /** @var int col count for the textarea     */
16
    protected int $iCols;
17
    /** @var int rowcount for the textarea     */
18
    protected int $iRows;
19
20
    /**
21
     * Create a textarea element.
22
     * @param string $strName   Name (if no ID specified, name is used also as ID)
23
     * @param int $iCols        col count
24
     * @param int $iRows        row count
25
     * @param string $strWidth  width of the textarea
26
     * @param int $wFlags       any combination of FormFlag constants
27
     */
28
    public function __construct(string $strName, int $iCols, int $iRows, string $strWidth = '95%', int $wFlags = 0)
29
    {
30
        parent::__construct($strName, -1, $wFlags);
31
        $this->iCols = $iCols;
32
        $this->iRows = $iRows;
33
        $this->addStyle('width', $strWidth);
34
    }
35
36
    /**
37
     * {@inheritDoc}
38
     * @see \SKien\Formgenerator\FormElement::fromXML()
39
     * @internal
40
     */
41
    static public function fromXML(\DOMElement $oXMLElement, FormCollection $oFormParent) : ?FormElement
42
    {
43
        $strName = self::getAttribString($oXMLElement, 'name');
44
        $iCols = self::getAttribInt($oXMLElement, 'cols', 80);
45
        $iRows = self::getAttribInt($oXMLElement, 'rows', 10);
46
        $strWidth = self::getAttribString($oXMLElement, 'width', '95%');
47
        $wFlags = self::getAttribFlags($oXMLElement);
48
        $oFormElement = new self($strName, $iCols, $iRows, $strWidth, $wFlags);
49
        $oFormParent->add($oFormElement);
50
        $oFormElement->readAdditionalXML($oXMLElement);
51
        return $oFormElement;
52
    }
53
54
    /**
55
     * Build the HTML-notation for the textarea
56
     * @return string
57
     * @internal
58
     */
59
    public function getHTML() : string
60
    {
61
        $this->processFlags();
62
63
        $this->strID = $this->strID ?: $this->strName;
64
65
        $strValue = '';
66
        if (!$this->oFlags->isSet(FormFlags::SET_JSON_DATA)) {
67
            $strValue = $this->oFG->getData()->getValue($this->strName);
68
69
            // CR only relevant for Textareas ...
70
            if ($this->oFlags->isSet(FormFlags::REPLACE_BR_CR)) {
71
                $strValue = str_replace('<br>', "\n", $strValue);
72
                $strValue = str_replace('<br/>', "\n", $strValue);
73
                $strValue = str_replace('<br />', "\n", $strValue);
74
            }
75
        }
76
77
        $strHTML = $this->buildContainerDiv();
78
        $strHTML .= '<textarea';
79
        $strHTML .= ' name="' . $this->strName . '"';
80
        $strHTML .= $this->buildClass();
81
        $strHTML .= $this->buildID();
82
        $strHTML .= ' cols="' . $this->iCols . '"';
83
        $strHTML .= ' rows="' . $this->iRows . '"';
84
        $strHTML .= $this->buildStyle();
85
        $strHTML .= $this->buildAttributes();
86
        $strHTML .= $this->buildTabindex();
87
        $strHTML .= '>' . $strValue . '</textarea>';
88
        $strHTML .= $this->buildSelectButton('picker_top');
89
90
        $strHTML .= '</div>' . PHP_EOL;
91
92
        return $strHTML;
93
    }
94
}
95