Passed
Push — main ( 4485e3...ffe0c0 )
by Stefan
02:12
created

XMLHelper::getAttribBool()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 4
c 1
b 0
f 1
nc 3
nop 3
dl 0
loc 7
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace SKien\Formgenerator;
5
6
/**
7
 * Trait containing some Helper to generate the Form from XML-File
8
 * @package Formgenerator
9
 * @author Stefanius <[email protected]>
10
 * @copyright MIT License - see the LICENSE file for details
11
 * @internal
12
 */
13
trait XMLHelper
14
{
15
    /**
16
     * Get the string value of named attrib or return default value, if attrib not exist.
17
     * @param \DOMElement $oXMLElement
18
     * @param string $strName
19
     * @param string $strDefault
20
     * @return string|null
21
     */
22
    static protected function getAttribString(\DOMElement $oXMLElement, string $strName, ?string $strDefault = null) : ?string
23
    {
24
        if (!$oXMLElement->hasAttribute($strName)) {
25
            return $strDefault;
26
        }
27
        return $oXMLElement->getAttribute($strName);
28
    }
29
30
    /**
31
     * Get the integer value of named attrib or return default value, if attrib not exist.
32
     * @param \DOMElement $oXMLElement
33
     * @param string $strName
34
     * @param int $iDefault
35
     * @return int|null
36
     */
37
    static protected function getAttribInt(\DOMElement $oXMLElement, string $strName, ?int $iDefault = null) : ?int
38
    {
39
        if (!$oXMLElement->hasAttribute($strName)) {
40
            return $iDefault;
41
        }
42
        return intval($oXMLElement->getAttribute($strName));
43
    }
44
45
    /**
46
     * Get the float value of named attrib or return default value, if attrib not exist.
47
     * @param \DOMElement $oXMLElement
48
     * @param string $strName
49
     * @param float $fltDefault
50
     * @return float|null
51
     */
52
    static protected function getAttribFloat(\DOMElement $oXMLElement, string $strName, ?float $fltDefault = null) : ?float
53
    {
54
        if (!$oXMLElement->hasAttribute($strName)) {
55
            return $fltDefault;
56
        }
57
        return floatval($oXMLElement->getAttribute($strName));
58
    }
59
60
    /**
61
     * Get the boolean value of named attrib or return default value, if attrib not exist.
62
     * Legal values for boolean are true, false, 1 (which indicates true), and 0 (which indicates false).
63
     * @link https://www.w3schools.com/xml/schema_dtypes_misc.asp
64
     * @param \DOMElement $oXMLElement
65
     * @param string $strName
66
     * @param bool $iDefault
67
     * @return bool|null
68
     */
69
    static protected function getAttribBool(\DOMElement $oXMLElement, string $strName, ?bool $bDefault = null) : ?bool
70
    {
71
        if (!$oXMLElement->hasAttribute($strName)) {
72
            return $bDefault;
73
        }
74
        $strValue = $oXMLElement->getAttribute($strName);
75
        return ($strValue == 'true' || $strValue == '1');
76
    }
77
78
    /**
79
     * Get an array of string values of the named attrib.
80
     * The attrib must contain a list spearated by whitespace(s).
81
     * @param \DOMElement $oXMLElement
82
     * @param string $strName
83
     * @return array|null
84
     */
85
    static protected function getAttribStringArray(\DOMElement $oXMLElement, string $strName) : ?array
86
    {
87
        $aValues = null;
88
        $strArray = $oXMLElement->getAttribute($strName);
89
        if (strlen($strArray) > 0) {
90
            // to make it validateable by XSD-schema, we use a whitespace-separated list since
91
            // there is no way to define the delimiter for xs:list in XSD...
92
            $aValues = array_map('trim', preg_split('/\s+/', trim($strArray)));
93
        }
94
        return $aValues;
95
    }
96
97
    /**
98
     * Get an array of integer values of the named attrib.
99
     * The attrib must contain a list spearated by comma.
100
     * @param \DOMElement $oXMLElement
101
     * @param string $strName
102
     * @return array|null
103
     */
104
    static protected function getAttribIntArray(\DOMElement $oXMLElement, string $strName) : ?array
105
    {
106
        $aValues = null;
107
        $strArray = $oXMLElement->getAttribute($strName);
108
        if (strlen($strArray) > 0) {
109
            $aValues = array_map('intval', explode(',', $strArray));
110
        }
111
        return $aValues;
112
    }
113
114
    /**
115
     * Get the flags specified by the named attrib.
116
     * The attrib must contain a list of FormFlag - constants spearated by any whitespace(s).
117
     * @param \DOMElement $oXMLElement
118
     * @return int
119
     */
120
    static protected function getAttribFlags(\DOMElement $oXMLElement) : int
121
    {
122
        $wFlags = 0;
123
        $strFlags = $oXMLElement->getAttribute('flags');
124
        if (strlen($strFlags) > 0) {
125
            // to make it validateable by XSD-schema, we use a whitespace-separated list since
126
            // there is no way to define the delimiter for xs:list in XSD...
127
            $aFlags = array_map('trim', preg_split('/\s+/', trim($strFlags)));
128
            foreach ($aFlags as $strFlag) {
129
                $strConstName = __NAMESPACE__ . '\FormFlags::' . strtoupper($strFlag);
130
                if (defined($strConstName)) {
131
                    $wFlags += constant($strConstName);
132
                } else {
133
                    trigger_error('Unknown Constant [' . $strConstName . '] for the FormFlag property!', E_USER_ERROR);
134
                }
135
            }
136
        }
137
        return $wFlags;
138
    }
139
140
    /**
141
     * Read all known attributes that don't need any further processing.
142
     * @param \DOMElement $oXMLElement
143
     */
144
    protected function readElementAttributes(\DOMElement $oXMLElement, ?array $aAttributes) : array
145
    {
146
        $aAttributesToRead = [
147
            'onclick',
148
            'ondblclick',
149
            'onchange',
150
            'oninput',
151
            'onfocus',
152
            'onblur',
153
            'onkeydown',
154
            'onkeypress',
155
            'onkeyup',
156
            'title',
157
            'placeholder',
158
            'maxlength',
159
        ];
160
        if ($aAttributes == null) {
161
            $aAttributes = array();
162
        }
163
        foreach ($aAttributesToRead as $strAttribute) {
164
            if (($strValue = self::getAttribString($oXMLElement, $strAttribute)) !== null) {
165
                $aAttributes[$strAttribute] = $strValue;
166
            }
167
        }
168
        return $aAttributes;
169
    }
170
171
    /**
172
     * Get the child with the given tag name.
173
     * The given parent must only contain one chuld with this name!
174
     * @param string $strName
175
     * @return \DOMElement|null
176
     */
177
    protected function getXMLChild(\DOMElement $oXMLElement, string $strName) : ?\DOMElement
178
    {
179
        $oList = $oXMLElement->getElementsByTagName($strName);
180
        if ($oList->count() === 1) {
181
            $oChild = $oList->item(0);
182
            if ($oChild instanceof \DOMElement) {
183
                return $oChild;
184
            }
185
        }
186
        return null;
187
    }
188
}
189