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