AudienceRestriction   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 65
rs 10
c 0
b 0
f 0
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A toXML() 0 9 2
A fromXML() 0 8 1
A getAudience() 0 3 1
A __construct() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\XML\saml;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\XML\Constants as C;
10
use SimpleSAML\XML\Exception\InvalidDOMElementException;
11
use SimpleSAML\XML\Exception\SchemaViolationException;
12
use SimpleSAML\XML\SchemaValidatableElementInterface;
13
use SimpleSAML\XML\SchemaValidatableElementTrait;
14
15
/**
16
 * SAML AudienceRestriction data type.
17
 *
18
 * @package simplesamlphp/saml2
19
 */
20
final class AudienceRestriction extends AbstractConditionType implements SchemaValidatableElementInterface
21
{
22
    use SchemaValidatableElementTrait;
23
24
25
    /**
26
     * Initialize a saml:AudienceRestriction
27
     *
28
     * @param \SimpleSAML\SAML2\XML\saml\Audience[] $audience
29
     */
30
    public function __construct(
31
        protected array $audience,
32
    ) {
33
        Assert::minCount($audience, 1, SchemaViolationException::class);
34
        Assert::maxCount($audience, C::UNBOUNDED_LIMIT);
35
        Assert::allIsInstanceOf($audience, Audience::class, SchemaViolationException::class);
36
    }
37
38
39
    /**
40
     * Collect the audience
41
     *
42
     * @return \SimpleSAML\SAML2\XML\saml\Audience[]
43
     */
44
    public function getAudience(): array
45
    {
46
        return $this->audience;
47
    }
48
49
50
    /**
51
     * Convert XML into an AudienceRestriction
52
     *
53
     * @param \DOMElement $xml The XML element we should load
54
     * @return static
55
     *
56
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
57
     *   if the qualified name of the supplied element is wrong
58
     */
59
    public static function fromXML(DOMElement $xml): static
60
    {
61
        Assert::same($xml->localName, 'AudienceRestriction', InvalidDOMElementException::class);
62
        Assert::same($xml->namespaceURI, AudienceRestriction::NS, InvalidDOMElementException::class);
63
64
        $audience = Audience::getChildrenOfClass($xml);
65
66
        return new static($audience);
67
    }
68
69
70
    /**
71
     * Convert this Audience to XML.
72
     *
73
     * @param \DOMElement|null $parent The element we are converting to XML.
74
     * @return \DOMElement The XML element after adding the data corresponding to this AudienceRestriction.
75
     */
76
    public function toXML(?DOMElement $parent = null): DOMElement
77
    {
78
        $e = $this->instantiateParentElement($parent);
79
80
        foreach ($this->getAudience() as $audience) {
81
            $audience->toXML($e);
82
        }
83
84
        return $e;
85
    }
86
}
87