ExtensionsTrait::isEmptyElement()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 4
nop 0
dl 0
loc 13
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\XML;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\SAML2\Constants as C;
10
use SimpleSAML\SAML2\Exception\ProtocolViolationException;
11
use SimpleSAML\XML\ElementInterface;
12
13
use function in_array;
14
15
/**
16
 * Trait grouping common functionality for elements implementing ExtensionType.
17
 *
18
 * @package simplesamlphp/saml2
19
 */
20
trait ExtensionsTrait
21
{
22
    /** @var \SimpleSAML\XML\SerializableElementInterface[] */
23
    protected array $extensions = [];
24
25
26
    /**
27
     * Extensions constructor.
28
     *
29
     * @param \SimpleSAML\XML\SerializableElementInterface[] $extensions
30
     */
31
    public function __construct(array $extensions)
32
    {
33
        Assert::maxCount($extensions, C::UNBOUNDED_LIMIT);
34
        Assert::allIsInstanceOf($extensions, ElementInterface::class);
35
36
        foreach ($extensions as $extension) {
37
            /** @var \SimpleSAML\XML\AbstractElement $extension */
38
            $namespace = $extension->getNamespaceURI();
39
40
            Assert::notNull(
41
                $namespace,
42
                'Extensions MUST NOT include global (non-namespace-qualified) elements.',
43
                ProtocolViolationException::class,
44
            );
45
            Assert::true(
46
                !in_array($namespace, [C::NS_SAML, C::NS_SAMLP], true),
47
                'Extensions MUST NOT include any SAML-defined namespace elements.',
48
                ProtocolViolationException::class,
49
            );
50
        }
51
52
        /**
53
         * Set an array with all extensions present.
54
         */
55
        $this->extensions = $extensions;
56
    }
57
58
59
    /**
60
     * Get an array with all extensions present.
61
     *
62
     * @return \SimpleSAML\XML\SerializableElementInterface[]
63
     */
64
    public function getList(): array
65
    {
66
        return $this->extensions;
67
    }
68
69
70
    /**
71
     * @return bool
72
     */
73
    public function isEmptyElement(): bool
74
    {
75
        if (empty($this->extensions)) {
76
            return true;
77
        }
78
79
        foreach ($this->extensions as $extension) {
80
            if ($extension->isEmptyElement() === false) {
81
                return false;
82
            }
83
        }
84
85
        return true;
86
    }
87
88
89
    /**
90
     * Convert this object into its md:Extensions XML representation.
91
     *
92
     * @param \DOMElement|null $parent The element we should add this Extensions element to.
93
     * @return \DOMElement The new md:Extensions XML element.
94
     */
95
    public function toXML(?DOMElement $parent = null): DOMElement
96
    {
97
        $e = $this->instantiateParentElement($parent);
98
99
        foreach ($this->extensions as $extension) {
100
            if (!$extension->isEmptyElement()) {
101
                $extension->toXML($e);
102
            }
103
        }
104
105
        return $e;
106
    }
107
108
109
    /**
110
     * @param \DOMElement|null $parent
111
     * @return \DOMElement
112
     */
113
    abstract public function instantiateParentElement(?DOMElement $parent = null): DOMElement;
114
}
115