Annotation::isEmptyElement()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 4
nc 4
nop 0
dl 0
loc 6
rs 10
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;
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
    /** @var string */
28
    public const LOCALNAME = 'annotation';
29
30
31
    /**
32
     * Annotation constructor
33
     *
34
     * @param array<\SimpleSAML\XMLSchema\XML\Appinfo> $appinfo
35
     * @param array<\SimpleSAML\XMLSchema\XML\Documentation> $documentation
36
     * @param \SimpleSAML\XMLSchema\Type\IDValue|null $id
37
     * @param array<\SimpleSAML\XML\Attribute> $namespacedAttributes
38
     */
39
    public function __construct(
40
        protected array $appinfo,
41
        protected array $documentation,
42
        protected ?IDValue $id,
43
        array $namespacedAttributes = [],
44
    ) {
45
        Assert::allIsInstanceOf($appinfo, Appinfo::class, SchemaViolationException::class);
46
        Assert::allIsInstanceOf($documentation, Documentation::class, SchemaViolationException::class);
47
48
        parent::__construct($namespacedAttributes);
49
    }
50
51
52
    /**
53
     * Collect the value of the appinfo-property
54
     *
55
     * @return \SimpleSAML\XMLSchema\XML\Appinfo[]
56
     */
57
    public function getAppinfo(): array
58
    {
59
        return $this->appinfo;
60
    }
61
62
63
    /**
64
     * Collect the value of the documentation-property
65
     *
66
     * @return \SimpleSAML\XMLSchema\XML\Documentation[]
67
     */
68
    public function getDocumentation(): array
69
    {
70
        return $this->documentation;
71
    }
72
73
74
    /**
75
     * Collect the value of the id-property
76
     *
77
     * @return \SimpleSAML\XMLSchema\Type\IDValue|null
78
     */
79
    public function getId(): ?IDValue
80
    {
81
        return $this->id;
82
    }
83
84
85
    /**
86
     * Test if an object, at the state it's in, would produce an empty XML-element
87
     *
88
     * @return bool
89
     */
90
    public function isEmptyElement(): bool
91
    {
92
        return parent::isEmptyElement() &&
93
            empty($this->getAppinfo()) &&
94
            empty($this->getDocumentation()) &&
95
            empty($this->id);
96
    }
97
98
99
    /**
100
     * Create a class from XML
101
     *
102
     * @param \DOMElement $xml
103
     * @return static
104
     */
105
    public static function fromXML(DOMElement $xml): static
106
    {
107
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
108
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
109
110
        return new static(
111
            Appinfo::getChildrenOfClass($xml),
112
            Documentation::getChildrenOfClass($xml),
113
            self::getOptionalAttribute($xml, 'id', IDValue::class, null),
114
            self::getAttributesNSFromXML($xml),
115
        );
116
    }
117
118
119
    /**
120
     * Add this Annotation to an XML element.
121
     *
122
     * @param \DOMElement|null $parent The element we should append this Annotation to.
123
     * @return \DOMElement
124
     */
125
    public function toXML(?DOMElement $parent = null): DOMElement
126
    {
127
        $e = parent::toXML($parent);
128
129
        if ($this->getId() !== null) {
130
            $e->setAttribute('id', strval($this->getId()));
131
        }
132
133
        foreach ($this->getAppinfo() as $appinfo) {
134
            $appinfo->toXML($e);
135
        }
136
137
        foreach ($this->getDocumentation() as $documentation) {
138
            $documentation->toXML($e);
139
        }
140
141
        return $e;
142
    }
143
}
144