AttributeStatement::fromXML()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\XML\saml;
6
7
use DOMElement;
8
use SimpleSAML\SAML2\Assert\Assert;
9
use SimpleSAML\XML\Constants as C;
0 ignored issues
show
Bug introduced by
The type SimpleSAML\XML\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\XML\SchemaValidatableElementInterface;
11
use SimpleSAML\XML\SchemaValidatableElementTrait;
12
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
13
14
/**
15
 * Class representing a SAML2 AttributeStatement
16
 *
17
 * @package simplesamlphp/saml2
18
 */
19
class AttributeStatement extends AbstractStatementType implements SchemaValidatableElementInterface
20
{
21
    use SchemaValidatableElementTrait;
22
23
24
    /**
25
     * AttributeStatement constructor.
26
     *
27
     * @param \SimpleSAML\SAML2\XML\saml\Attribute[] $attributes
28
     * @param \SimpleSAML\SAML2\XML\saml\EncryptedAttribute[] $encryptedAttributes
29
     */
30
    final public function __construct(
31
        protected array $attributes = [],
32
        protected array $encryptedAttributes = [],
33
    ) {
34
        Assert::true(!empty($attributes) || !empty($encryptedAttributes));
35
        Assert::maxCount($attributes, C::UNBOUNDED_LIMIT);
36
        Assert::maxCount($encryptedAttributes, C::UNBOUNDED_LIMIT);
37
        Assert::allIsInstanceOf($attributes, Attribute::class);
38
        Assert::allIsInstanceOf($encryptedAttributes, EncryptedAttribute::class);
39
    }
40
41
42
    /**
43
     * @return \SimpleSAML\SAML2\XML\saml\Attribute[]
44
     */
45
    public function getAttributes(): array
46
    {
47
        return $this->attributes;
48
    }
49
50
51
    /**
52
     * @return \SimpleSAML\SAML2\XML\saml\EncryptedAttribute[]
53
     */
54
    public function getEncryptedAttributes(): array
55
    {
56
        return $this->encryptedAttributes;
57
    }
58
59
60
    /**
61
     */
62
    public function hasEncryptedAttributes(): bool
63
    {
64
        return !empty($this->encryptedAttributes);
65
    }
66
67
68
    /**
69
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
70
     *   if the qualified name of the supplied element is wrong
71
     */
72
    public static function fromXML(DOMElement $xml): static
73
    {
74
        Assert::same($xml->localName, 'AttributeStatement', InvalidDOMElementException::class);
75
        Assert::same($xml->namespaceURI, AttributeStatement::NS, InvalidDOMElementException::class);
76
77
        return new static(
78
            Attribute::getChildrenOfClass($xml),
79
            EncryptedAttribute::getChildrenOfClass($xml),
80
        );
81
    }
82
83
84
    /**
85
     * Convert this Attribute to XML.
86
     */
87
    public function toXML(?DOMElement $parent = null): DOMElement
88
    {
89
        $e = $this->instantiateParentElement($parent);
90
91
        foreach ($this->getAttributes() as $attribute) {
92
            $attribute->toXML($e);
93
        }
94
95
        foreach ($this->getEncryptedAttributes() as $encryptedAttribute) {
96
            $encryptedAttribute->toXML($e);
97
        }
98
99
        return $e;
100
    }
101
}
102