Completed
Push — master ( 179c60...360858 )
by Jaime Pérez
07:06
created

AttributeValue::fromXML()   A

Complexity

Conditions 6
Paths 3

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 13
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 19
rs 9.2222
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SAML2\XML\saml;
6
7
use DOMElement;
8
use SAML2\Constants;
9
use SAML2\XML\AbstractXMLElement;
10
use Webmozart\Assert\Assert;
11
12
/**
13
 * Serializable class representing an AttributeValue.
14
 *
15
 * @package simplesamlphp/saml2
16
 */
17
class AttributeValue extends AbstractSamlElement
18
{
19
20
    /**
21
     * @var string|int|AbstractXMLElement|null
22
     */
23
    protected $value;
24
25
26
    /**
27
     * Create an AttributeValue.
28
     *
29
     * @param mixed $value The value of this element. Can be one of:
30
     *  - string
31
     *  - int
32
     *  - null
33
     *  - \SAML2\XML\AbstractXMLElement
34
     *
35
     * @throws \InvalidArgumentException if the supplied value is neither a string or a DOMElement
36
     */
37
    public function __construct($value)
38
    {
39
        Assert::true(
40
            is_string($value) || is_int($value) || is_null($value) || $value instanceof AbstractXMLElement,
41
            'Value must be of type "string", "int", "null" or "AbstractXMLElement".'
42
        );
43
        $this->value = $value;
44
    }
45
46
47
    /**
48
     * Get the XSI type of this attribute value.
49
     *
50
     * @return string
51
     */
52
    public function getXsiType(): string
53
    {
54
        switch (gettype($this->value)) {
55
            case "integer":
56
                return "xs:integer";
57
            case "NULL":
58
                return "xs:nil";
59
            case "object":
60
                return $this->value::NS_PREFIX . ":"  . AbstractXMLElement::getClassName(get_class($this->value));
61
            default:
62
                return "xs:string";
63
        }
64
    }
65
66
67
    /**
68
     * Get this attribute value.
69
     *
70
     * @return string|int|\SAML2\XML\AbstractXMLElement|null
71
     */
72
    public function getValue()
73
    {
74
        return $this->value;
75
    }
76
77
78
    /**
79
     * Convert XML into a AttributeValue
80
     *
81
     * @param \DOMElement $xml The XML element we should load
82
     *
83
     * @return \SAML2\XML\saml\AttributeValue
84
     * @throws \InvalidArgumentException if the qualified name of the supplied element is wrong
85
     */
86
    public static function fromXML(DOMElement $xml): object
87
    {
88
        Assert::same($xml->localName, 'AttributeValue');
89
        Assert::same($xml->namespaceURI, AttributeValue::NS);
90
        $value = $xml->textContent;
91
        if (
92
            $xml->hasAttributeNS(Constants::NS_XSI, "type") &&
93
            $xml->getAttributeNS(Constants::NS_XSI, "type") === "xs:integer"
94
        ) {
95
            $value = intval($value);
96
        } elseif (
97
            $xml->hasAttributeNS(Constants::NS_XSI, "nil") &&
98
            ($xml->getAttributeNS(Constants::NS_XSI, "nil") === "1" ||
99
                $xml->getAttributeNS(Constants::NS_XSI, "nil") === "true")
100
        ) {
101
            $value = null;
102
        }
103
104
        return new self($value);
105
    }
106
107
108
    /**
109
     * Append this attribute value to an element.
110
     *
111
     * @param \DOMElement|null $parent The element we should append this attribute value to.
112
     *
113
     * @return \DOMElement The generated AttributeValue element.
114
     */
115
    public function toXML(DOMElement $parent = null): DOMElement
116
    {
117
        $e = parent::instantiateParentElement($parent);
118
119
        $value = $this->value;
120
        switch (gettype($this->value)) {
121
            case "integer":
122
                // make sure that the xs namespace is available in the AttributeValue
123
                $e->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:xs', Constants::NS_XS);
124
125
                $e->setAttributeNS(Constants::NS_XSI, "xsi:type", "xs:integer");
126
                $value = strval($value);
127
                break;
128
            case "NULL":
129
                $e->setAttributeNS(Constants::NS_XSI, "xsi:nil", "1");
130
                $value = "";
131
                break;
132
            case "object":
133
                $value = $this->value->__toString();
134
        }
135
136
        $e->textContent = $value;
137
        return $e;
138
    }
139
}
140