Issues (341)

src/XMLSchema/XML/ComplexContent.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\BooleanValue;
14
use SimpleSAML\XMLSchema\Type\IDValue;
15
16
use function array_merge;
17
use function strval;
18
19
/**
20
 * Class representing the complexContent-type.
21
 *
22
 * @package simplesamlphp/xml-common
23
 */
24
final class ComplexContent extends AbstractAnnotated implements SchemaValidatableElementInterface
25
{
26
    use SchemaValidatableElementTrait;
27
28
29
    public const string LOCALNAME = 'complexContent';
0 ignored issues
show
A parse error occurred: Syntax error, unexpected T_STRING, expecting '=' on line 29 at column 24
Loading history...
30
31
32
    /**
33
     * ComplexContent constructor
34
     *
35
     * @param \SimpleSAML\XMLSchema\XML\ComplexRestriction|\SimpleSAML\XMLSchema\XML\Extension $content
36
     * @param \SimpleSAML\XMLSchema\Type\BooleanValue|null $mixed
37
     * @param \SimpleSAML\XMLSchema\XML\Annotation|null $annotation
38
     * @param \SimpleSAML\XMLSchema\Type\IDValue|null $id
39
     * @param array<\SimpleSAML\XML\Attribute> $namespacedAttributes
40
     */
41
    public function __construct(
42
        protected ComplexRestriction|Extension $content,
43
        protected ?BooleanValue $mixed = null,
44
        ?Annotation $annotation = null,
45
        ?IDValue $id = null,
46
        array $namespacedAttributes = [],
47
    ) {
48
        parent::__construct($annotation, $id, $namespacedAttributes);
49
    }
50
51
52
    /**
53
     * Collect the value of the content-property
54
     *
55
     * @return \SimpleSAML\XMLSchema\XML\ComplexRestriction|\SimpleSAML\XMLSchema\XML\Extension
56
     */
57
    public function getContent(): ComplexRestriction|Extension
58
    {
59
        return $this->content;
60
    }
61
62
63
    /**
64
     * Collect the value of the mixed-property
65
     *
66
     * @return \SimpleSAML\XMLSchema\Type\BooleanValue|null
67
     */
68
    public function getMixed(): ?BooleanValue
69
    {
70
        return $this->mixed;
71
    }
72
73
74
    /**
75
     * Test if an object, at the state it's in, would produce an empty XML-element
76
     *
77
     * @return bool
78
     */
79
    public function isEmptyElement(): bool
80
    {
81
        return false;
82
    }
83
84
85
    /**
86
     * Add this ComplexContent to an XML element.
87
     *
88
     * @param \DOMElement|null $parent The element we should append this ComplexContent to.
89
     * @return \DOMElement
90
     */
91
    public function toXML(?DOMElement $parent = null): DOMElement
92
    {
93
        $e = parent::toXML($parent);
94
95
        if ($this->getMixed() !== null) {
96
            $e->setAttribute('mixed', strval($this->getMixed()));
97
        }
98
99
        $this->getContent()->toXML($e);
100
101
        return $e;
102
    }
103
104
105
    /**
106
     * Create an instance of this object from its XML representation.
107
     *
108
     * @param \DOMElement $xml
109
     * @return static
110
     *
111
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
112
     *   if the qualified name of the supplied element is wrong
113
     */
114
    public static function fromXML(DOMElement $xml): static
115
    {
116
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
117
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
118
119
        $annotation = Annotation::getChildrenOfClass($xml);
120
        Assert::maxCount($annotation, 1, TooManyElementsException::class);
121
122
        $complexRestriction = ComplexRestriction::getChildrenOfClass($xml);
123
        Assert::maxCount($complexRestriction, 1, TooManyElementsException::class);
124
125
        $extension = Extension::getChildrenOfClass($xml);
126
        Assert::maxCount($extension, 1, TooManyElementsException::class);
127
128
        $content = array_merge($complexRestriction, $extension);
129
        Assert::maxCount($content, 1, TooManyElementsException::class);
130
131
        return new static(
132
            $content[0],
133
            self::getOptionalAttribute($xml, 'mixed', BooleanValue::class, null),
134
            array_pop($annotation),
135
            self::getOptionalAttribute($xml, 'id', IDValue::class, null),
136
            self::getAttributesNSFromXML($xml),
137
        );
138
    }
139
}
140