SimpleContent   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 23
dl 0
loc 96
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A isEmptyElement() 0 3 1
A toXML() 0 7 1
A __construct() 0 7 1
A getContent() 0 3 1
A fromXML() 0 22 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSchema\XML;
6
7
use DOMElement;
8
use SimpleSAML\XML\Assert\Assert;
9
use SimpleSAML\XML\SchemaValidatableElementInterface;
10
use SimpleSAML\XML\SchemaValidatableElementTrait;
11
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
12
use SimpleSAML\XMLSchema\Exception\TooManyElementsException;
13
use SimpleSAML\XMLSchema\Type\IDValue;
14
15
use function array_merge;
16
17
/**
18
 * Class representing the simpleContent-type.
19
 *
20
 * @package simplesamlphp/xml-common
21
 */
22
final class SimpleContent extends AbstractAnnotated implements SchemaValidatableElementInterface
23
{
24
    use SchemaValidatableElementTrait;
25
26
27
    /** @var string */
28
    public const LOCALNAME = 'simpleContent';
29
30
31
    /**
32
     * SimpleContent constructor
33
     *
34
     * @param \SimpleSAML\XMLSchema\XML\SimpleRestriction|\SimpleSAML\XMLSchema\XML\SimpleExtension $content
35
     * @param \SimpleSAML\XMLSchema\XML\Annotation|null $annotation
36
     * @param \SimpleSAML\XMLSchema\Type\IDValue|null $id
37
     * @param array<\SimpleSAML\XML\Attribute> $namespacedAttributes
38
     */
39
    public function __construct(
40
        protected SimpleRestriction|SimpleExtension $content,
41
        ?Annotation $annotation = null,
42
        ?IDValue $id = null,
43
        array $namespacedAttributes = [],
44
    ) {
45
        parent::__construct($annotation, $id, $namespacedAttributes);
46
    }
47
48
49
    /**
50
     * Collect the value of the content-property
51
     *
52
     * @return \SimpleSAML\XMLSchema\XML\SimpleRestriction|\SimpleSAML\XMLSchema\XML\SimpleExtension
53
     */
54
    public function getContent(): SimpleRestriction|SimpleExtension
55
    {
56
        return $this->content;
57
    }
58
59
60
    /**
61
     * Test if an object, at the state it's in, would produce an empty XML-element
62
     *
63
     * @return bool
64
     */
65
    public function isEmptyElement(): bool
66
    {
67
        return false;
68
    }
69
70
71
    /**
72
     * Add this SimpleContent to an XML element.
73
     *
74
     * @param \DOMElement|null $parent The element we should append this SimpleContent to.
75
     * @return \DOMElement
76
     */
77
    public function toXML(?DOMElement $parent = null): DOMElement
78
    {
79
        $e = parent::toXML($parent);
80
81
        $this->getContent()->toXML($e);
82
83
        return $e;
84
    }
85
86
87
    /**
88
     * Create an instance of this object from its XML representation.
89
     *
90
     * @param \DOMElement $xml
91
     * @return static
92
     *
93
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
94
     *   if the qualified name of the supplied element is wrong
95
     */
96
    public static function fromXML(DOMElement $xml): static
97
    {
98
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
99
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
100
101
        $annotation = Annotation::getChildrenOfClass($xml);
102
        Assert::maxCount($annotation, 1, TooManyElementsException::class);
103
104
        $simpleRestriction = SimpleRestriction::getChildrenOfClass($xml);
105
        Assert::maxCount($simpleRestriction, 1, TooManyElementsException::class);
106
107
        $simpleExtension = SimpleExtension::getChildrenOfClass($xml);
108
        Assert::maxCount($simpleExtension, 1, TooManyElementsException::class);
109
110
        $content = array_merge($simpleRestriction, $simpleExtension);
111
        Assert::maxCount($content, 1, TooManyElementsException::class);
112
113
        return new static(
114
            $content[0],
115
            array_pop($annotation),
116
            self::getOptionalAttribute($xml, 'id', IDValue::class, null),
117
            self::getAttributesNSFromXML($xml),
118
        );
119
    }
120
}
121