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

XMLHelper::getAttribStringArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
nc 2
nop 2
dl 0
loc 9
rs 10
c 1
b 0
f 0
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
 */
12
trait XMLHelper
13
{
14
    /**
15
     * Get the string value of named attrib or return default value, if attrib not exist
16
     * @param \DOMElement $oXMLElement
17
     * @param string $strName
18
     * @param string $strDefault
19
     * @return string|null
20
     */
21
    static protected function getAttribString(\DOMElement $oXMLElement, string $strName, ?string $strDefault = null) : ?string
22
    {
23
        $oAttrib = $oXMLElement->attributes->getNamedItem($strName);
0 ignored issues
show
Bug introduced by
The method getNamedItem() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

23
        /** @scrutinizer ignore-call */ 
24
        $oAttrib = $oXMLElement->attributes->getNamedItem($strName);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
24
        $strValue = $oAttrib ? $oAttrib->nodeValue : $strDefault;
25
        return $strValue;
26
    }
27
    
28
    /**
29
     * Get the integer value of named attrib or return default value, if attrib not exist
30
     * @param \DOMElement $oXMLElement
31
     * @param string $strName
32
     * @param int $iDefault
33
     * @return int|null
34
     */
35
    static protected function getAttribInt(\DOMElement $oXMLElement, string $strName, ?int $iDefault = null) : ?int
36
    {
37
        $oAttrib = $oXMLElement->attributes->getNamedItem($strName);
38
        $iValue = $oAttrib ? intval($oAttrib->nodeValue) : $iDefault;
39
        return $iValue;
40
    }
41
    
42
    /**
43
     * Get an array of string values of the named attrib.
44
     * The attrib must contain a list spearated by comma.
45
     * @param \DOMElement $oXMLElement
46
     * @param string $strName
47
     * @return array|null
48
     */
49
    static protected function getAttribStringArray(\DOMElement $oXMLElement, string $strName) : ?array
50
    {
51
        $aValues = null;
52
        $oAttrib = $oXMLElement->attributes->getNamedItem($strName);
53
        if ($oAttrib) {
54
            $strArray = $oAttrib->nodeValue;
55
            $aValues = array_map('trim', explode(',', $strArray));
56
        }
57
        return $aValues;
58
    }
59
    
60
    /**
61
     * Get an array of integer values of the named attrib.
62
     * The attrib must contain a list spearated by comma.
63
     * @param \DOMElement $oXMLElement
64
     * @param string $strName
65
     * @return array|null
66
     */
67
    static protected function getAttribIntArray(\DOMElement $oXMLElement, string $strName) : ?array
68
    {
69
        $aValues = null;
70
        $oAttrib = $oXMLElement->attributes->getNamedItem($strName);
71
        if ($oAttrib) {
72
            $strArray = $oAttrib->nodeValue;
73
            $aValues = array_map('intval', explode(',', $strArray));
74
        }
75
        return $aValues;
76
    }
77
    
78
    /**
79
     * Get the flags specified by the named attrib.
80
     * The attrib must contain the textlist of FormFlag - constants spearated by comma. 
81
     * @param \DOMElement $oXMLElement
82
     * @return int
83
     */
84
    static protected function getAttribFlags(\DOMElement $oXMLElement) : int
85
    {
86
        $wFlags = 0;
87
        $strFlags = $oXMLElement->getAttribute('flags');
88
        if (strlen($strFlags) > 0) {
89
            $aFlags = array_map('trim', explode(',', $strFlags));
90
            foreach ($aFlags as $strFlag) {
91
                $strConstName = __NAMESPACE__ . '\FormFlags::' . strtoupper($strFlag);
92
                if (defined($strConstName)) {
93
                    $wFlags += constant($strConstName);
94
                } else {
95
                    trigger_error('Unknown Constant [' . $strConstName . '] for the FormFlag property!', E_USER_WARNING );
96
                }
97
            }
98
        }
99
        return $wFlags;
100
    }
101
    
102
    /**
103
     * Read all known attributes that don't need any further processing. 
104
     * @param \DOMElement $oXMLElement
105
     */
106
    public function readElementAttributes(\DOMElement $oXMLElement) : void
107
    {
108
        $aAttributes = [
109
            'onclick', 
110
            'ondblclick', 
111
            'onchange', 
112
            'oninput', 
113
            'onfocus', 
114
            'onblur', 
115
            'onkeydown', 
116
            'onkeypress',
117
            'onkeyup',
118
            'title', 
119
            'placeholder', 
120
            'maxlength', 
121
        ];
122
        foreach ($aAttributes as $strAttribute) {
123
            if (($strValue = self::getAttribString($oXMLElement, $strAttribute)) !== null) {
124
                $this->addAttribute($strAttribute, $strValue);
0 ignored issues
show
Bug introduced by
It seems like addAttribute() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

124
                $this->/** @scrutinizer ignore-call */ 
125
                       addAttribute($strAttribute, $strValue);
Loading history...
125
            }
126
        }
127
    }
128
}