Passed
Pull Request — master (#61)
by Tim
02:17
created

Any::isEmptyElement()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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