AbstractConstrainedSingleValueType   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 20
c 1
b 0
f 0
dl 0
loc 94
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A getValue() 0 3 1
A toXML() 0 8 1
A getStructuredValue() 0 3 1
A isEmptyElement() 0 3 2
A fromXML() 0 14 1
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\Exception\InvalidDOMElementException;
10
use SimpleSAML\XML\Exception\TooManyElementsException;
11
12
use function array_pop;
13
14
/**
15
 * Class representing WS-authorization ConstrainedSingleValueType.
16
 *
17
 * @package simplesamlphp/ws-security
18
 */
19
abstract class AbstractConstrainedSingleValueType extends AbstractAuthElement
20
{
21
    /**
22
     * AbstractConstrainedSingleValueType constructor.
23
     *
24
     * @param \SimpleSAML\WSSecurity\XML\auth\Value $value
25
     * @param \SimpleSAML\WSSecurity\XML\auth\StructuredValue $structuredValue
26
     */
27
    final public function __construct(
28
        protected ?Value $value = null,
29
        protected ?StructuredValue $structuredValue = null,
30
    ) {
31
        Assert::oneOf(
32
            null,
33
            [$structuredValue, $value],
34
            'Can only have one of StructuredValue/Value',
35
        );
36
    }
37
38
39
    /**
40
     * Get the value of the $structuredValue property.
41
     *
42
     * @return \SimpleSAML\WSSecurity\XML\auth\StructuredValue|null
43
     */
44
    public function getStructuredValue(): ?StructuredValue
45
    {
46
        return $this->structuredValue;
47
    }
48
49
50
    /**
51
     * Get the value of the $value property.
52
     *
53
     * @return \SimpleSAML\WSSecurity\XML\auth\Value|null
54
     */
55
    public function getValue(): ?Value
56
    {
57
        return $this->value;
58
    }
59
60
61
    /**
62
     * Test if an object, at the state it's in, would produce an empty XML-element
63
     *
64
     * @return bool
65
     */
66
    public function isEmptyElement(): bool
67
    {
68
        return empty($this->value) && empty($this->structuredValue);
69
    }
70
71
72
    /**
73
     * Convert XML into a class instance
74
     *
75
     * @param \DOMElement $xml The XML element we should load
76
     * @return static
77
     *
78
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
79
     *   If the qualified name of the supplied element is wrong
80
     */
81
    public static function fromXML(DOMElement $xml): static
82
    {
83
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
84
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
85
86
        $structuredValue = StructuredValue::getChildrenOfClass($xml);
87
        Assert::maxCount($structuredValue, 1, TooManyElementsException::class);
88
89
        $value = Value::getChildrenOfClass($xml);
90
        Assert::maxCount($value, 1, TooManyElementsException::class);
91
92
        return new static(
93
            array_pop($value),
94
            array_pop($structuredValue),
95
        );
96
    }
97
98
99
    /**
100
     * Convert this element to XML.
101
     *
102
     * @param \DOMElement|null $parent The element we should append this element to.
103
     * @return \DOMElement
104
     */
105
    public function toXML(?DOMElement $parent = null): DOMElement
106
    {
107
        $e = $this->instantiateParentElement($parent);
108
109
        $this->getStructuredValue()?->toXML($e);
110
        $this->getValue()?->toXML($e);
111
112
        return $e;
113
    }
114
}
115