Passed
Push — master ( 54bedc...fe2750 )
by Tim
02:40
created

PolicyAttachment::fromXML()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
dl 0
loc 20
rs 9.7998
c 1
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\WSSecurity\XML\wsp;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\WSSecurity\XML\wsp\AppliesTo;
10
use SimpleSAML\WSSecurity\XML\wsp\Policy;
11
use SimpleSAML\WSSecurity\XML\wsp\PolicyReference;
12
use SimpleSAML\XML\Exception\InvalidDOMElementException;
13
use SimpleSAML\XML\Exception\MissingElementException;
14
use SimpleSAML\XML\Exception\TooManyElementsException;
15
use SimpleSAML\XML\ExtendableAttributesTrait;
16
use SimpleSAML\XML\ExtendableElementTrait;
17
use SimpleSAML\XML\XsNamespace as NS;
18
19
use function array_merge;
20
21
/**
22
 * Class representing a wsp:PolicyAttachment element.
23
 *
24
 * @package simplesamlphp/ws-security
25
 */
26
final class PolicyAttachment extends AbstractWspElement
27
{
28
    use ExtendableAttributesTrait;
29
    use ExtendableElementTrait;
0 ignored issues
show
introduced by
The trait SimpleSAML\XML\ExtendableElementTrait requires some properties which are not provided by SimpleSAML\WSSecurity\XML\wsp\PolicyAttachment: $namespaceURI, $localName, $childNodes
Loading history...
30
31
    /** The namespace-attribute for the xs:any element */
32
    public const XS_ANY_ELT_NAMESPACE = NS::OTHER;
33
34
    /** The namespace-attribute for the xs:anyAttribute element */
35
    public const XS_ANY_ATTR_NAMESPACE = NS::ANY;
36
37
38
    /**
39
     * Initialize a wsp:PolicyAttachment
40
     *
41
     * @param \SimpleSAML\WSSecurity\XML\wsp\AppliesTo $appliesTo
42
     * @param (\SimpleSAML\WSSecurity\XML\wsp\Policy|\SimpleSAML\WSSecurity\XML\wsp\PolicyReference)[] $policies
43
     * @param array<\SimpleSAML\XML\SerializableElementInterface> $children
44
     * @param array<\SimpleSAML\XML\Attribute> $namespacedAttributes
45
     */
46
    public function __construct(
47
        protected AppliesTo $appliesTo,
48
        protected array $policies,
49
        array $children = [],
50
        array $namespacedAttributes = [],
51
    ) {
52
        $this->setElements($children);
53
        $this->setAttributesNS($namespacedAttributes);
54
    }
55
56
57
    /**
58
     * Collect the value of the AppliesTo property.
59
     *
60
     * @return \SimpleSAML\WSSecurity\XML\wsp\AppliesTo
61
     */
62
    public function getAppliesTo(): AppliesTo
63
    {
64
        return $this->appliesTo;
65
    }
66
67
68
    /**
69
     * Collect the value of the Policies property.
70
     *
71
     * @return (\SimpleSAML\WSSecurity\XML\wsp\Policy|\SimpleSAML\WSSecurity\XML\wsp\PolicyReference)[]
72
     */
73
    public function getPolicies(): array
74
    {
75
        return $this->policies;
76
    }
77
78
79
    /*
80
     * Convert XML into an wsp:PolicyAttachment element
81
     *
82
     * @param \DOMElement $xml The XML element we should load
83
     * @return static
84
     *
85
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
86
     *   If the qualified name of the supplied element is wrong
87
     */
88
    public static function fromXML(DOMElement $xml): static
89
    {
90
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
91
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
92
93
        $appliesTo = AppliesTo::getChildrenOfClass($xml);
94
        Assert::minCount($appliesTo, 1, MissingElementException::class);
95
        Assert::maxCount($appliesTo, 1, TooManyElementsException::class);
96
97
        $policy = Policy::getChildrenOfClass($xml);
98
        $policyReference = PolicyReference::getChildrenOfClass($xml);
99
100
        $policies = array_merge($policy, $policyReference);
101
        Assert::minCount($policies, 1, MissingElementException::class);
102
103
        return new static(
104
            $appliesTo[0],
105
            $policies,
106
            self::getChildElementsFromXML($xml),
107
            self::getAttributesNSFromXML($xml),
108
        );
109
    }
110
111
112
    /**
113
     * Convert this wsp:PolicyAttachment to XML.
114
     *
115
     * @param \DOMElement|null $parent The element we should add this wsp:AppliesTo to.
116
     * @return \DOMElement This wsp:AppliesTo element.
117
     */
118
    public function toXML(DOMElement $parent = null): DOMElement
119
    {
120
        $e = $this->instantiateParentElement($parent);
121
122
        $this->getAppliesTo()->toXML($e);
123
124
        foreach ($this->getPolicies() as $pol) {
125
            $pol->toXML($e);
126
        }
127
128
        foreach ($this->getAttributesNS() as $attr) {
129
            $attr->toXML($e);
130
        }
131
132
        /** @psalm-var \SimpleSAML\XML\SerializableElementInterface $child */
133
        foreach ($this->getElements() as $child) {
134
            if (!$child->isEmptyElement()) {
135
                $child->toXML($e);
136
            }
137
        }
138
139
        return $e;
140
    }
141
}
142