Issues (341)

src/XMLSchema/XML/Any.php (1 issue)

Labels
Severity
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
    public const string LOCALNAME = 'any';
0 ignored issues
show
A parse error occurred: Syntax error, unexpected T_STRING, expecting '=' on line 37 at column 24
Loading history...
38
39
40
    /**
41
     * Wildcard constructor
42
     *
43
     * @param \SimpleSAML\XMLSchema\Type\Schema\NamespaceListValue|null $namespace
44
     * @param \SimpleSAML\XMLSchema\Type\Schema\ProcessContentsValue|null $processContents
45
     * @param \SimpleSAML\XMLSchema\XML\Annotation|null $annotation
46
     * @param \SimpleSAML\XMLSchema\Type\IDValue|null $id
47
     * @param array<\SimpleSAML\XML\Attribute> $namespacedAttributes
48
     * @param \SimpleSAML\XMLSchema\Type\Schema\MinOccursValue|null $minOccurs
49
     * @param \SimpleSAML\XMLSchema\Type\Schema\MaxOccursValue|null $maxOccurs
50
     */
51
    public function __construct(
52
        protected ?NamespaceListValue $namespace = null,
53
        protected ?ProcessContentsValue $processContents = null,
54
        ?Annotation $annotation = null,
55
        ?IDValue $id = null,
56
        array $namespacedAttributes = [],
57
        ?MinOccursValue $minOccurs = null,
58
        ?MaxOccursValue $maxOccurs = null,
59
    ) {
60
        parent::__construct($namespace, $processContents, $annotation, $id, $namespacedAttributes);
61
62
        $this->setMinOccurs($minOccurs);
63
        $this->setMaxOccurs($maxOccurs);
64
    }
65
66
67
    /**
68
     * Test if an object, at the state it's in, would produce an empty XML-element
69
     *
70
     * @return bool
71
     */
72
    public function isEmptyElement(): bool
73
    {
74
        return parent::isEmptyElement() &&
75
            empty($this->getMinOccurs()) &&
76
            empty($this->getMaxOccurs());
77
    }
78
79
80
    /**
81
     * Create an instance of this object from its XML representation.
82
     *
83
     * @param \DOMElement $xml
84
     * @return static
85
     *
86
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
87
     *   if the qualified name of the supplied element is wrong
88
     */
89
    public static function fromXML(DOMElement $xml): static
90
    {
91
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
92
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
93
94
        $annotation = Annotation::getChildrenOfClass($xml);
95
        Assert::maxCount($annotation, 1, TooManyElementsException::class);
96
97
        return new static(
98
            self::getOptionalAttribute($xml, 'namespace', NamespaceListValue::class, null),
99
            self::getOptionalAttribute($xml, 'processContents', ProcessContentsValue::class, null),
100
            array_pop($annotation),
101
            self::getOptionalAttribute($xml, 'id', IDValue::class, null),
102
            self::getAttributesNSFromXML($xml),
103
            self::getOptionalAttribute($xml, 'minOccurs', MinOccursValue::class, null),
104
            self::getOptionalAttribute($xml, 'maxOccurs', MaxOccursvalue::class, null),
105
        );
106
    }
107
108
109
    /**
110
     * Add this Wildcard to an XML element.
111
     *
112
     * @param \DOMElement|null $parent The element we should append this Wildcard to.
113
     * @return \DOMElement
114
     */
115
    public function toXML(?DOMElement $parent = null): DOMElement
116
    {
117
        $e = parent::toXML($parent);
118
119
        if ($this->getMinOccurs() !== null) {
120
            $e->setAttribute('minOccurs', strval($this->getMinOccurs()));
121
        }
122
123
        if ($this->getMaxOccurs() !== null) {
124
            $e->setAttribute('maxOccurs', strval($this->getMaxOccurs()));
125
        }
126
127
        return $e;
128
    }
129
}
130