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

Restriction::fromXML()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 47
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

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