simplesamlphp /
xml-common
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace SimpleSAML\XMLSchema\XML; |
||
| 6 | |||
| 7 | use DOMElement; |
||
| 8 | use DOMNodeList; |
||
| 9 | use SimpleSAML\XML\Assert\Assert; |
||
| 10 | use SimpleSAML\XML\ExtendableAttributesTrait; |
||
| 11 | use SimpleSAML\XML\SchemaValidatableElementInterface; |
||
| 12 | use SimpleSAML\XML\SchemaValidatableElementTrait; |
||
| 13 | use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException; |
||
| 14 | use SimpleSAML\XMLSchema\Type\AnyURIValue; |
||
| 15 | use SimpleSAML\XMLSchema\XML\Constants\NS; |
||
| 16 | |||
| 17 | use function strval; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Class representing the appinfo element |
||
| 21 | * |
||
| 22 | * @package simplesamlphp/xml-common |
||
| 23 | */ |
||
| 24 | final class Appinfo extends AbstractXsElement implements SchemaValidatableElementInterface |
||
| 25 | { |
||
| 26 | use ExtendableAttributesTrait; |
||
| 27 | use SchemaValidatableElementTrait; |
||
| 28 | |||
| 29 | |||
| 30 | public const string LOCALNAME = 'appinfo'; |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 31 | |||
| 32 | /** The namespace-attribute for the xs:anyAttribute element */ |
||
| 33 | public const array|string XS_ANY_ATTR_NAMESPACE = NS::OTHER; |
||
| 34 | |||
| 35 | |||
| 36 | /** |
||
| 37 | * Appinfo constructor |
||
| 38 | * |
||
| 39 | * @param \DOMNodeList<\DOMNode> $content |
||
| 40 | * @param \SimpleSAML\XMLSchema\Type\AnyURIValue|null $source |
||
| 41 | * @param array<\SimpleSAML\XML\Attribute> $namespacedAttributes |
||
| 42 | */ |
||
| 43 | final public function __construct( |
||
| 44 | protected DOMNodeList $content, |
||
| 45 | protected ?AnyURIValue $source = null, |
||
| 46 | array $namespacedAttributes = [], |
||
| 47 | ) { |
||
| 48 | $this->setAttributesNS($namespacedAttributes); |
||
| 49 | } |
||
| 50 | |||
| 51 | |||
| 52 | /** |
||
| 53 | * Get the content property. |
||
| 54 | * |
||
| 55 | * @return \DOMNodeList<\DOMNode> |
||
| 56 | */ |
||
| 57 | public function getContent(): DOMNodeList |
||
| 58 | { |
||
| 59 | return $this->content; |
||
| 60 | } |
||
| 61 | |||
| 62 | |||
| 63 | /** |
||
| 64 | * Get the source property. |
||
| 65 | * |
||
| 66 | * @return \SimpleSAML\XMLSchema\Type\AnyURIValue|null |
||
| 67 | */ |
||
| 68 | public function getSource(): ?AnyURIValue |
||
| 69 | { |
||
| 70 | return $this->source; |
||
| 71 | } |
||
| 72 | |||
| 73 | |||
| 74 | /** |
||
| 75 | * Test if an object, at the state it's in, would produce an empty XML-element |
||
| 76 | * |
||
| 77 | * @return bool |
||
| 78 | */ |
||
| 79 | public function isEmptyElement(): bool |
||
| 80 | { |
||
| 81 | return $this->getContent()->count() === 0 |
||
| 82 | && empty($this->getSource()) |
||
| 83 | && empty($this->getAttributesNS()); |
||
| 84 | } |
||
| 85 | |||
| 86 | |||
| 87 | /** |
||
| 88 | * Create an instance of this object from its XML representation. |
||
| 89 | * |
||
| 90 | * @param \DOMElement $xml |
||
| 91 | * @return static |
||
| 92 | * |
||
| 93 | * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException |
||
| 94 | * if the qualified name of the supplied element is wrong |
||
| 95 | */ |
||
| 96 | public static function fromXML(DOMElement $xml): static |
||
| 97 | { |
||
| 98 | Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class); |
||
| 99 | Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class); |
||
| 100 | |||
| 101 | return new static( |
||
| 102 | $xml->childNodes, |
||
| 103 | self::getOptionalAttribute($xml, 'source', AnyURIValue::class, null), |
||
| 104 | self::getAttributesNSFromXML($xml), |
||
| 105 | ); |
||
| 106 | } |
||
| 107 | |||
| 108 | |||
| 109 | /** |
||
| 110 | * Add this Appinfo to an XML element. |
||
| 111 | * |
||
| 112 | * @param \DOMElement|null $parent The element we should append this Appinfo to. |
||
| 113 | * @return \DOMElement |
||
| 114 | */ |
||
| 115 | public function toXML(?DOMElement $parent = null): DOMElement |
||
| 116 | { |
||
| 117 | $e = parent::instantiateParentElement($parent); |
||
| 118 | |||
| 119 | if ($this->getSource() !== null) { |
||
| 120 | $e->setAttribute('source', strval($this->getSource())); |
||
| 121 | } |
||
| 122 | |||
| 123 | foreach ($this->getAttributesNS() as $attr) { |
||
| 124 | $attr->toXML($e); |
||
| 125 | } |
||
| 126 | |||
| 127 | foreach ($this->getContent() as $i) { |
||
| 128 | $e->appendChild($e->ownerDocument->importNode($i, true)); |
||
| 129 | } |
||
| 130 | |||
| 131 | return $e; |
||
| 132 | } |
||
| 133 | } |
||
| 134 |