Anonymous   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 17
dl 0
loc 66
rs 10
c 1
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A fromXML() 0 14 2
A __construct() 0 7 1
A toXML() 0 9 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\WSSecurity\XML\wsaw;
6
7
use DOMElement;
8
use SimpleSAML\WSSecurity\Assert\Assert;
9
use SimpleSAML\XML\ExtendableAttributesTrait;
10
use SimpleSAML\XML\SchemaValidatableElementInterface;
11
use SimpleSAML\XML\SchemaValidatableElementTrait;
12
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
13
use SimpleSAML\XMLSchema\Exception\SchemaViolationException;
14
use SimpleSAML\XMLSchema\XML\Constants\NS;
15
use ValueError;
16
17
use function sprintf;
18
19
/**
20
 * Class defining the Anonymous element
21
 *
22
 * @package simplesamlphp/ws-security
23
 */
24
final class Anonymous extends AbstractAnonymousType implements SchemaValidatableElementInterface
25
{
26
    use ExtendableAttributesTrait;
27
    use SchemaValidatableElementTrait;
0 ignored issues
show
introduced by
The trait SimpleSAML\XML\SchemaValidatableElementTrait requires some properties which are not provided by SimpleSAML\WSSecurity\XML\wsaw\Anonymous: $message, $line
Loading history...
28
29
30
    /** The namespace-attribute for the xs:anyAttribute element */
31
    public const XS_ANY_ATTR_NAMESPACE = NS::OTHER;
32
33
34
    /**
35
     * Anonymous constructor
36
     *
37
     * @param array<\SimpleSAML\XML\Attribute> $namespacedAttributes
38
     */
39
    final public function __construct(
40
        AnonymousEnum $value,
41
        array $namespacedAttributes = [],
42
    ) {
43
        parent::__construct($value);
44
45
        $this->setAttributesNS($namespacedAttributes);
46
    }
47
48
49
    /**
50
     * Create an instance of this object from its XML representation.
51
     *
52
     * @param \DOMElement $xml
53
     * @return static
54
     *
55
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
56
     *   if the qualified name of the supplied element is wrong
57
     */
58
    public static function fromXML(DOMElement $xml): static
59
    {
60
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
61
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
62
63
        try {
64
            $anonymous = AnonymousEnum::from($xml->textContent);
65
        } catch (ValueError) {
66
            throw new SchemaViolationException(
67
                sprintf('Unknown value \'%s\' for Anonymous element.', $xml->textContent),
68
            );
69
        }
70
71
        return new static($anonymous, self::getAttributesNSFromXML($xml));
72
    }
73
74
75
    /**
76
     * Convert this Anonymous to XML.
77
     *
78
     * @param \DOMElement|null $parent The element we should append this class to.
79
     * @return \DOMElement The XML element after adding the data corresponding to this Anonymous.
80
     */
81
    public function toXML(?DOMElement $parent = null): DOMElement
82
    {
83
        $e = parent::toXML($parent);
84
85
        foreach ($this->getAttributesNS() as $attr) {
86
            $attr->toXML($e);
87
        }
88
89
        return $e;
90
    }
91
}
92