Passed
Push — master ( 8c2723...6ca2a7 )
by Tim
02:37
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 $content
33
     * @param string $type
34
     */
35
    protected function __construct(
36
        string $content,
37
        string $type
38
    ) {
39
        $this->setContent($content);
40
        $this->setType($type);
41
    }
42
43
44
    /**
45
     * Get the type of this Condition (expressed in the xsi:type attribute).
46
     *
47
     * @return string
48
     */
49
    public function getType(): string
50
    {
51
        return $this->type;
52
    }
53
54
55
    /**
56
     * Set the type of this Condition (in the xsi:type attribute)
57
     *
58
     * @param string $type
59
     */
60
    protected function setType(string $type): void
61
    {
62
        Assert::notWhitespaceOnly($type, 'The "xsi:type" attribute of a Condition cannot be empty.');
63
        Assert::contains($type, ':');
64
65
        $this->type = $type;
66
    }
67
68
69
    /**
70
     * Convert this Condition to XML.
71
     *
72
     * @param \DOMElement $parent The element we are converting to XML.
73
     * @return \DOMElement The XML element after adding the data corresponding to this Condition.
74
     */
75
    public function toXML(DOMElement $parent = null): DOMElement
76
    {
77
        $e = $this->instantiateParentElement($parent);
78
79
        $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...
80
        $e->setAttributeNS(Constants::NS_XSI, 'xsi:type', $this->type);
81
82
        return $e;
83
    }
84
}
85