AbstractAttributeType::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 8
rs 10
cc 1
nc 1
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML11\XML\saml;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\SAML11\Type\SAMLAnyURIValue;
10
use SimpleSAML\SAML11\Type\SAMLStringValue;
11
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
12
use SimpleSAML\XMLSchema\Exception\SchemaViolationException;
13
14
/**
15
 * SAML AttributeType abstract data type.
16
 *
17
 * @package simplesamlphp/saml11
18
 */
19
abstract class AbstractAttributeType extends AbstractAttributeDesignatorType
20
{
21
    /**
22
     * Initialize a saml:AttributeType from scratch
23
     *
24
     * @param \SimpleSAML\SAML11\Type\SAMLStringValue $AttributeName
25
     * @param \SimpleSAML\SAML11\Type\SAMLAnyURIValue $AttributeNamespace
26
     * @param array<\SimpleSAML\SAML11\XML\saml\AttributeValue> $attributeValue
27
     */
28
    final public function __construct(
29
        SAMLStringValue $AttributeName,
30
        SAMLAnyURIValue $AttributeNamespace,
31
        protected array $attributeValue,
32
    ) {
33
        Assert::allIsInstanceOf($attributeValue, AttributeValue::class, SchemaViolationException::class);
34
35
        parent::__construct($AttributeName, $AttributeNamespace);
36
    }
37
38
39
    /**
40
     * Collect the value of the attributeValue-property
41
     *
42
     * @return array<\SimpleSAML\SAML11\XML\saml\AttributeValue>
43
     */
44
    public function getAttributeValue(): array
45
    {
46
        return $this->attributeValue;
47
    }
48
49
50
    /**
51
     * Convert XML into an AttributeType
52
     *
53
     * @param \DOMElement $xml The XML element we should load
54
     * @return static
55
     *
56
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
57
     *   if the qualified name of the supplied element is wrong
58
     */
59
    public static function fromXML(DOMElement $xml): static
60
    {
61
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
62
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
63
64
        $attributeValue = AttributeValue::getChildrenOfClass($xml);
65
        $AttributeName = self::getAttribute($xml, 'AttributeName', SAMLStringValue::class);
66
        $AttributeNamespace = self::getAttribute($xml, 'AttributeNamespace', SAMLAnyURIValue::class);
67
68
        return new static($AttributeName, $AttributeNamespace, $attributeValue);
69
    }
70
71
72
    /**
73
     * Convert this AttributeType to XML.
74
     *
75
     * @param \DOMElement $parent The element we are converting to XML.
76
     * @return \DOMElement The XML element after adding the data corresponding to this AttributeType.
77
     */
78
    public function toXML(?DOMElement $parent = null): DOMElement
79
    {
80
        $e = parent::toXML($parent);
81
82
        foreach ($this->getAttributeValue() as $av) {
83
            $av->toXML($e);
84
        }
85
86
        return $e;
87
    }
88
}
89