Passed
Push — master ( 4c710f...167df6 )
by Tim
14:01
created

KeySize   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 12
dl 0
loc 60
c 1
b 0
f 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A validateContent() 0 5 1
A __construct() 0 3 1
A fromXML() 0 6 1
A toXML() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\WSSecurity\XML\wst_200502;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\XML\Exception\InvalidDOMElementException;
10
use SimpleSAML\XML\Exception\SchemaViolationException;
11
use SimpleSAML\XML\StringElementTrait;
12
13
/**
14
 * Class representing WS-trust KeySize.
15
 *
16
 * @package simplesamlphp/ws-security
17
 */
18
final class KeySize extends AbstractWstElement
19
{
20
    use StringElementTrait;
21
22
    /**
23
     * KeySize constructor.
24
     *
25
     * @param string $value The long.
26
     */
27
    final public function __construct(string $value)
28
    {
29
        $this->setContent($value);
30
    }
31
32
33
    /**
34
     * Validate the content of the element.
35
     *
36
     * @param string $content  The value to go in the XML textContent
37
     * @throws \SimpleSAML\XML\Exception\SchemaViolationException on failure
38
     * @return void
39
     */
40
    protected function validateContent(string $content): void
41
    {
42
        $content = intval($content);
43
        Assert::natural($content);
44
        Assert::range($content, 0, 4294967295, SchemaViolationException::class);
45
    }
46
47
48
    /**
49
     * Convert XML into a class instance
50
     *
51
     * @param \DOMElement $xml The XML element we should load
52
     * @return static
53
     *
54
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
55
     *   If the qualified name of the supplied element is wrong
56
     */
57
    public static function fromXML(DOMElement $xml): static
58
    {
59
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
60
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
61
62
        return new static($xml->textContent);
63
    }
64
65
66
    /**
67
     * Convert this element to XML.
68
     *
69
     * @param \DOMElement|null $parent The element we should append this element to.
70
     * @return \DOMElement
71
     */
72
    public function toXML(DOMElement $parent = null): DOMElement
73
    {
74
        $e = $this->instantiateParentElement($parent);
75
        $e->textContent = $this->getContent();
76
77
        return $e;
78
    }
79
}
80