AbstractContextItemType::getName()   A
last analyzed

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
eloc 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\WSSecurity\XML\auth;
6
7
use DOMElement;
8
use SimpleSAML\WSSecurity\Assert\Assert;
9
use SimpleSAML\XML\ExtendableAttributesTrait;
10
use SimpleSAML\XML\ExtendableElementTrait;
11
use SimpleSAML\XML\SerializableElementInterface;
12
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
13
use SimpleSAML\XMLSchema\Exception\SchemaViolationException;
14
use SimpleSAML\XMLSchema\Exception\TooManyElementsException;
15
use SimpleSAML\XMLSchema\Type\AnyURIValue;
16
use SimpleSAML\XMLSchema\XML\Constants\NS;
17
18
use function array_pop;
19
20
/**
21
 * Class defining the ContextItemType element
22
 *
23
 * @package simplesamlphp/ws-security
24
 */
25
abstract class AbstractContextItemType extends AbstractAuthElement
26
{
27
    use ExtendableAttributesTrait;
28
    use ExtendableElementTrait;
0 ignored issues
show
introduced by
The trait SimpleSAML\XML\ExtendableElementTrait requires some properties which are not provided by SimpleSAML\WSSecurity\XM...AbstractContextItemType: $namespaceURI, $localName, $childNodes
Loading history...
29
30
31
    /** The namespace-attribute for the xs:anyAttribute */
32
    public const XS_ANY_ATTR_NAMESPACE = NS::OTHER;
33
34
    /** The namespace-attribute for the xs:any */
35
    public const XS_ANY_ELT_NAMESPACE = NS::OTHER;
36
37
38
    /**
39
     * AbstractContextItemType constructor
40
     *
41
     * @param \SimpleSAML\XMLSchema\Type\AnyURIValue $Name
42
     * @param \SimpleSAML\XMLSchema\Type\AnyURIValue|null $Scope
43
     * @param \SimpleSAML\WSSecurity\XML\auth\Value|null $value
44
     * @param \SimpleSAML\XML\SerializableElementInterface|null $child
45
     * @param list<\SimpleSAML\XML\Attribute> $namespacedAttributes
0 ignored issues
show
Bug introduced by
The type SimpleSAML\WSSecurity\XML\auth\list was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
46
     */
47
    final public function __construct(
48
        protected AnyURIValue $Name,
49
        protected ?AnyURIValue $Scope = null,
50
        protected ?Value $value = null,
51
        ?SerializableElementInterface $child = null,
52
        array $namespacedAttributes = [],
53
    ) {
54
        // One of both must exist, they can't be both null
55
        Assert::inArray(null, [$value, $child], SchemaViolationException::class);
56
        Assert::notSame($value, $child, SchemaViolationException::class);
57
58
        if ($child !== null) {
59
            $this->setElements([$child]);
60
        }
61
        $this->setAttributesNS($namespacedAttributes);
62
    }
63
64
65
    /**
66
     * Get the value of the $value property.
67
     *
68
     * @return \SimpleSAML\WSSecurity\XML\auth\Value
69
     */
70
    public function getValue(): ?Value
71
    {
72
        return $this->value;
73
    }
74
75
76
    /**
77
     * Get the value of the Name property.
78
     *
79
     * @return \SimpleSAML\XMLSchema\Type\AnyURIValue
80
     */
81
    public function getName(): AnyURIValue
82
    {
83
        return $this->Name;
84
    }
85
86
87
    /**
88
     * Get the value of the Scope property.
89
     *
90
     * @return \SimpleSAML\XMLSchema\Type\AnyURIValue|null
91
     */
92
    public function getScope(): ?AnyURIValue
93
    {
94
        return $this->Scope;
95
    }
96
97
98
    /**
99
     * Create an instance of this object from its XML representation.
100
     *
101
     * @param \DOMElement $xml
102
     * @return static
103
     *
104
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
105
     *   if the qualified name of the supplied element is wrong
106
     */
107
    public static function fromXML(DOMElement $xml): static
108
    {
109
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
110
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
111
112
        $value = Value::getChildrenOfClass($xml);
113
        Assert::maxCount($value, 1, TooManyElementsException::class);
114
115
        $children = self::getChildElementsFromXML($xml);
116
        Assert::maxCount($children, 1, TooManyElementsException::class);
117
118
        return new static(
119
            self::getAttribute($xml, 'Name', AnyURIValue::class),
120
            self::getOptionalAttribute($xml, 'Scope', AnyURIValue::class, null),
121
            array_pop($value),
122
            array_pop($children),
123
            self::getAttributesNSFromXML($xml),
124
        );
125
    }
126
127
128
    /**
129
     * Add this ContextItemType to an XML element.
130
     *
131
     * @param \DOMElement $parent The element we should append this username token to.
132
     * @return \DOMElement
133
     */
134
    public function toXML(?DOMElement $parent = null): DOMElement
135
    {
136
        $e = $this->instantiateParentElement($parent);
137
138
        $e->setAttribute('Name', $this->getName()->getValue());
139
        if ($this->getScope() !== null) {
140
            $e->setAttribute('Scope', $this->getScope()->getValue());
141
        }
142
143
        foreach ($this->getAttributesNS() as $attr) {
144
            $attr->toXML($e);
145
        }
146
147
        $this->getValue()?->toXML($e);
148
149
        foreach ($this->getElements() as $elt) {
150
            $elt->toXML($e);
151
        }
152
153
        return $e;
154
    }
155
}
156