AbstractClaimTypesRequestedType::toXML()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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