AbstractAttribute::toXML()   B
last analyzed

Complexity

Conditions 8
Paths 128

Size

Total Lines 35
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 17
nc 128
nop 1
dl 0
loc 35
rs 8.2111
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSchema\XML;
6
7
use DOMElement;
8
use SimpleSAML\XML\Assert\Assert;
9
use SimpleSAML\XMLSchema\Exception\ProtocolViolationException;
10
use SimpleSAML\XMLSchema\Type\IDValue;
11
use SimpleSAML\XMLSchema\Type\NCNameValue;
12
use SimpleSAML\XMLSchema\Type\QNameValue;
13
use SimpleSAML\XMLSchema\Type\Schema\FormChoiceValue;
14
use SimpleSAML\XMLSchema\Type\Schema\UseValue;
15
use SimpleSAML\XMLSchema\Type\StringValue;
16
use SimpleSAML\XMLSchema\XML\Trait\DefRefTrait;
17
use SimpleSAML\XMLSchema\XML\Trait\FormChoiceTrait;
18
19
use function strval;
20
21
/**
22
 * Abstract class representing the attribute-type.
23
 *
24
 * @package simplesamlphp/xml-common
25
 */
26
abstract class AbstractAttribute extends AbstractAnnotated
27
{
28
    use DefRefTrait;
29
    use FormChoiceTrait;
30
31
32
    /**
33
     * Attribute constructor
34
     *
35
     * @param \SimpleSAML\XMLSchema\Type\QNameValue|null $type
36
     * @param \SimpleSAML\XMLSchema\Type\NCNameValue|null $name
37
     * @param \SimpleSAML\XMLSchema\Type\QNameValue|null $reference
38
     * @param \SimpleSAML\XMLSchema\Type\Schema\UseValue|null $use
39
     * @param \SimpleSAML\XMLSchema\Type\StringValue|null $default
40
     * @param \SimpleSAML\XMLSchema\Type\StringValue|null $fixed
41
     * @param \SimpleSAML\XMLSchema\Type\Schema\FormChoiceValue|null $formChoice
42
     * @param \SimpleSAML\XMLSchema\XML\LocalSimpleType|null $simpleType
43
     * @param \SimpleSAML\XMLSchema\XML\Annotation|null $annotation
44
     * @param \SimpleSAML\XMLSchema\Type\IDValue|null $id
45
     * @param array<\SimpleSAML\XML\Attribute> $namespacedAttributes
46
     */
47
    public function __construct(
48
        protected ?QNameValue $type = null,
49
        ?NCNameValue $name = null,
50
        ?QNameValue $reference = null,
51
        protected ?UseValue $use = null,
52
        protected ?StringValue $default = null,
53
        protected ?StringValue $fixed = null,
54
        ?FormChoiceValue $formChoice = null,
55
        protected ?LocalSimpleType $simpleType = null,
56
        ?Annotation $annotation = null,
57
        ?IDValue $id = null,
58
        array $namespacedAttributes = [],
59
    ) {
60
        Assert::oneOf(null, [$type, $reference], ProtocolViolationException::class);
61
        Assert::oneOf(null, [$name, $reference], ProtocolViolationException::class);
62
        Assert::false(is_null($name) && is_null($reference), ProtocolViolationException::class);
63
64
        /**
65
         * default and fixed are mutually exclusive
66
         */
67
        Assert::oneOf(null, [$default, $fixed], ProtocolViolationException::class);
68
69
        // simpleType only if no type|ref attribute
70
        if ($simpleType !== null) {
71
            Assert::true(is_null($type) && is_null($reference), ProtocolViolationException::class);
72
        }
73
74
        parent::__construct($annotation, $id, $namespacedAttributes);
75
76
        $this->setName($name);
77
        $this->setReference($reference);
78
        $this->setFormChoice($formChoice);
79
    }
80
81
82
    /**
83
     * Collect the value of the simpleType-property
84
     *
85
     * @return \SimpleSAML\XMLSchema\XML\LocalSimpleType|null
86
     */
87
    public function getSimpleType(): ?LocalSimpleType
88
    {
89
        return $this->simpleType;
90
    }
91
92
93
    /**
94
     * Collect the value of the type-property
95
     *
96
     * @return \SimpleSAML\XMLSchema\Type\QNameValue|null
97
     */
98
    public function getType(): ?QNameValue
99
    {
100
        return $this->type;
101
    }
102
103
104
    /**
105
     * Collect the value of the use-property
106
     *
107
     * @return \SimpleSAML\XMLSchema\Type\Schema\UseValue|null
108
     */
109
    public function getUse(): ?UseValue
110
    {
111
        return $this->use;
112
    }
113
114
115
    /**
116
     * Collect the value of the default-property
117
     *
118
     * @return \SimpleSAML\XMLSchema\Type\StringValue|null
119
     */
120
    public function getDefault(): ?StringValue
121
    {
122
        return $this->default;
123
    }
124
125
126
    /**
127
     * Collect the value of the fixed-property
128
     *
129
     * @return \SimpleSAML\XMLSchema\Type\StringValue|null
130
     */
131
    public function getFixed(): ?StringValue
132
    {
133
        return $this->fixed;
134
    }
135
136
137
    /**
138
     * Add this Attribute to an XML element.
139
     *
140
     * @param \DOMElement|null $parent The element we should append this facet to.
141
     * @return \DOMElement
142
     */
143
    public function toXML(?DOMElement $parent = null): DOMElement
144
    {
145
        $e = parent::toXML($parent);
146
147
        if ($this->getName() !== null) {
148
            $e->setAttribute('name', strval($this->getName()));
149
        }
150
151
        if ($this->getReference() !== null) {
152
            $e->setAttribute('ref', strval($this->getReference()));
153
        }
154
155
        if ($this->getType() !== null) {
156
            $e->setAttribute('type', strval($this->getType()));
157
        }
158
159
        if ($this->getUse() !== null) {
160
            $e->setAttribute('use', strval($this->getUse()));
161
        }
162
163
        if ($this->getDefault() !== null) {
164
            $e->setAttribute('default', strval($this->getDefault()));
165
        }
166
167
        if ($this->getFixed() !== null) {
168
            $e->setAttribute('fixed', strval($this->getFixed()));
169
        }
170
171
        if ($this->getFormChoice() !== null) {
172
            $e->setAttribute('form', strval($this->getFormChoice()));
173
        }
174
175
        $this->getSimpleType()?->toXML($e);
176
177
        return $e;
178
    }
179
}
180