Passed
Pull Request — master (#226)
by Jaime Pérez
02:37
created

AttributeValue   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 32
c 3
b 0
f 0
dl 0
loc 108
rs 10
wmc 12

5 Methods

Rating   Name   Duplication   Size   Complexity  
A toXML() 0 10 2
A getElement() 0 3 1
A getString() 0 3 1
A fromXML() 0 6 1
B __construct() 0 31 7
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SAML2\XML\saml;
6
7
use DOMElement;
8
use InvalidArgumentException;
9
use SAML2\Constants;
10
use SAML2\DOMDocumentFactory;
11
use Webmozart\Assert\Assert;
12
13
/**
14
 * Serializable class representing an AttributeValue.
15
 *
16
 * @package simplesamlphp/saml2
17
 */
18
class AttributeValue extends AbstractSamlElement
19
{
20
    /**
21
     * The raw DOMElement representing this value.
22
     *
23
     * @var \DOMElement
24
     */
25
    protected $element;
26
27
28
    /**
29
     * Create an AttributeValue.
30
     *
31
     * @param mixed $value The value of this element. Can be one of:
32
     *  - string                       Create an attribute value with a simple string.
33
     *  - \DOMElement(AttributeValue)  Create an attribute value of the given DOMElement.
34
     *  - \DOMElement                  Create an attribute value with the given DOMElement as a child.
35
     *
36
     * @throws \InvalidArgumentException if the supplied value is neither a string or a DOMElement
37
     */
38
    public function __construct($value)
39
    {
40
        Assert::true(is_string($value) || is_int($value) || $value instanceof DOMElement);
41
42
        if (is_string($value)) {
43
            $doc = DOMDocumentFactory::create();
44
            $this->element = $doc->createElementNS(Constants::NS_SAML, 'saml:AttributeValue');
45
            $this->element->setAttributeNS(Constants::NS_XSI, 'xsi:type', 'xs:string');
46
            $this->element->appendChild($doc->createTextNode($value));
47
48
            /* Make sure that the xs-namespace is available in the AttributeValue (for xs:string). */
49
            $this->element->setAttributeNS(Constants::NS_XS, 'xs:tmp', 'tmp');
50
            $this->element->removeAttributeNS(Constants::NS_XS, 'tmp');
51
            return;
52
        } elseif (is_int($value)) {
53
            $doc = DOMDocumentFactory::create();
54
            $this->element = $doc->createElementNS(Constants::NS_SAML, 'saml:AttributeValue');
55
            $this->element->setAttributeNS(Constants::NS_XSI, 'xsi:type', 'xs:integer');
56
            $this->element->appendChild($doc->createTextNode(strval($value)));
57
58
            /* Make sure that the xs-namespace is available in the AttributeValue (for xs:int). */
59
            $this->element->setAttributeNS(Constants::NS_XS, 'xs:tmp', 'tmp');
60
            $this->element->removeAttributeNS(Constants::NS_XS, 'tmp');
61
            return;
62
        }
63
64
        if ($value->namespaceURI === Constants::NS_SAML && $value->localName === 'AttributeValue') {
65
            $this->element = $value;
66
        }
67
68
        $this->element = $value;
69
    }
70
71
72
    /**
73
     * Collect the value of the element-property
74
     *
75
     * @return \DOMElement
76
     */
77
    public function getElement(): DOMElement
78
    {
79
        return $this->element;
80
    }
81
82
83
    /**
84
     * Convert XML into a AttributeValue
85
     *
86
     * @param \DOMElement $xml The XML element we should load
87
     * @return \SAML2\XML\saml\AttributeValue
88
     * @throws \InvalidArgumentException if the qualified name of the supplied element is wrong
89
     */
90
    public static function fromXML(DOMElement $xml): object
91
    {
92
        Assert::same($xml->localName, 'AttributeValue');
93
        Assert::same($xml->namespaceURI, AttributeValue::NS);
94
95
        return new self($xml);
96
    }
97
98
99
    /**
100
     * Append this attribute value to an element.
101
     *
102
     * @param \DOMElement|null $parent The element we should append this attribute value to.
103
     * @return \DOMElement The generated AttributeValue element.
104
     */
105
    public function toXML(DOMElement $parent = null): DOMElement
106
    {
107
        if ($parent === null) {
108
            return $this->element;
109
        }
110
111
        /** @var \DOMElement $element */
112
        $element = $parent->ownerDocument->importNode($this->element, true);
113
        $parent->appendChild($element);
114
        return $element;
115
    }
116
117
118
    /**
119
     * Returns a plain text content of the attribute value.
120
     *
121
     * @return string
122
     */
123
    public function getString(): string
124
    {
125
        return $this->element->textContent;
126
    }
127
}
128