toXML()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 7
c 1
b 0
f 1
nc 2
nop 1
dl 0
loc 14
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\SAML11\Type\DecisionTypeValue;
10
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
11
use SimpleSAML\XMLSchema\Exception\MissingElementException;
12
use SimpleSAML\XMLSchema\Exception\SchemaViolationException;
13
use SimpleSAML\XMLSchema\Exception\TooManyElementsException;
14
use SimpleSAML\XMLSchema\Type\AnyURIValue;
15
16
use function strval;
17
18
/**
19
 * SAML AuthorizationDecisionStatementType abstract data type.
20
 *
21
 * @package simplesamlphp/saml11
22
 */
23
abstract class AbstractAuthorizationDecisionStatementType extends AbstractSubjectStatementType
24
{
25
    /**
26
     * Initialize a saml:AuthorizationDecisionStatementType from scratch
27
     *
28
     * @param \SimpleSAML\XMLSchema\Type\AnyURIValue $resource
29
     * @param \SimpleSAML\SAML11\Type\DecisionTypeValue $decision
30
     * @param \SimpleSAML\SAML11\XML\saml\Subject $subject
31
     * @param array<\SimpleSAML\SAML11\XML\saml\Action> $action
32
     * @param \SimpleSAML\SAML11\XML\saml\Evidence|null $evidence
33
     */
34
    final public function __construct(
35
        Subject $subject,
36
        // Uses the base AnyURIValue because the SAML specification allows this attribute to be an empty string
37
        protected AnyURIValue $resource,
38
        protected DecisionTypeValue $decision,
39
        protected array $action = [],
40
        protected ?Evidence $evidence = null,
41
    ) {
42
        Assert::minCount($action, 1, MissingElementException::class);
43
        Assert::allIsInstanceOf($action, Action::class, SchemaViolationException::class);
44
45
        parent::__construct($subject);
46
    }
47
48
49
    /**
50
     * Collect the value of the resource-property
51
     *
52
     * @return \SimpleSAML\XMLSchema\Type\AnyURIValue
53
     */
54
    public function getResource(): AnyURIValue
55
    {
56
        return $this->resource;
57
    }
58
59
60
    /**
61
     * Collect the value of the decision-property
62
     *
63
     * @return \SimpleSAML\SAML11\Type\DecisionTypeValue
64
     */
65
    public function getDecision(): DecisionTypeValue
66
    {
67
        return $this->decision;
68
    }
69
70
71
    /**
72
     * Collect the value of the evidence-property
73
     *
74
     * @return \SimpleSAML\SAML11\XML\saml\Evidence|null
75
     */
76
    public function getEvidence(): ?Evidence
77
    {
78
        return $this->evidence;
79
    }
80
81
82
    /**
83
     * Collect the value of the action-property
84
     *
85
     * @return array<\SimpleSAML\SAML11\XML\saml\Action>
86
     */
87
    public function getAction(): array
88
    {
89
        return $this->action;
90
    }
91
92
93
    /**
94
     * Convert XML into an AuthorizationDecisionStatementType
95
     *
96
     * @param \DOMElement $xml The XML element we should load
97
     * @return static
98
     *
99
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
100
     *   if the qualified name of the supplied element is wrong
101
     */
102
    public static function fromXML(DOMElement $xml): static
103
    {
104
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
105
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
106
107
        $subject = Subject::getChildrenOfClass($xml);
108
        Assert::minCount($subject, 1, MissingElementException::class);
109
        Assert::maxCount($subject, 1, TooManyElementsException::class);
110
111
        $evidence = Evidence::getChildrenOfClass($xml);
112
        Assert::maxCount($evidence, 1, TooManyElementsException::class);
113
114
        return new static(
115
            array_pop($subject),
116
            self::getAttribute($xml, 'Resource', AnyURIValue::class),
117
            self::getAttribute($xml, 'Decision', DecisionTypeValue::class),
118
            Action::getChildrenOfClass($xml),
119
            array_pop($evidence),
120
        );
121
    }
122
123
124
    /**
125
     * Convert this AuthorizationDecisionStatementType to XML.
126
     *
127
     * @param \DOMElement $parent The element we are converting to XML.
128
     * @return \DOMElement The XML element after adding the data
129
     *   corresponding to this AuthorizationDecisionStatementType.
130
     */
131
    public function toXML(?DOMElement $parent = null): DOMElement
132
    {
133
        $e = parent::toXML($parent);
134
135
        $e->setAttribute('Resource', strval($this->getResource()));
136
        $e->setAttribute('Decision', strval($this->getDecision()));
137
138
        foreach ($this->getAction() as $action) {
139
            $action->toXML($e);
140
        }
141
142
        $this->getEvidence()?->toXML($e);
143
144
        return $e;
145
    }
146
}
147