AbstractAttributeStatementType::getAttributes()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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\XMLSchema\Exception\{
10
    InvalidDOMElementException,
11
    MissingElementException,
12
    SchemaViolationException,
13
    TooManyElementsException,
14
};
15
16
/**
17
 * SAML AttributeStatementType abstract data type.
18
 *
19
 * @package simplesamlphp/saml11
20
 */
21
abstract class AbstractAttributeStatementType extends AbstractSubjectStatementType
22
{
23
    /**
24
     * Initialize a saml:AttributeStatementType from scratch
25
     *
26
     * @param \SimpleSAML\SAML11\XML\saml\Subject $subject
27
     * @param array<\SimpleSAML\SAML11\XML\saml\Attribute> $attribute
28
     */
29
    final public function __construct(
30
        Subject $subject,
31
        protected array $attribute = [],
32
    ) {
33
        Assert::allIsInstanceOf($attribute, Attribute::class, SchemaViolationException::class);
34
35
        parent::__construct($subject);
36
    }
37
38
39
    /**
40
     * Collect the value of the attribute-property
41
     *
42
     * @return array<\SimpleSAML\SAML11\XML\saml\Attribute>
43
     */
44
    public function getAttributes(): array
45
    {
46
        return $this->attribute;
47
    }
48
49
50
    /**
51
     * Convert XML into an AttributeStatementType
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
        $subject = Subject::getChildrenOfClass($xml);
65
        Assert::minCount($subject, 1, MissingElementException::class);
66
        Assert::maxCount($subject, 1, TooManyElementsException::class);
67
68
        return new static(
69
            array_pop($subject),
70
            Attribute::getChildrenOfClass($xml),
71
        );
72
    }
73
74
75
    /**
76
     * Convert this AttributeStatementType to XML.
77
     *
78
     * @param \DOMElement $parent The element we are converting to XML.
79
     * @return \DOMElement The XML element after adding the data corresponding to this AttributeStatementType.
80
     */
81
    public function toXML(?DOMElement $parent = null): DOMElement
82
    {
83
        $e = parent::toXML($parent);
84
85
        foreach ($this->getAttributes() as $attr) {
86
            $attr->toXML($e);
87
        }
88
89
        return $e;
90
    }
91
}
92