Any::toXML()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 4
nop 1
dl 0
loc 13
rs 10
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\Schema\MaxOccursValue;
15
use SimpleSAML\XMLSchema\Type\Schema\MinOccursValue;
16
use SimpleSAML\XMLSchema\Type\Schema\NamespaceListValue;
17
use SimpleSAML\XMLSchema\Type\Schema\ProcessContentsValue;
18
use SimpleSAML\XMLSchema\XML\Interface\NestedParticleInterface;
19
use SimpleSAML\XMLSchema\XML\Trait\OccursTrait;
20
21
use function array_pop;
22
use function strval;
23
24
/**
25
 * Class representing the Any element
26
 *
27
 * @package simplesamlphp/xml-common
28
 */
29
final class Any extends AbstractWildcard implements
30
    NestedParticleInterface,
31
    SchemaValidatableElementInterface
32
{
33
    use OccursTrait;
34
    use SchemaValidatableElementTrait;
35
36
37
    /** @var string */
38
    public const LOCALNAME = 'any';
39
40
41
    /**
42
     * Wildcard constructor
43
     *
44
     * @param \SimpleSAML\XMLSchema\Type\Schema\NamespaceListValue|null $namespace
45
     * @param \SimpleSAML\XMLSchema\Type\Schema\ProcessContentsValue|null $processContents
46
     * @param \SimpleSAML\XMLSchema\XML\Annotation|null $annotation
47
     * @param \SimpleSAML\XMLSchema\Type\IDValue|null $id
48
     * @param array<\SimpleSAML\XML\Attribute> $namespacedAttributes
49
     * @param \SimpleSAML\XMLSchema\Type\Schema\MinOccursValue|null $minOccurs
50
     * @param \SimpleSAML\XMLSchema\Type\Schema\MaxOccursValue|null $maxOccurs
51
     */
52
    public function __construct(
53
        protected ?NamespaceListValue $namespace = null,
54
        protected ?ProcessContentsValue $processContents = null,
55
        ?Annotation $annotation = null,
56
        ?IDValue $id = null,
57
        array $namespacedAttributes = [],
58
        ?MinOccursValue $minOccurs = null,
59
        ?MaxOccursValue $maxOccurs = null,
60
    ) {
61
        parent::__construct($namespace, $processContents, $annotation, $id, $namespacedAttributes);
62
63
        $this->setMinOccurs($minOccurs);
64
        $this->setMaxOccurs($maxOccurs);
65
    }
66
67
68
    /**
69
     * Test if an object, at the state it's in, would produce an empty XML-element
70
     *
71
     * @return bool
72
     */
73
    public function isEmptyElement(): bool
74
    {
75
        return parent::isEmptyElement() &&
76
            empty($this->getMinOccurs()) &&
77
            empty($this->getMaxOccurs());
78
    }
79
80
81
    /**
82
     * Create an instance of this object from its XML representation.
83
     *
84
     * @param \DOMElement $xml
85
     * @return static
86
     *
87
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
88
     *   if the qualified name of the supplied element is wrong
89
     */
90
    public static function fromXML(DOMElement $xml): static
91
    {
92
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
93
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
94
95
        $annotation = Annotation::getChildrenOfClass($xml);
96
        Assert::maxCount($annotation, 1, TooManyElementsException::class);
97
98
        return new static(
99
            self::getOptionalAttribute($xml, 'namespace', NamespaceListValue::class, null),
100
            self::getOptionalAttribute($xml, 'processContents', ProcessContentsValue::class, null),
101
            array_pop($annotation),
102
            self::getOptionalAttribute($xml, 'id', IDValue::class, null),
103
            self::getAttributesNSFromXML($xml),
104
            self::getOptionalAttribute($xml, 'minOccurs', MinOccursValue::class, null),
105
            self::getOptionalAttribute($xml, 'maxOccurs', MaxOccursvalue::class, null),
106
        );
107
    }
108
109
110
    /**
111
     * Add this Wildcard to an XML element.
112
     *
113
     * @param \DOMElement|null $parent The element we should append this Wildcard to.
114
     * @return \DOMElement
115
     */
116
    public function toXML(?DOMElement $parent = null): DOMElement
117
    {
118
        $e = parent::toXML($parent);
119
120
        if ($this->getMinOccurs() !== null) {
121
            $e->setAttribute('minOccurs', strval($this->getMinOccurs()));
122
        }
123
124
        if ($this->getMaxOccurs() !== null) {
125
            $e->setAttribute('maxOccurs', strval($this->getMaxOccurs()));
126
        }
127
128
        return $e;
129
    }
130
}
131