Passed
Push — main ( 2edd53...9e1454 )
by Stefan
02:35
created

FormDiv::fromXML()   A

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 <div> inside of the form.
8
 *  It can be used to arrange some fieldset horizontally or to group some 
9
 *  elements for JS hide/show operations
10
 *
11
 * @package Formgenerator
12
 * @author Stefanius <[email protected]>
13
 * @copyright MIT License - see the LICENSE file for details
14
 */
15
class FormDiv extends FormCollection
16
{
17
    /** float styles for the div */
18
    const   NONE    = -1;
19
    const   CLEAR   = 0;
20
    const   LEFT    = 1;
21
    const   RIGHT   = 2;
22
    
23
    /** @var int width of the div in percent     */
24
    protected int  $iWidth;
25
    /** @var int align of the div (FormDiv::NONE, FormDiv::LEFT, FormDiv::RIGHT, FormDiv::CLEAR)     */
26
    protected int  $iAlign;
27
28
    /**
29
     * Create a Div element
30
     * @param int $iWidth
31
     * @param int $iAlign
32
     */
33
    public function __construct(int $iWidth = 0, int $iAlign = self::CLEAR)
34
    {
35
        parent::__construct(0);
36
        $this->iAlign = $iAlign;
37
        $this->iWidth = $iWidth;
38
        if ($this->iWidth > 0) {
39
            $this->addStyle('width', $this->iWidth . '%');
40
        }
41
        switch ($this->iAlign) {
42
        case self::CLEAR:
43
            $this->addStyle('clear', 'both');
44
            break;
45
        case self::LEFT:
46
            $this->addStyle('float', 'left');
47
            break;
48
        case self::RIGHT:
49
            $this->addStyle('float', 'right');
50
            break;
51
        case self::NONE:
52
        default:
53
            break;
54
        }
55
    }
56
    
57
    /**
58
     * {@inheritDoc}
59
     * @see \SKien\Formgenerator\FormElement::fromXML()
60
     */
61
    static public function fromXML(\DOMElement $oXMLElement, FormCollection $oFormParent) : ?FormElement
62
    {
63
        $iAlign = self::CLEAR;
64
        $strAlign = self::getAttribString($oXMLElement, 'align', 'CLEAR');
65
        $strConstName = 'self::' . strtoupper($strAlign);
66
        if (defined($strConstName)) {
67
            $iAlign = constant($strConstName);
68
        } else {
69
            trigger_error('Unknown Constant [' . $strConstName . '] for the Div-Alignment property!', E_USER_WARNING );
70
        }
71
        $iWidth = self::getAttribInt($oXMLElement, 'width', 0);
72
        $oFormElement = new self($iWidth, $iAlign);
73
        $oFormParent->add($oFormElement);
74
        $oFormElement->readAdditionalXML($oXMLElement);
75
        
76
        return $oFormElement;
77
    }
78
    
79
    /**
80
     * Build the HTML-notation for the div element.
81
     */
82
    public function getHTML() : string
83
    {
84
        $strHTML  = '';
85
        $strHTML .= '<div';
86
        $strHTML .= $this->buildID();
87
        $strHTML .= $this->buildStyle();
88
        $strHTML .= $this->buildAttributes();
89
        $strHTML .= ">" . PHP_EOL;
90
        $iCnt = count($this->aChild);
91
        for ($i = 0; $i < $iCnt; $i++) {
92
            $strHTML .= $this->aChild[$i]->GetHTML();
93
        }
94
        $strHTML .= '</div>' . PHP_EOL;
95
        return $strHTML;
96
    }
97
}
98
99