simplesamlphp /
xml-common
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace SimpleSAML\XMLSchema\XML; |
||
| 6 | |||
| 7 | use DOMElement; |
||
| 8 | use SimpleSAML\XML\Assert\Assert; |
||
| 9 | use SimpleSAML\XML\Constants as C_XML; |
||
| 10 | use SimpleSAML\XML\SchemaValidatableElementInterface; |
||
| 11 | use SimpleSAML\XML\SchemaValidatableElementTrait; |
||
| 12 | use SimpleSAML\XMLSchema\Constants as C_XS; |
||
| 13 | use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException; |
||
| 14 | use SimpleSAML\XMLSchema\Exception\SchemaViolationException; |
||
| 15 | use SimpleSAML\XMLSchema\Type\AnyURIValue; |
||
| 16 | use SimpleSAML\XMLSchema\Type\IDValue; |
||
| 17 | use SimpleSAML\XMLSchema\XML\Interface\RedefinableInterface; |
||
| 18 | |||
| 19 | use function strval; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Class representing the redefine-element |
||
| 23 | * |
||
| 24 | * @package simplesamlphp/xml-common |
||
| 25 | */ |
||
| 26 | final class Redefine extends AbstractOpenAttrs implements SchemaValidatableElementInterface |
||
| 27 | { |
||
| 28 | use SchemaValidatableElementTrait; |
||
| 29 | |||
| 30 | |||
| 31 | public const string LOCALNAME = 'redefine'; |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 32 | |||
| 33 | |||
| 34 | /** |
||
| 35 | * Schema constructor |
||
| 36 | * |
||
| 37 | * @param \SimpleSAML\XMLSchema\Type\AnyURIValue $schemaLocation |
||
| 38 | * @param \SimpleSAML\XMLSchema\Type\IDValue|null $id |
||
| 39 | * @param ( |
||
| 40 | * \SimpleSAML\XMLSchema\XML\Annotation| |
||
| 41 | * \SimpleSAML\XMLSchema\XML\Interface\RedefinableInterface |
||
| 42 | * )[] $redefineElements |
||
| 43 | * @param array<\SimpleSAML\XML\Attribute> $namespacedAttributes |
||
| 44 | */ |
||
| 45 | public function __construct( |
||
| 46 | protected AnyURIValue $schemaLocation, |
||
| 47 | protected ?IDValue $id = null, |
||
| 48 | protected array $redefineElements = [], |
||
| 49 | array $namespacedAttributes = [], |
||
| 50 | ) { |
||
| 51 | Assert::maxCount($redefineElements, C_XML::UNBOUNDED_LIMIT); |
||
| 52 | Assert::allIsInstanceOfAny( |
||
| 53 | $redefineElements, |
||
| 54 | [RedefinableInterface::class, Annotation::class], |
||
| 55 | SchemaViolationException::class, |
||
| 56 | ); |
||
| 57 | |||
| 58 | parent::__construct($namespacedAttributes); |
||
| 59 | } |
||
| 60 | |||
| 61 | |||
| 62 | /** |
||
| 63 | * Collect the value of the redefineElements-property |
||
| 64 | * |
||
| 65 | * @return array<\SimpleSAML\XMLSchema\XML\Interface\RedefinableInterface|\SimpleSAML\XMLSchema\XML\Annotation> |
||
| 66 | */ |
||
| 67 | public function getRedefineElements(): array |
||
| 68 | { |
||
| 69 | return $this->redefineElements; |
||
| 70 | } |
||
| 71 | |||
| 72 | |||
| 73 | /** |
||
| 74 | * Collect the value of the schemaLocation-property |
||
| 75 | * |
||
| 76 | * @return \SimpleSAML\XMLSchema\Type\AnyURIValue |
||
| 77 | */ |
||
| 78 | public function getSchemaLocation(): AnyURIValue |
||
| 79 | { |
||
| 80 | return $this->schemaLocation; |
||
| 81 | } |
||
| 82 | |||
| 83 | |||
| 84 | /** |
||
| 85 | * Collect the value of the id-property |
||
| 86 | * |
||
| 87 | * @return \SimpleSAML\XMLSchema\Type\IDValue|null |
||
| 88 | */ |
||
| 89 | public function getID(): ?IDValue |
||
| 90 | { |
||
| 91 | return $this->id; |
||
| 92 | } |
||
| 93 | |||
| 94 | |||
| 95 | /** |
||
| 96 | * Test if an object, at the state it's in, would produce an empty XML-element |
||
| 97 | * |
||
| 98 | * @return bool |
||
| 99 | */ |
||
| 100 | public function isEmptyElement(): bool |
||
| 101 | { |
||
| 102 | return false; |
||
| 103 | } |
||
| 104 | |||
| 105 | |||
| 106 | /** |
||
| 107 | * Create an instance of this object from its XML representation. |
||
| 108 | * |
||
| 109 | * @param \DOMElement $xml |
||
| 110 | * @return static |
||
| 111 | * |
||
| 112 | * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException |
||
| 113 | * if the qualified name of the supplied element is wrong |
||
| 114 | */ |
||
| 115 | public static function fromXML(DOMElement $xml): static |
||
| 116 | { |
||
| 117 | Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class); |
||
| 118 | Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class); |
||
| 119 | |||
| 120 | $allowed = [ |
||
| 121 | 'annotation' => Annotation::class, |
||
| 122 | 'attributeGroup' => NamedAttributeGroup::class, |
||
| 123 | 'complexType' => TopLevelComplexType::class, |
||
| 124 | 'group' => NamedGroup::class, |
||
| 125 | 'simpleType' => TopLevelSimpleType::class, |
||
| 126 | ]; |
||
| 127 | |||
| 128 | $redefineElements = []; |
||
| 129 | foreach ($xml->childNodes as $node) { |
||
| 130 | if ($node instanceof DOMElement) { |
||
| 131 | if ($node->namespaceURI === C_XS::NS_XS && array_key_exists($node->localName, $allowed)) { |
||
| 132 | $redefineElements[] = $allowed[$node->localName]::fromXML($node); |
||
| 133 | } |
||
| 134 | } |
||
| 135 | } |
||
| 136 | |||
| 137 | return new static( |
||
| 138 | self::getAttribute($xml, 'schemaLocation', AnyURIValue::class), |
||
| 139 | self::getOptionalAttribute($xml, 'id', IDValue::class, null), |
||
| 140 | $redefineElements, |
||
| 141 | self::getAttributesNSFromXML($xml), |
||
| 142 | ); |
||
| 143 | } |
||
| 144 | |||
| 145 | |||
| 146 | /** |
||
| 147 | * Add this Schema to an XML element. |
||
| 148 | * |
||
| 149 | * @param \DOMElement|null $parent The element we should append this Schema to. |
||
| 150 | * @return \DOMElement |
||
| 151 | */ |
||
| 152 | public function toXML(?DOMElement $parent = null): DOMElement |
||
| 153 | { |
||
| 154 | $e = parent::toXML($parent); |
||
| 155 | $e->setAttribute('schemaLocation', strval($this->getSchemaLocation())); |
||
| 156 | |||
| 157 | if ($this->getId() !== null) { |
||
| 158 | $e->setAttribute('id', strval($this->getId())); |
||
| 159 | } |
||
| 160 | |||
| 161 | foreach ($this->getRedefineElements() as $elt) { |
||
| 162 | $elt->toXML($e); |
||
| 163 | } |
||
| 164 | |||
| 165 | return $e; |
||
| 166 | } |
||
| 167 | } |
||
| 168 |