Passed
Push — master ( 8413b2...00f101 )
by Tim
02:19
created

Any::fromXML()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

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