Passed
Pull Request — master (#268)
by Tim
10:48 queued 08:24
created

Condition::fromXML()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 1
dl 0
loc 16
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\XML\saml;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\SAML2\Constants;
10
use SimpleSAML\SAML2\Utils;
11
use SimpleSAML\XML\Exception\InvalidDOMElementException;
12
13
/**
14
 * SAML Condition data type.
15
 *
16
 * @package simplesamlphp/saml2
17
 */
18
abstract class Condition extends AbstractConditionType
19
{
20
    /** @var string */
21
    public const LOCALNAME = 'Condition';
22
23
    /** @var string */
24
    protected string $type;
25
26
27
    /**
28
     * Initialize a saml:Condition from scratch
29
     *
30
     * @param string $type
31
     */
32
    protected function __construct(
33
        string $type
34
    ) {
35
        $this->setType($type);
36
    }
37
38
39
    /**
40
     * Get the type of this Condition (expressed in the xsi:type attribute).
41
     *
42
     * @return string
43
     */
44
    public function getType(): string
45
    {
46
        return $this->type;
47
    }
48
49
50
    /**
51
     * Set the type of this Condition (in the xsi:type attribute)
52
     *
53
     * @param string $type
54
     */
55
    protected function setType(string $type): void
56
    {
57
        Assert::notWhitespaceOnly($type, 'The "xsi:type" attribute of a Condition cannot be empty.');
58
        Assert::contains($type, ':');
59
60
        $this->type = $type;
61
    }
62
63
64
    /**
65
     * Convert this Condition to XML.
66
     *
67
     * @param \DOMElement $parent The element we are converting to XML.
68
     * @return \DOMElement The XML element after adding the data corresponding to this Condition.
69
     */
70
    public function toXML(DOMElement $parent = null): DOMElement
71
    {
72
        $e = $this->instantiateParentElement($parent);
73
74
        $e->setAttribute('xmlns:' . static::XSI_TYPE_PREFIX, static::XSI_TYPE_NS);
0 ignored issues
show
Bug introduced by
The constant SimpleSAML\SAML2\XML\sam...dition::XSI_TYPE_PREFIX was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The constant SimpleSAML\SAML2\XML\saml\Condition::XSI_TYPE_NS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
75
        $e->setAttributeNS(Constants::NS_XSI, 'xsi:type', $this->type);
76
77
        return $e;
78
    }
79
}
80