Issues (341)

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