AbstractClaimDialectsOfferedType::fromXML()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 16
rs 9.9332
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\WSSecurity\XML\fed;
6
7
use DOMElement;
8
use SimpleSAML\WSSecurity\Assert\Assert;
9
use SimpleSAML\WSSecurity\XML\fed\ClaimDialect;
10
use SimpleSAML\XML\ExtendableAttributesTrait;
11
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
12
use SimpleSAML\XMLSchema\Exception\MissingElementException;
13
use SimpleSAML\XMLSchema\Exception\SchemaViolationException;
14
use SimpleSAML\XMLSchema\XML\Constants\NS;
15
16
/**
17
 * Class defining the ClaimDialectsOfferedType element
18
 *
19
 * @package simplesamlphp/ws-security
20
 */
21
abstract class AbstractClaimDialectsOfferedType extends AbstractFedElement
22
{
23
    use ExtendableAttributesTrait;
24
25
26
    /** The namespace-attribute for the xs:anyAttribute element */
27
    public const XS_ANY_ATTR_NAMESPACE = NS::OTHER;
28
29
30
    /**
31
     * AbstractClaimDialectsOfferedType constructor
32
     *
33
     * @param array<\SimpleSAML\WSSecurity\XML\fed\ClaimDialect> $claimDialect
34
     * @param array<\SimpleSAML\XML\Attribute> $namespacedAttributes
35
     */
36
    final public function __construct(
37
        protected array $claimDialect,
38
        array $namespacedAttributes = [],
39
    ) {
40
        Assert::notEmpty($claimDialect, SchemaViolationException::class);
41
        Assert::allIsInstanceOf($claimDialect, ClaimDialect::class);
42
43
        $this->setAttributesNS($namespacedAttributes);
44
    }
45
46
47
    /**
48
     * @return array<\SimpleSAML\WSSecurity\XML\fed\ClaimDialect>
49
     */
50
    public function getClaimDialect(): array
51
    {
52
        return $this->claimDialect;
53
    }
54
55
56
    /**
57
     * Create an instance of this object from its XML representation.
58
     *
59
     * @param \DOMElement $xml
60
     * @return static
61
     *
62
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
63
     *   if the qualified name of the supplied element is wrong
64
     */
65
    public static function fromXML(DOMElement $xml): static
66
    {
67
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
68
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
69
70
        $claimDialect = ClaimDialect::getChildrenOfClass($xml);
71
        Assert::minCount(
72
            $claimDialect,
73
            1,
74
            'Missing <fed:ClaimDialect> in ClaimDialectsOffered.',
75
            MissingElementException::class,
76
        );
77
78
        return new static(
79
            $claimDialect,
80
            self::getAttributesNSFromXML($xml),
81
        );
82
    }
83
84
85
    /**
86
     * Add this ClaimDialectsOfferedType to an XML element.
87
     *
88
     * @param \DOMElement|null $parent The element we should append this ClaimDialectsOfferedType to.
89
     * @return \DOMElement
90
     */
91
    public function toXML(?DOMElement $parent = null): DOMElement
92
    {
93
        $e = parent::instantiateParentElement($parent);
94
95
        foreach ($this->getAttributesNS() as $attr) {
96
            $attr->toXML($e);
97
        }
98
99
        foreach ($this->getClaimDialect() as $claimDialect) {
100
            $claimDialect->toXML($e);
101
        }
102
103
        return $e;
104
    }
105
}
106