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

Annotation::getId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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