AbstractConditionsType::toXML()   A
last analyzed

Complexity

Conditions 6
Paths 32

Size

Total Lines 25
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 12
c 1
b 0
f 0
nc 32
nop 1
dl 0
loc 25
rs 9.2222
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\SAMLDateTimeValue;
10
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
11
12
use function strval;
13
14
/**
15
 * SAML ConditionsType abstract data type.
16
 *
17
 * @package simplesamlphp/saml11
18
 */
19
abstract class AbstractConditionsType extends AbstractSamlElement
20
{
21
    /**
22
     * Initialize a saml:ConditionsType from scratch
23
     *
24
     * @param array<\SimpleSAML\SAML11\XML\saml\AudienceRestrictionCondition> $audienceRestrictionCondition
25
     * @param array<\SimpleSAML\SAML11\XML\saml\DoNotCacheCondition> $doNotCacheCondition
26
     * @param array<\SimpleSAML\SAML11\XML\saml\AbstractCondition> $condition
27
     * @param \SimpleSAML\SAML11\Type\SAMLDateTimeValue|null $notBefore
28
     * @param \SimpleSAML\SAML11\Type\SAMLDateTimeValue|null $notOnOrAfter
29
     */
30
    final public function __construct(
31
        protected array $audienceRestrictionCondition = [],
32
        protected array $doNotCacheCondition = [],
33
        protected array $condition = [],
34
        protected ?SAMLDateTimeValue $notBefore = null,
35
        protected ?SAMLDateTimeValue $notOnOrAfter = null,
36
    ) {
37
        Assert::allIsInstanceOf($audienceRestrictionCondition, AudienceRestrictionCondition::class);
38
        Assert::allIsInstanceOf($doNotCacheCondition, DoNotCacheCondition::class);
39
        Assert::allIsInstanceOf($condition, AbstractCondition::class);
40
    }
41
42
43
    /**
44
     * Collect the value of the notBefore-property
45
     *
46
     * @return \SimpleSAML\SAML11\Type\SAMLDateTimeValue|null
47
     */
48
    public function getNotBefore(): ?SAMLDateTimeValue
49
    {
50
        return $this->notBefore;
51
    }
52
53
54
    /**
55
     * Collect the value of the notOnOrAfter-property
56
     *
57
     * @return \SimpleSAML\SAML11\Type\SAMLDateTimeValue|null
58
     */
59
    public function getNotOnOrAfter(): ?SAMLDateTimeValue
60
    {
61
        return $this->notOnOrAfter;
62
    }
63
64
65
    /**
66
     * Collect the value of the audienceRestrictionCondition-property
67
     *
68
     * @return array<\SimpleSAML\SAML11\XML\saml\AudienceRestrictionCondition>
69
     */
70
    public function getAudienceRestrictionCondition(): array
71
    {
72
        return $this->audienceRestrictionCondition;
73
    }
74
75
76
    /**
77
     * Collect the value of the doNotCacheCondition-property
78
     *
79
     * @return array<\SimpleSAML\SAML11\XML\saml\DoNotCacheCondition>
80
     */
81
    public function getDoNotCacheCondition(): array
82
    {
83
        return $this->doNotCacheCondition;
84
    }
85
86
87
    /**
88
     * Collect the value of the condition-property
89
     *
90
     * @return array<\SimpleSAML\SAML11\XML\saml\Condition>
91
     */
92
    public function getCondition(): array
93
    {
94
        return $this->condition;
95
    }
96
97
98
    /**
99
     * Test if an object, at the state it's in, would produce an empty XML-element
100
     *
101
     * @return bool
102
     */
103
    public function isEmptyElement(): bool
104
    {
105
        return empty($this->getAudienceRestrictionCondition())
106
            && empty($this->getDoNotCacheCondition())
107
            && empty($this->getCondition())
108
            && empty($this->getNotBefore())
109
            && empty($this->getNotOnOrAfter());
110
    }
111
112
113
    /**
114
     * Convert XML into an ConditionsType
115
     *
116
     * @param \DOMElement $xml The XML element we should load
117
     * @return static
118
     *
119
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
120
     *   if the qualified name of the supplied element is wrong
121
     */
122
    public static function fromXML(DOMElement $xml): static
123
    {
124
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
125
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
126
127
        return new static(
128
            AudienceRestrictionCondition::getChildrenOfClass($xml),
129
            DoNotCacheCondition::getChildrenOfClass($xml),
130
            AbstractCondition::getChildrenOfClass($xml),
131
            self::getOptionalAttribute($xml, 'NotBefore', SAMLDateTimeValue::class),
132
            self::getOptionalAttribute($xml, 'NotOnOrAfter', SAMLDateTimeValue::class),
133
        );
134
    }
135
136
137
    /**
138
     * Convert this ConditionsType to XML.
139
     *
140
     * @param \DOMElement $parent The element we are converting to XML.
141
     * @return \DOMElement The XML element after adding the data corresponding to this ConditionsType.
142
     */
143
    public function toXML(?DOMElement $parent = null): DOMElement
144
    {
145
        $e = $this->instantiateParentElement($parent);
146
147
        if ($this->getNotBefore() !== null) {
148
            $e->setAttribute('NotBefore', strval($this->getNotBefore()));
149
        }
150
151
        if ($this->getNotOnOrAfter() !== null) {
152
            $e->setAttribute('NotOnOrAfter', strval($this->getNotOnOrAfter()));
153
        }
154
155
        foreach ($this->getAudienceRestrictionCondition() as $audienceRestrictionCondition) {
156
            $audienceRestrictionCondition->toXML($e);
157
        }
158
159
        foreach ($this->getDoNotCacheCondition() as $doNotCacheCondition) {
160
            $doNotCacheCondition->toXML($e);
161
        }
162
163
        foreach ($this->getCondition() as $condition) {
164
            $condition->toXML($e);
165
        }
166
167
        return $e;
168
    }
169
}
170