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

Notation::getPublic()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
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, SchemaValidatableElementTrait};
10
use SimpleSAML\XMLSchema\Exception\{InvalidDOMElementException, TooManyElementsException};
11
use SimpleSAML\XMLSchema\Type\{AnyURIValue, IDValue, NCNameValue};
12
use SimpleSAML\XMLSchema\Type\Schema\PublicValue;
13
use SimpleSAML\XMLSchema\XML\Interface\SchemaTopInterface;
14
15
use function strval;
16
17
/**
18
 * Class representing the notation-element.
19
 *
20
 * @package simplesamlphp/xml-common
21
 */
22
final class Notation extends AbstractAnnotated implements
23
    SchemaTopInterface,
24
    SchemaValidatableElementInterface
25
{
26
    use SchemaValidatableElementTrait;
1 ignored issue
show
introduced by
The trait SimpleSAML\XML\SchemaValidatableElementTrait requires some properties which are not provided by SimpleSAML\XMLSchema\XML\Notation: $message, $line
Loading history...
27
28
    /** @var string */
29
    public const LOCALNAME = 'notation';
30
31
32
    /**
33
     * Notation constructor
34
     *
35
     * @param \SimpleSAML\XMLSchema\Type\NCNameValue $name
36
     * @param \SimpleSAML\XMLSchema\Type\Schema\PublicValue|null $public
37
     * @param \SimpleSAML\XMLSchema\Type\AnyURIValue|null $system
38
     * @param \SimpleSAML\XMLSchema\XML\Annotation|null $annotation
39
     * @param \SimpleSAML\XMLSchema\Type\IDValue|null $id
40
     * @param array<\SimpleSAML\XML\Attribute> $namespacedAttributes
41
     */
42
    public function __construct(
43
        protected NCNameValue $name,
44
        protected ?PublicValue $public = null,
45
        protected ?AnyURIValue $system = null,
46
        ?Annotation $annotation = null,
47
        ?IDValue $id = null,
48
        array $namespacedAttributes = [],
49
    ) {
50
        parent::__construct($annotation, $id, $namespacedAttributes);
51
    }
52
53
54
    /**
55
     * Collect the value of the name-property
56
     *
57
     * @return \SimpleSAML\XMLSchema\Type\NCNameValue
58
     */
59
    public function getName(): NCNameValue
60
    {
61
        return $this->name;
62
    }
63
64
65
    /**
66
     * Collect the value of the public-property
67
     *
68
     * @return \SimpleSAML\XMLSchema\Type\Schema\PublicValue|null
69
     */
70
    public function getPublic(): ?PublicValue
71
    {
72
        return $this->public;
73
    }
74
75
76
    /**
77
     * Collect the value of the system-property
78
     *
79
     * @return \SimpleSAML\XMLSchema\Type\AnyURIValue|null
80
     */
81
    public function getSystem(): ?AnyURIValue
82
    {
83
        return $this->system;
84
    }
85
86
87
    /**
88
     * Add this Notation to an XML element.
89
     *
90
     * @param \DOMElement|null $parent The element we should append this notation to.
91
     * @return \DOMElement
92
     */
93
    public function toXML(?DOMElement $parent = null): DOMElement
94
    {
95
        $e = parent::toXML($parent);
96
97
        $e->setAttribute('name', strval($this->getName()));
98
99
        if ($this->getPublic() !== null) {
100
            $e->setAttribute('public', strval($this->getPublic()));
101
        }
102
103
        if ($this->getSystem() !== null) {
104
            $e->setAttribute('system', strval($this->getSystem()));
105
        }
106
107
        return $e;
108
    }
109
110
111
    /**
112
     * Create an instance of this object from its XML representation.
113
     *
114
     * @param \DOMElement $xml
115
     * @return static
116
     *
117
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
118
     *   if the qualified name of the supplied element is wrong
119
     */
120
    public static function fromXML(DOMElement $xml): static
121
    {
122
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
123
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
124
125
        $annotation = Annotation::getChildrenOfClass($xml);
126
        Assert::maxCount($annotation, 1, TooManyElementsException::class);
127
128
        return new static(
129
            self::getAttribute($xml, 'name', NCNameValue::class),
130
            self::getOptionalAttribute($xml, 'public', PublicValue::class, null),
131
            self::getOptionalAttribute($xml, 'system', AnyURIValue::class, null),
132
            array_pop($annotation),
133
            self::getOptionalAttribute($xml, 'id', IDValue::class, null),
134
            self::getAttributesNSFromXML($xml),
135
        );
136
    }
137
}
138