AbstractRenewingType::toXML()   A
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
dl 0
loc 13
c 1
b 0
f 0
rs 9.6111
cc 5
nc 4
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\WSSecurity\XML\wst_200502;
6
7
use DOMElement;
8
use SimpleSAML\WSSecurity\Assert\Assert;
9
use SimpleSAML\XML\Exception\InvalidDOMElementException;
10
11
/**
12
 * Class defining the RenewingType element
13
 *
14
 * @package simplesamlphp/ws-security
15
 */
16
abstract class AbstractRenewingType extends AbstractWstElement
17
{
18
    /**
19
     * AbstractRenewingType constructor
20
     *
21
     * @param bool|null $allow
22
     * @param bool|null $ok
23
     */
24
    final public function __construct(
25
        protected ?bool $allow = null,
26
        protected ?bool $ok = null,
27
    ) {
28
    }
29
30
31
    /**
32
     * @return bool|null
33
     */
34
    public function getAllow(): ?bool
35
    {
36
        return $this->allow;
37
    }
38
39
40
    /**
41
     * @return bool|null
42
     */
43
    public function getOk(): ?bool
44
    {
45
        return $this->ok;
46
    }
47
48
49
    /**
50
     * Test if an object, at the state it's in, would produce an empty XML-element
51
     *
52
     * @return bool
53
     */
54
    public function isEmptyElement(): bool
55
    {
56
        return empty($this->getAllow())
57
            && empty($this->getOk());
58
    }
59
60
61
    /**
62
     * Create an instance of this object from its XML representation.
63
     *
64
     * @param \DOMElement $xml
65
     * @return static
66
     *
67
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
68
     *   if the qualified name of the supplied element is wrong
69
     */
70
    public static function fromXML(DOMElement $xml): static
71
    {
72
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
73
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
74
75
        return new static(
76
            self::getOptionalBooleanAttribute($xml, 'Allow', null),
77
            self::getOptionalBooleanAttribute($xml, 'OK', null),
78
        );
79
    }
80
81
82
    /**
83
     * Add this UseKeyType to an XML element.
84
     *
85
     * @param \DOMElement|null $parent The element we should append this username token to.
86
     * @return \DOMElement
87
     */
88
    public function toXML(?DOMElement $parent = null): DOMElement
89
    {
90
        $e = parent::instantiateParentElement($parent);
91
92
        if ($this->getAllow() !== null) {
93
            $e->setAttribute('Allow', $this->getAllow() ? 'true' : 'false');
94
        }
95
96
        if ($this->getOk() !== null) {
97
            $e->setAttribute('OK', $this->getOk() ? 'true' : 'false');
98
        }
99
100
        return $e;
101
    }
102
}
103