ExtensionsTrait::toXML()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 2
nop 1
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\SAML2\Assert\Assert;
9
use SimpleSAML\SAML2\Constants as C;
0 ignored issues
show
Bug introduced by
The type SimpleSAML\SAML2\Constants was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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
     */
72
    public function isEmptyElement(): bool
73
    {
74
        if (empty($this->getList())) {
75
            return true;
76
        }
77
78
        foreach ($this->getList() as $extension) {
79
            if ($extension->isEmptyElement() === false) {
80
                return false;
81
            }
82
        }
83
84
        return true;
85
    }
86
87
88
    /**
89
     * Convert this object into its md:Extensions XML representation.
90
     *
91
     * @param \DOMElement|null $parent The element we should add this Extensions element to.
92
     * @return \DOMElement The new md:Extensions XML element.
93
     */
94
    public function toXML(?DOMElement $parent = null): DOMElement
95
    {
96
        $e = $this->instantiateParentElement($parent);
97
98
        if (!$this->isEmptyElement()) {
99
            foreach ($this->getList() as $extension) {
100
                if (!$extension->isEmptyElement()) {
101
                    $extension->toXML($e);
102
                }
103
            }
104
        }
105
106
        return $e;
107
    }
108
109
110
    /**
111
     */
112
    abstract public function instantiateParentElement(?DOMElement $parent = null): DOMElement;
113
}
114