AbstractConstrainedValueType   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 34
dl 0
loc 124
rs 10
c 1
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A fromXML() 0 27 1
A getAssertConstraint() 0 3 1
A getValue() 0 3 1
A __construct() 0 6 1
A toXML() 0 18 5
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\MissingElementException;
11
use SimpleSAML\XML\Exception\TooManyElementsException;
12
use SimpleSAML\XML\ExtendableElementTrait;
13
use SimpleSAML\XML\XsNamespace as NS;
14
15
use function array_filter;
16
use function array_merge;
17
use function array_pop;
18
19
/**
20
 * Class defining the ConstrainedValueType element
21
 *
22
 * @package simplesamlphp/ws-security
23
 */
24
abstract class AbstractConstrainedValueType extends AbstractAuthElement
25
{
26
    use ExtendableElementTrait;
0 ignored issues
show
introduced by
The trait SimpleSAML\XML\ExtendableElementTrait requires some properties which are not provided by SimpleSAML\WSSecurity\XM...actConstrainedValueType: $namespaceURI, $localName, $childNodes
Loading history...
27
28
    /** The namespace-attribute for the xs:any element */
29
    public const XS_ANY_ELT_NAMESPACE = NS::OTHER;
30
31
32
    /**
33
     * AbstractConstrainedValueType constructor
34
     *
35
     * @param (
36
     *   \SimpleSAML\WSSecurity\XML\auth\ValueLessThan|
37
     *   \SimpleSAML\WSSecurity\XML\auth\ValueLessThanOrEqual|
38
     *   \SimpleSAML\WSSecurity\XML\auth\ValueGreaterThan|
39
     *   \SimpleSAML\WSSecurity\XML\auth\ValueGreaterThanOrEqual|
40
     *   \SimpleSAML\WSSecurity\XML\auth\ValueInRangen|
41
     *   \SimpleSAML\WSSecurity\XML\auth\ValueOneOf
42
     * ) $value
43
     * @param \SimpleSAML\XML\SerializableElementInterface[] $children
44
     * @param bool|null $assertConstraint
45
     */
46
    final public function __construct(
47
        protected ValueLessThan|ValueLessThanOrEqual|ValueGreaterThan|ValueGreaterThanOrEqual|ValueInRangen|ValueOneOf $value,
48
        array $children = [],
49
        protected ?bool $assertConstraint = null,
50
    ) {
51
        $this->setElements($children);
52
    }
53
54
55
    /**
56
     * Get the value of the $value property.
57
     *
58
     * @return (
0 ignored issues
show
Documentation Bug introduced by
The doc comment ( at position 1 could not be parsed: the token is null at position 1.
Loading history...
59
     *   \SimpleSAML\WSSecurity\XML\auth\ValueLessThan|
60
     *   \SimpleSAML\WSSecurity\XML\auth\ValueLessThanOrEqual|
61
     *   \SimpleSAML\WSSecurity\XML\auth\ValueGreaterThan|
62
     *   \SimpleSAML\WSSecurity\XML\auth\ValueGreaterThanOrEqual|
63
     *   \SimpleSAML\WSSecurity\XML\auth\ValueInRangen|
64
     *   \SimpleSAML\WSSecurity\XML\auth\ValueOneOf
65
     * )
66
     */
67
    public function getValue(): ValueLessThan|ValueLessThanOrEqual|ValueGreaterThan|ValueGreaterThanOrEqual|ValueInRangen|ValueOneOf
68
    {
69
        return $this->value;
70
    }
71
72
73
    /**
74
     * Get the value of the assertConstraint property.
75
     *
76
     * @return bool|null
77
     */
78
    public function getAssertConstraint(): ?bool
79
    {
80
        return $this->assertConstraint;
81
    }
82
83
84
    /**
85
     * Create an instance of this object from its XML representation.
86
     *
87
     * @param \DOMElement $xml
88
     * @return static
89
     *
90
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
91
     *   if the qualified name of the supplied element is wrong
92
     */
93
    public static function fromXML(DOMElement $xml): static
94
    {
95
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
96
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
97
98
        $valueLessThan = ValueLessThan::getChildrenOfClass($xml);
99
        $valueLessThanOrEqual = ValueLessThanOrEqual::getChildrenOfClass($xml);
100
        $valueGreaterThan = ValueGreaterThan::getChildrenOfClass($xml);
101
        $valueGreaterThanOrEqual = ValueGreaterThanOrEqual::getChildrenOfClass($xml);
102
        $valueInRangen = ValueInRangen::getChildrenOfClass($xml);
103
        $valueOneOf = ValueOneOf::getChildrenOfClass($xml);
104
105
        $value = array_filter(array_merge(
106
            $valueLessThan,
107
            $valueLessThanOrEqual,
108
            $valueGreaterThan,
109
            $valueGreaterThanOrEqual,
110
            $valueInRangen,
111
            $valueOneOf,
112
        ));
113
        Assert::minCount($value, 1, MissingElementException::class);
114
        Assert::maxCount($value, 1, TooManyElementsException::class);
115
116
        return new static(
117
            array_pop($value),
118
            self::getChildElementsFromXML($xml),
119
            self::getOptionalBooleanAttribute($xml, 'AssertConstraint', null),
120
        );
121
    }
122
123
124
    /**
125
     * Add this StructuredValueType to an XML element.
126
     *
127
     * @param \DOMElement $parent The element we should append this username token to.
128
     * @return \DOMElement
129
     */
130
    public function toXML(?DOMElement $parent = null): DOMElement
131
    {
132
        $e = $this->instantiateParentElement($parent);
133
134
        if ($this->getAssertConstraint() !== null) {
135
            $e->setAttribute('AssertConstraint', $this->getAssertConstraint() ? 'true' : 'false');
136
        }
137
138
        $this->getValue()->toXML($e);
139
140
        /** @psalm-var \SimpleSAML\XML\SerializableElementInterface $child */
141
        foreach ($this->getElements() as $child) {
142
            if (!$child->isEmptyElement()) {
143
                $child->toXML($e);
144
            }
145
        }
146
147
        return $e;
148
    }
149
}
150