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\SchemaValidatableElementInterface; |
||
| 10 | use SimpleSAML\XML\SchemaValidatableElementTrait; |
||
| 11 | use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException; |
||
| 12 | use SimpleSAML\XMLSchema\Exception\TooManyElementsException; |
||
| 13 | use SimpleSAML\XMLSchema\Type\IDValue; |
||
| 14 | |||
| 15 | use function array_merge; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Class representing the simpleContent-type. |
||
| 19 | * |
||
| 20 | * @package simplesamlphp/xml-common |
||
| 21 | */ |
||
| 22 | final class SimpleContent extends AbstractAnnotated implements SchemaValidatableElementInterface |
||
| 23 | { |
||
| 24 | use SchemaValidatableElementTrait; |
||
| 25 | |||
| 26 | |||
| 27 | public const string LOCALNAME = 'simpleContent'; |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 28 | |||
| 29 | |||
| 30 | /** |
||
| 31 | * SimpleContent constructor |
||
| 32 | * |
||
| 33 | * @param \SimpleSAML\XMLSchema\XML\SimpleRestriction|\SimpleSAML\XMLSchema\XML\SimpleExtension $content |
||
| 34 | * @param \SimpleSAML\XMLSchema\XML\Annotation|null $annotation |
||
| 35 | * @param \SimpleSAML\XMLSchema\Type\IDValue|null $id |
||
| 36 | * @param array<\SimpleSAML\XML\Attribute> $namespacedAttributes |
||
| 37 | */ |
||
| 38 | public function __construct( |
||
| 39 | protected SimpleRestriction|SimpleExtension $content, |
||
| 40 | ?Annotation $annotation = null, |
||
| 41 | ?IDValue $id = null, |
||
| 42 | array $namespacedAttributes = [], |
||
| 43 | ) { |
||
| 44 | parent::__construct($annotation, $id, $namespacedAttributes); |
||
| 45 | } |
||
| 46 | |||
| 47 | |||
| 48 | /** |
||
| 49 | * Collect the value of the content-property |
||
| 50 | * |
||
| 51 | * @return \SimpleSAML\XMLSchema\XML\SimpleRestriction|\SimpleSAML\XMLSchema\XML\SimpleExtension |
||
| 52 | */ |
||
| 53 | public function getContent(): SimpleRestriction|SimpleExtension |
||
| 54 | { |
||
| 55 | return $this->content; |
||
| 56 | } |
||
| 57 | |||
| 58 | |||
| 59 | /** |
||
| 60 | * Test if an object, at the state it's in, would produce an empty XML-element |
||
| 61 | * |
||
| 62 | * @return bool |
||
| 63 | */ |
||
| 64 | public function isEmptyElement(): bool |
||
| 65 | { |
||
| 66 | return false; |
||
| 67 | } |
||
| 68 | |||
| 69 | |||
| 70 | /** |
||
| 71 | * Add this SimpleContent to an XML element. |
||
| 72 | * |
||
| 73 | * @param \DOMElement|null $parent The element we should append this SimpleContent to. |
||
| 74 | * @return \DOMElement |
||
| 75 | */ |
||
| 76 | public function toXML(?DOMElement $parent = null): DOMElement |
||
| 77 | { |
||
| 78 | $e = parent::toXML($parent); |
||
| 79 | |||
| 80 | $this->getContent()->toXML($e); |
||
| 81 | |||
| 82 | return $e; |
||
| 83 | } |
||
| 84 | |||
| 85 | |||
| 86 | /** |
||
| 87 | * Create an instance of this object from its XML representation. |
||
| 88 | * |
||
| 89 | * @param \DOMElement $xml |
||
| 90 | * @return static |
||
| 91 | * |
||
| 92 | * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException |
||
| 93 | * if the qualified name of the supplied element is wrong |
||
| 94 | */ |
||
| 95 | public static function fromXML(DOMElement $xml): static |
||
| 96 | { |
||
| 97 | Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class); |
||
| 98 | Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class); |
||
| 99 | |||
| 100 | $annotation = Annotation::getChildrenOfClass($xml); |
||
| 101 | Assert::maxCount($annotation, 1, TooManyElementsException::class); |
||
| 102 | |||
| 103 | $simpleRestriction = SimpleRestriction::getChildrenOfClass($xml); |
||
| 104 | Assert::maxCount($simpleRestriction, 1, TooManyElementsException::class); |
||
| 105 | |||
| 106 | $simpleExtension = SimpleExtension::getChildrenOfClass($xml); |
||
| 107 | Assert::maxCount($simpleExtension, 1, TooManyElementsException::class); |
||
| 108 | |||
| 109 | $content = array_merge($simpleRestriction, $simpleExtension); |
||
| 110 | Assert::maxCount($content, 1, TooManyElementsException::class); |
||
| 111 | |||
| 112 | return new static( |
||
| 113 | $content[0], |
||
| 114 | array_pop($annotation), |
||
| 115 | self::getOptionalAttribute($xml, 'id', IDValue::class, null), |
||
| 116 | self::getAttributesNSFromXML($xml), |
||
| 117 | ); |
||
| 118 | } |
||
| 119 | } |
||
| 120 |