Passed
Push — master ( 768e90...be6a83 )
by Tim
01:37
created

Proxies::toXML()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\CAS\XML\cas;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\XML\Exception\InvalidDOMElementException;
10
use SimpleSAML\XML\Exception\MissingElementException;
11
12
/**
13
 * Class for CAS proxies
14
 *
15
 * @package simplesamlphp/cas
16
 */
17
final class Proxies extends AbstractCasElement
18
{
19
    /** @var string */
20
    public const LOCALNAME = 'proxies';
21
22
23
    /**
24
     * Initialize a Proxies element.
25
     *
26
     * @param \SimpleSAML\CAS\XML\cas\Proxy[] $proxy
27
     */
28
    final public function __construct(
29
        protected array $proxy = [],
30
    ) {
31
        Assert::allIsInstanceOf($proxy, Proxy::class);
32
        Assert::minCount($proxy, 1, 'Missing at least one Proxy in Proxies.', MissingElementException::class);
33
    }
34
35
36
    /**
37
     * @return \SimpleSAML\CAS\XML\cas\Proxy[]
38
     */
39
    public function getProxy(): array
40
    {
41
        return $this->proxy;
42
    }
43
44
45
    /**
46
     * Convert XML into a Proxies-element
47
     *
48
     * @param \DOMElement $xml The XML element we should load
49
     * @return static
50
     *
51
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
52
     *  if the qualified name of the supplied element is wrong
53
     * @throws \SimpleSAML\XML\Exception\MissingElementException if one of the mandatory child-elements is missing
54
     * @throws \SimpleSAML\XML\Exception\TooManyElementsException if too many child-elements of a type are specified
55
     */
56
    public static function fromXML(DOMElement $xml): static
57
    {
58
        Assert::same($xml->localName, 'proxies', InvalidDOMElementException::class);
59
        Assert::same($xml->namespaceURI, Proxies::NS, InvalidDOMElementException::class);
60
61
        $proxy = Proxy::getChildrenOfClass($xml);
62
        Assert::minCount($proxy, 1, 'Missing at least one Proxy in Proxies.', MissingElementException::class);
63
64
        return new static($proxy);
65
    }
66
67
68
    /**
69
     * Convert this Proxies to XML.
70
     *
71
     * @param \DOMElement|null $parent The element we should append this Proxies to.
72
     * @return \DOMElement
73
     */
74
    public function toXML(DOMElement $parent = null): DOMElement
75
    {
76
        $e = $this->instantiateParentElement($parent);
77
78
        foreach ($this->getProxy() as $proxy) {
79
            $proxy->toXML($e);
80
        }
81
82
        return $e;
83
    }
84
}
85