Passed
Branch feature/php8.3 (4d3b0a)
by Tim
17:15
created

Restriction::fromXML()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 47
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

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