Completed
Push — master ( f72cfb...4ef12f )
by Tim
21s queued 18s
created

AbstractRestrictionType   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 28
dl 0
loc 107
rs 10
c 1
b 0
f 0
wmc 13

4 Methods

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