Restriction   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 138
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 58
dl 0
loc 138
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 21 3
A getBase() 0 3 1
A toXML() 0 16 3
A fromXML() 0 47 1
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\XML\SchemaValidatableElementInterface;
10
use SimpleSAML\XML\SchemaValidatableElementTrait;
11
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
12
use SimpleSAML\XMLSchema\Exception\TooManyElementsException;
13
use SimpleSAML\XMLSchema\Type\IDValue;
14
use SimpleSAML\XMLSchema\Type\QNameValue;
15
use SimpleSAML\XMLSchema\XML\Interface\SimpleDerivationInterface;
16
use SimpleSAML\XMLSchema\XML\Trait\SimpleRestrictionModelTrait;
17
18
use function array_merge;
19
use function is_null;
20
use function strval;
21
22
/**
23
 * Class representing the restriction-element.
24
 *
25
 * @package simplesamlphp/xml-common
26
 */
27
final class Restriction extends AbstractAnnotated implements
28
    SchemaValidatableElementInterface,
29
    SimpleDerivationInterface
30
{
31
    use SchemaValidatableElementTrait;
32
    use SimpleRestrictionModelTrait;
33
34
35
    /** @var string */
36
    public const LOCALNAME = 'restriction';
37
38
39
    /**
40
     * Notation constructor
41
     *
42
     * @param \SimpleSAML\XMLSchema\XML\LocalSimpleType|null $simpleType
43
     * @param \SimpleSAML\XMLSchema\XML\Interface\FacetInterface[] $facets
44
     * @param \SimpleSAML\XMLSchema\Type\QNameValue|null $base
45
     * @param \SimpleSAML\XMLSchema\XML\Annotation|null $annotation
46
     * @param \SimpleSAML\XMLSchema\Type\IDValue|null $id
47
     * @param array<\SimpleSAML\XML\Attribute> $namespacedAttributes
48
     */
49
    public function __construct(
50
        ?LocalSimpleType $simpleType = null,
51
        array $facets = [],
52
        protected ?QNameValue $base = null,
53
        ?Annotation $annotation = null,
54
        ?IDValue $id = null,
55
        array $namespacedAttributes = [],
56
    ) {
57
        parent::__construct($annotation, $id, $namespacedAttributes);
58
59
        Assert::false(
60
            is_null($base) && is_null($simpleType),
61
            "Either a 'base' attribute must be set, or an <xs:simpleType>",
62
        );
63
        Assert::false(
64
            !is_null($base) && !is_null($simpleType),
65
            "Either a 'base' attribute must be set, or an <xs:simpleType>, not both",
66
        );
67
68
        $this->setSimpleType($simpleType);
69
        $this->setFacets($facets);
70
    }
71
72
73
    /**
74
     * Collect the value of the base-property
75
     *
76
     * @return \SimpleSAML\XMLSchema\Type\QNameValue|null
77
     */
78
    public function getBase(): ?QNameValue
79
    {
80
        return $this->base;
81
    }
82
83
84
    /**
85
     * Add this Restriction to an XML element.
86
     *
87
     * @param \DOMElement|null $parent The element we should append this restriction to.
88
     * @return \DOMElement
89
     */
90
    public function toXML(?DOMElement $parent = null): DOMElement
91
    {
92
        $e = parent::toXML($parent);
93
94
        if ($this->getBase() !== null) {
95
            $e->setAttribute('base', strval($this->getBase()));
96
        }
97
98
        $this->getSimpleType()?->toXML($e);
99
100
        foreach ($this->getFacets() as $facet) {
101
            /** @var \SimpleSAML\XML\SerializableElementInterface $facet */
102
            $facet->toXML($e);
103
        }
104
105
        return $e;
106
    }
107
108
109
    /**
110
     * Create an instance of this object from its XML representation.
111
     *
112
     * @param \DOMElement $xml
113
     * @return static
114
     *
115
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
116
     *   if the qualified name of the supplied element is wrong
117
     */
118
    public static function fromXML(DOMElement $xml): static
119
    {
120
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
121
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
122
123
        $annotation = Annotation::getChildrenOfClass($xml);
124
        Assert::maxCount($annotation, 1, TooManyElementsException::class);
125
126
        $simpleType = LocalSimpleType::getChildrenOfClass($xml);
127
        Assert::maxCount($simpleType, 1, TooManyElementsException::class);
128
129
        // Facets
130
        $maxExclusive = MaxExclusive::getChildrenOfClass($xml);
131
        $minExclusive = MinExclusive::getChildrenOfClass($xml);
132
        $maxInclusive = MaxInclusive::getChildrenOfClass($xml);
133
        $minInclusive = MinInclusive::getChildrenOfClass($xml);
134
        $minLength = MinLength::getChildrenOfClass($xml);
135
        $maxLength = MaxLength::getChildrenOfClass($xml);
136
        $length = Length::getChildrenOfClass($xml);
137
        $enumeration = Enumeration::getChildrenOfClass($xml);
138
        $whiteSpace = WhiteSpace::getChildrenOfClass($xml);
139
        $pattern = Pattern::getChildrenOfClass($xml);
140
        $fractionDigits = FractionDigits::getChildrenOfClass($xml);
141
        $totalDigits = TotalDigits::getChildrenOfClass($xml);
142
143
        $facets = array_merge(
144
            $maxExclusive,
145
            $minExclusive,
146
            $maxInclusive,
147
            $minInclusive,
148
            $minLength,
149
            $maxLength,
150
            $length,
151
            $enumeration,
152
            $whiteSpace,
153
            $pattern,
154
            $fractionDigits,
155
            $totalDigits,
156
        );
157
158
        return new static(
159
            array_pop($simpleType),
160
            $facets,
161
            self::getOptionalAttribute($xml, 'base', QNameValue::class),
162
            array_pop($annotation),
163
            self::getOptionalAttribute($xml, 'id', IDValue::class, null),
164
            self::getAttributesNSFromXML($xml),
165
        );
166
    }
167
}
168