Passed
Pull Request — master (#268)
by Tim
02:46
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
use function trim;
14
15
/**
16
 * SAML Condition data type.
17
 *
18
 * @package simplesamlphp/saml2
19
 */
20
abstract class Condition extends AbstractConditionType
21
{
22
    /** @var string */
23
    public const LOCALNAME = 'Condition';
24
25
    /** @var string */
26
    protected string $type;
27
28
29
    /**
30
     * Initialize a saml:Condition from scratch
31
     *
32
     * @param string $type
33
     */
34
    protected function __construct(
35
        string $type
36
    ) {
37
        $this->setType($type);
38
    }
39
40
41
    /**
42
     * Get the type of this Condition (expressed in the xsi:type attribute).
43
     *
44
     * @return string
45
     */
46
    public function getType(): string
47
    {
48
        return $this->type;
49
    }
50
51
52
    /**
53
     * Set the type of this Condition (in the xsi:type attribute)
54
     *
55
     * @param string $type
56
     */
57
    protected function setType(string $type): void
58
    {
59
        Assert::notWhitespaceOnly($type, 'The "xsi:type" attribute of a Condition cannot be empty.');
60
        Assert::contains($type, ':');
61
62
        $this->type = $type;
63
    }
64
65
66
    /**
67
     * Convert this Condition to XML.
68
     *
69
     * @param \DOMElement $parent The element we are converting to XML.
70
     * @return \DOMElement The XML element after adding the data corresponding to this Condition.
71
     */
72
    public function toXML(DOMElement $parent = null): DOMElement
73
    {
74
        $e = $this->instantiateParentElement($parent);
75
76
        $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...
77
        $e->setAttributeNS(Constants::NS_XSI, 'xsi:type', $this->type);
78
79
        return $e;
80
    }
81
}
82