Passed
Pull Request — master (#18)
by Tim
02:11
created

X509Certificate::fromXML()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 6
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSecurity\XML\ds;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\XML\Exception\InvalidDOMElementException;
10
use SimpleSAML\XML\XMLStringElementTrait;
11
12
13
/**
14
 * Class representing a ds:X509Certificate element.
15
 *
16
 * @package simplesamlphp/xml-security
17
 */
18
final class X509Certificate extends AbstractDsElement
19
{
20
    use XMLStringElementTrait;
21
22
23
    /**
24
     * Validate the content of the element.
25
     *
26
     * @param string $content  The value to go in the XML textContent
27
     * @throws \Exception on failure
28
     * @return void
29
     */
30
    protected function validateContent(string $content): void
31
    {
32
        Assert::notEmpty($content, 'ds:X509Certificate cannot be empty');
33
        Assert::stringPlausibleBase64($content, 'ds:X509Certificate is not a valid Base64 encoded string');
34
    }
35
36
37
    /**
38
     * Convert XML into a X509Certificate
39
     *
40
     * @param \DOMElement $xml The XML element we should load
41
     * @return self
42
     *
43
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
44
     *   If the qualified name of the supplied element is wrong
45
     */
46
    public static function fromXML(DOMElement $xml): object
47
    {
48
        Assert::same($xml->localName, 'X509Certificate', InvalidDOMElementException::class);
49
        Assert::same($xml->namespaceURI, X509Certificate::NS, InvalidDOMElementException::class);
50
51
        return new self($xml->textContent);
52
    }
53
}
54