FormButton   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 26
dl 0
loc 66
rs 10
c 1
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A getHTML() 0 17 3
A fromXML() 0 9 1
1
<?php
2
declare(strict_types=1);
3
4
namespace SKien\Formgenerator;
5
6
/**
7
 * HTML form button element (input type="button")
8
 *
9
 * @package Formgenerator
10
 * @author Stefanius <[email protected]>
11
 * @copyright MIT License - see the LICENSE file for details
12
 */
13
class FormButton extends FormInput
14
{
15
    /** @var string button text     */
16
    protected string $strBtnText;
17
18
    /**
19
     * Create button element.
20
     * > Note: <br/>
21
     * > Alignment `FormFlags::ALIGN_CENTER` / `FormFlags::ALIGN_RIGHT´ dont affect the
22
     *   alignment of the text within the button but the alignment of the button within the col!
23
     * @param string $strId        button id
24
     * @param string $strBtnText   button text (value)
25
     * @param string $strOnClick   onClick() handler
26
     * @param int $wFlags          any combination of FormFlag constants
27
     * @param string $strStyle     CSS style(s) (default: '')
28
     */
29
    public function __construct(string $strId, string $strBtnText, string $strOnClick, int $wFlags = 0, string $strStyle = '')
30
    {
31
        $this->oFlags = new FormFlags($wFlags);
32
        $this->strBtnText = $strBtnText;
33
        $this->strID = $strId;
34
        $this->addAttribute('onclick', $strOnClick);
35
36
        if (strlen($strStyle) > 0) {
37
            $this->parseStyle($strStyle);
38
        }
39
    }
40
41
    /**
42
     * {@inheritDoc}
43
     * @see \SKien\Formgenerator\FormElement::fromXML()
44
     * @internal
45
     */
46
    static public function fromXML(\DOMElement $oXMLElement, FormCollection $oFormParent) : ?FormElement
47
    {
48
        // id comes from FormElement::readAdditionalXML() !!
49
        $strText = self::getAttribString($oXMLElement, 'text');
50
        $wFlags = self::getAttribFlags($oXMLElement);
51
        $oFormElement = new self('', $strText, '', $wFlags);
52
        $oFormParent->add($oFormElement);
53
        $oFormElement->readAdditionalXML($oXMLElement);
54
        return $oFormElement;
55
    }
56
57
    /**
58
     * Build the HTML-notation for the button.
59
     * @return string
60
     * @internal
61
     */
62
    public function getHTML() : string
63
    {
64
        $strStyle = '';
65
        if ($this->oFlags->isSet(FormFlags::ALIGN_CENTER)) {
66
            $strStyle = 'text-align: center;';
67
        } else if ($this->oFlags->isSet(FormFlags::ALIGN_RIGHT)) {
68
            $strStyle = 'text-align: right;';
69
        }
70
        $strHTML  = $this->buildContainerDiv($strStyle);
71
72
        $strHTML .= '<input type="button" ';
73
        $strHTML .= $this->buildID();
74
        $strHTML .= $this->buildStyle();
75
        $strHTML .= $this->buildAttributes();
76
        $strHTML .= ' value="' . $this->strBtnText . '"></div>' . PHP_EOL;
77
78
        return $strHTML;
79
    }
80
}
81