AbstractRestrictionType::toXML()   B
last analyzed

Complexity

Conditions 7
Paths 12

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 13
nc 12
nop 1
dl 0
loc 25
rs 8.8333
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\SchemaViolationException;
10
use SimpleSAML\XMLSchema\Type\{IDValue, QNameValue};
11
use SimpleSAML\XMLSchema\XML\Interface\TypeDefParticleInterface;
12
use SimpleSAML\XMLSchema\XML\Trait\{AttrDeclsTrait, SimpleRestrictionModelTrait, TypeDefParticleTrait};
13
14
use function strval;
15
16
/**
17
 * Abstract class representing the restrictionType-type.
18
 *
19
 * @package simplesamlphp/xml-common
20
 */
21
abstract class AbstractRestrictionType extends AbstractAnnotated
22
{
23
    use AttrDeclsTrait;
24
    use SimpleRestrictionModelTrait;
25
    use TypeDefParticleTrait;
26
27
    /**
28
     * AbstractRestrictionType constructor
29
     *
30
     * @param \SimpleSAML\XMLSchema\Type\QNameValue $base
31
     * @param \SimpleSAML\XMLSchema\XML\Interface\TypeDefParticleInterface|null $particle
32
     * @param \SimpleSAML\XMLSchema\XML\LocalSimpleType|null $localSimpleType
33
     * @param array<\SimpleSAML\XMLSchema\XML\Interface\FacetInterface> $facets
34
     * @param (
35
     *     \SimpleSAML\XMLSchema\XML\LocalAttribute|
36
     *     \SimpleSAML\XMLSchema\XML\ReferencedAttributeGroup
37
     * )[] $attributes
38
     * @param \SimpleSAML\XMLSchema\XML\AnyAttribute|null $anyAttribute
39
     * @param \SimpleSAML\XMLSchema\XML\Annotation|null $annotation
40
     * @param \SimpleSAML\XMLSchema\Type\IDValue|null $id
41
     * @param array<\SimpleSAML\XML\Attribute> $namespacedAttributes
42
     */
43
    public function __construct(
44
        protected QNameValue $base,
45
        // xs:typeDefParticle
46
        ?TypeDefParticleInterface $particle = null,
47
        // xs:simpleRestrictionModel
48
        protected ?LocalSimpleType $localSimpleType = null,
49
        array $facets = [],
50
        // xs:attrDecls
51
        array $attributes = [],
52
        ?AnyAttribute $anyAttribute = null,
53
        // parent defined
54
        ?Annotation $annotation = null,
55
        ?IDValue $id = null,
56
        array $namespacedAttributes = [],
57
    ) {
58
        // The xs:typeDefParticle and xs:simpleRestrictionModel groups are mutually exclusive
59
        if ($particle !== null) {
60
            Assert::null($localSimpleType, SchemaViolationException::class);
61
            Assert::isEmpty($facets, SchemaViolationException::class);
62
        } elseif ($localSimpleType !== null || $facets !== []) {
63
            $this->setSimpleType($localSimpleType);
64
            $this->setFacets($facets);
65
        }
66
67
        parent::__construct($annotation, $id, $namespacedAttributes);
68
69
        $this->setAttributes($attributes);
70
        $this->setAnyAttribute($anyAttribute);
71
        $this->setParticle($particle);
72
    }
73
74
75
    /**
76
     * Collect the value of the localSimpleType-property
77
     *
78
     * @return \SimpleSAML\XMLSchema\XML\LocalSimpleType|null
79
     */
80
    public function getLocalSimpleType(): ?LocalSimpleType
81
    {
82
        return $this->localSimpleType;
83
    }
84
85
86
    /**
87
     * Collect the value of the base-property
88
     *
89
     * @return \SimpleSAML\XMLSchema\Type\QNameValue
90
     */
91
    public function getBase(): ?QNameValue
92
    {
93
        return $this->base;
94
    }
95
96
97
    /**
98
     * Add this RestrictionType to an XML element.
99
     *
100
     * @param \DOMElement|null $parent The element we should append this RestrictionType to.
101
     * @return \DOMElement
102
     */
103
    public function toXML(?DOMElement $parent = null): DOMElement
104
    {
105
        $e = parent::toXML($parent);
106
107
        if ($this->getParticle() !== null) {
108
            $this->getParticle()->toXML($e);
109
        } elseif ($this->getLocalSimpleType() !== null || $this->getFacets() !== []) {
110
            $this->getLocalSimpleType()?->toXML($e);
111
112
            foreach ($this->getFacets() as $facet) {
113
                $facet->toXML($e);
114
            }
115
        }
116
117
        foreach ($this->getAttributes() as $attr) {
118
            $attr->toXML($e);
119
        }
120
121
        $this->getAnyAttribute()?->toXML($e);
122
123
        if ($this->getBase() !== null) {
124
            $e->setAttribute('base', strval($this->getBase()));
125
        }
126
127
        return $e;
128
    }
129
}
130