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

AbstractAnnotated   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 70
rs 10
c 0
b 0
f 0
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getAnnotation() 0 3 1
A getId() 0 3 1
A toXML() 0 11 2
A isEmptyElement() 0 5 3
A __construct() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSchema\XML;
6
7
use DOMElement;
8
use SimpleSAML\XMLSchema\Type\IDValue;
9
10
use function strval;
11
12
/**
13
 * Abstract class representing the annotated-type.
14
 *
15
 * @package simplesamlphp/xml-common
16
 */
17
abstract class AbstractAnnotated extends AbstractOpenAttrs
18
{
19
    /**
20
     * Annotated constructor
21
     *
22
     * @param \SimpleSAML\XMLSchema\XML\Annotation|null $annotation
23
     * @param \SimpleSAML\XMLSchema\Type\IDValue|null $id
24
     * @param array<\SimpleSAML\XML\Attribute> $namespacedAttributes
25
     */
26
    public function __construct(
27
        protected ?Annotation $annotation = null,
28
        protected ?IDValue $id = null,
29
        array $namespacedAttributes = [],
30
    ) {
31
        parent::__construct($namespacedAttributes);
32
    }
33
34
35
    /**
36
     * Collect the value of the annotation-property
37
     *
38
     * @return \SimpleSAML\XMLSchema\XML\Annotation|null
39
     */
40
    public function getAnnotation(): ?Annotation
41
    {
42
        return $this->annotation;
43
    }
44
45
46
    /**
47
     * Collect the value of the id-property
48
     *
49
     * @return \SimpleSAML\XMLSchema\Type\IDValue|null
50
     */
51
    public function getId(): ?IDValue
52
    {
53
        return $this->id;
54
    }
55
56
57
    /**
58
     * Test if an object, at the state it's in, would produce an empty XML-element
59
     *
60
     * @return bool
61
     */
62
    public function isEmptyElement(): bool
63
    {
64
        return parent::isEmptyElement() &&
65
            empty($this->getAnnotation()) &&
66
            empty($this->getId());
67
    }
68
69
70
    /**
71
     * Add this Annotated to an XML element.
72
     *
73
     * @param \DOMElement|null $parent The element we should append this Annotated to.
74
     * @return \DOMElement
75
     */
76
    public function toXML(?DOMElement $parent = null): DOMElement
77
    {
78
        $e = parent::toXML($parent);
79
80
        if ($this->getId() !== null) {
81
            $e->setAttribute('id', strval($this->getId()));
82
        }
83
84
        $this->getAnnotation()?->toXML($e);
85
86
        return $e;
87
    }
88
}
89