Passed
Push — develop ( 10444e...4631c2 )
by Mikaël
01:29
created

AbstractAttributeHandler::getAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace WsdlToPhp\DomHandler;
6
7
use DOMAttr;
8
9
class AbstractAttributeHandler extends AbstractNodeHandler
10
{
11
    public const DEFAULT_VALUE_TYPE = 'string';
12
13
    public const ATTRIBUTE_NAMESPACE = 'namespace';
14
15
    public const ATTRIBUTE_NAME = 'name';
16
17
    public const ATTRIBUTE_REF = 'ref';
18
19
    public const ATTRIBUTE_VALUE = 'value';
20
21
    public const ATTRIBUTE_TYPE = 'type';
22
23
    public const ATTRIBUTE_ABSTRACT = 'abstract';
24
25
    public const ATTRIBUTE_MAX_OCCURS = 'maxOccurs';
26
27
    public const ATTRIBUTE_MIN_OCCURS = 'minOccurs';
28
29
    public const ATTRIBUTE_NILLABLE = 'nillable';
30
31
    public const VALUE_UNBOUNDED = 'unbounded';
32
33
    /**
34
     * @deprecated
35
     */
36
    public const DEFAULT_OCCURENCE_VALUE = 1;
37
38
    public const DEFAULT_OCCURRENCE_VALUE = 1;
39
40
    public function getAttribute(): DOMAttr
41
    {
42
        return $this->getNode();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getNode() returns the type DOMNode which includes types incompatible with the type-hinted return DOMAttr.
Loading history...
43
    }
44
45
    /**
46
     * Tries to get attribute type on the same node in order to return the value of the attribute in its type.
47
     */
48
    public function getType(): ?string
49
    {
50
        $type = null;
51
        if (($parent = $this->getParent()) instanceof ElementHandler && $parent->hasAttribute(self::ATTRIBUTE_TYPE)) {
52
            $type = $parent->getAttribute(self::ATTRIBUTE_TYPE)->getValue(false, false);
53
        }
54
55
        return $type;
56
    }
57
58
    public function getValue(bool $withNamespace = false, bool $withinItsType = true, ?string $asType = self::DEFAULT_VALUE_TYPE)
59
    {
60
        $value = $this->getAttribute()->value;
61
        if (false === $withNamespace && !empty($value)) {
62
            $value = implode('', array_slice(explode(':', $value), -1, 1));
63
        }
64
        if (null !== $value && true === $withinItsType) {
65
            $value = self::getValueWithinItsType($value, empty($asType) ? $this->getType() : $asType);
66
        }
67
68
        return $value;
69
    }
70
71
    public function getValueNamespace(): ?string
72
    {
73
        $value = $this->getAttribute()->value;
74
        $namespace = null;
75
        if (false !== strpos($value, ':')) {
76
            $namespace = implode('', array_slice(explode(':', $value), 0, -1));
77
        }
78
79
        return $namespace;
80
    }
81
82
    /**
83
     * Returns the value with good type.
84
     *
85
     * @param mixed       $value     the value
86
     * @param null|string $knownType the value
87
     *
88
     * @return mixed
89
     */
90
    public static function getValueWithinItsType($value, ?string $knownType = null)
91
    {
92
        if (is_int($value) || (!is_null($value) && in_array($knownType, [
93
            'time',
94
            'positiveInteger',
95
            'unsignedLong',
96
            'unsignedInt',
97
            'short',
98
            'long',
99
            'int',
100
            'integer',
101
        ], true))) {
102
            return intval($value);
103
        }
104
        if (is_float($value) || (!is_null($value) && in_array($knownType, [
105
            'float',
106
            'double',
107
            'decimal',
108
        ], true))) {
109
            return floatval($value);
110
        }
111
        if (is_bool($value) || (!is_null($value) && in_array($knownType, [
112
            'bool',
113
            'boolean',
114
        ], true))) {
115
            return 'true' === $value || true === $value || 1 === $value || '1' === $value;
116
        }
117
118
        return $value;
119
    }
120
}
121