AbstractAttributeStatementType   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 69
rs 10
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A toXML() 0 9 2
A fromXML() 0 12 1
A __construct() 0 7 1
A getAttributes() 0 3 1
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\InvalidDOMElementException;
10
use SimpleSAML\XMLSchema\Exception\MissingElementException;
11
use SimpleSAML\XMLSchema\Exception\SchemaViolationException;
12
use SimpleSAML\XMLSchema\Exception\TooManyElementsException;
13
14
/**
15
 * SAML AttributeStatementType abstract data type.
16
 *
17
 * @package simplesamlphp/saml11
18
 */
19
abstract class AbstractAttributeStatementType extends AbstractSubjectStatementType
20
{
21
    /**
22
     * Initialize a saml:AttributeStatementType from scratch
23
     *
24
     * @param \SimpleSAML\SAML11\XML\saml\Subject $subject
25
     * @param array<\SimpleSAML\SAML11\XML\saml\Attribute> $attribute
26
     */
27
    final public function __construct(
28
        Subject $subject,
29
        protected array $attribute = [],
30
    ) {
31
        Assert::allIsInstanceOf($attribute, Attribute::class, SchemaViolationException::class);
32
33
        parent::__construct($subject);
34
    }
35
36
37
    /**
38
     * Collect the value of the attribute-property
39
     *
40
     * @return array<\SimpleSAML\SAML11\XML\saml\Attribute>
41
     */
42
    public function getAttributes(): array
43
    {
44
        return $this->attribute;
45
    }
46
47
48
    /**
49
     * Convert XML into an AttributeStatementType
50
     *
51
     * @param \DOMElement $xml The XML element we should load
52
     * @return static
53
     *
54
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
55
     *   if the qualified name of the supplied element is wrong
56
     */
57
    public static function fromXML(DOMElement $xml): static
58
    {
59
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
60
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
61
62
        $subject = Subject::getChildrenOfClass($xml);
63
        Assert::minCount($subject, 1, MissingElementException::class);
64
        Assert::maxCount($subject, 1, TooManyElementsException::class);
65
66
        return new static(
67
            array_pop($subject),
68
            Attribute::getChildrenOfClass($xml),
69
        );
70
    }
71
72
73
    /**
74
     * Convert this AttributeStatementType to XML.
75
     *
76
     * @param \DOMElement $parent The element we are converting to XML.
77
     * @return \DOMElement The XML element after adding the data corresponding to this AttributeStatementType.
78
     */
79
    public function toXML(?DOMElement $parent = null): DOMElement
80
    {
81
        $e = parent::toXML($parent);
82
83
        foreach ($this->getAttributes() as $attr) {
84
            $attr->toXML($e);
85
        }
86
87
        return $e;
88
    }
89
}
90