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