FormDiv::fromXML()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 12
nc 2
nop 2
dl 0
loc 16
rs 9.8666
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace SKien\Formgenerator;
5
6
/**
7
 *  Represents a &lt;div&gt; inside of the form.
8
 *  Div's can be used to arrange some fieldset horizontally or to group some
9
 *  elements for JS hide/show operations.
10
 *
11
 *  An example of arranging two div / fieldsets horizontally and adjusting the
12
 *  height of the two can be found in ColumnForm.php and ColumnFormXML.php in
13
 *  the example directory.
14
 *
15
 * @package Formgenerator
16
 * @author Stefanius <[email protected]>
17
 * @copyright MIT License - see the LICENSE file for details
18
 */
19
class FormDiv extends FormCollection
20
{
21
    /** 'normal' div with no float style */
22
    const   NONE    = -1;
23
    /** Use `CLEAR` div after one or more floating divs */
24
    const   CLEAR   = 0;
25
    /** Left floating div */
26
    const   LEFT    = 1;
27
    /** Right floating div */
28
    const   RIGHT   = 2;
29
30
    /** @var int width of the div in percent     */
31
    protected int  $iWidth;
32
    /** @var int align of the div (FormDiv::NONE, FormDiv::LEFT, FormDiv::RIGHT, FormDiv::CLEAR)     */
33
    protected int  $iAlign;
34
35
    /**
36
     * Create a Div element.
37
     * @param int $iWidth   width of the div in %
38
     * @param int $iAlign   align of the div (FormDiv::NONE, FormDiv::LEFT, FormDiv::RIGHT, FormDiv::CLEAR)
39
     */
40
    public function __construct(int $iWidth = 0, int $iAlign = self::CLEAR)
41
    {
42
        parent::__construct(0);
43
        $this->iAlign = $iAlign;
44
        $this->iWidth = $iWidth;
45
        if ($this->iWidth > 0) {
46
            $this->addStyle('width', $this->iWidth . '%');
47
        }
48
        switch ($this->iAlign) {
49
        case self::CLEAR:
50
            $this->addStyle('clear', 'both');
51
            break;
52
        case self::LEFT:
53
            $this->addStyle('float', 'left');
54
            break;
55
        case self::RIGHT:
56
            $this->addStyle('float', 'right');
57
            break;
58
        case self::NONE:
59
        default:
60
            break;
61
        }
62
    }
63
64
    /**
65
     * {@inheritDoc}
66
     * @see \SKien\Formgenerator\FormElement::fromXML()
67
     * @internal
68
     */
69
    static public function fromXML(\DOMElement $oXMLElement, FormCollection $oFormParent) : ?FormElement
70
    {
71
        $iAlign = self::CLEAR;
72
        $strAlign = self::getAttribString($oXMLElement, 'align', 'CLEAR');
73
        $strConstName = 'self::' . strtoupper($strAlign);
74
        if (defined($strConstName)) {
75
            $iAlign = constant($strConstName);
76
        } else {
77
            trigger_error('Unknown Constant [' . $strConstName . '] for the Div-Alignment property!', E_USER_ERROR);
78
        }
79
        $iWidth = self::getAttribInt($oXMLElement, 'width', 0);
80
        $oFormElement = new self($iWidth, $iAlign);
81
        $oFormParent->add($oFormElement);
82
        $oFormElement->readAdditionalXML($oXMLElement);
83
84
        return $oFormElement;
85
    }
86
87
    /**
88
     * Build the HTML-notation for the div element.
89
     * @internal
90
     */
91
    public function getHTML() : string
92
    {
93
        $strHTML  = '';
94
        $strHTML .= '<div';
95
        $strHTML .= $this->buildID();
96
        $strHTML .= $this->buildStyle();
97
        $strHTML .= $this->buildAttributes();
98
        $strHTML .= ">" . PHP_EOL;
99
        $iCnt = count($this->aChild);
100
        for ($i = 0; $i < $iCnt; $i++) {
101
            $strHTML .= $this->aChild[$i]->GetHTML();
102
        }
103
        $strHTML .= '</div>' . PHP_EOL;
104
        return $strHTML;
105
    }
106
}
107
108