Skip to content

Commit

Permalink
Add generic XMLStringElementTrait::fromXML
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdijen committed Apr 28, 2021
1 parent 0ddc3ee commit 1db4c4f
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/XMLStringElementTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use DOMElement;
use SimpleSAML\Assert\Assert;
use SimpleSAML\XML\Constants;
use SimpleSAML\XML\Exception\InvalidDOMElementException;

/**
* Trait grouping common functionality for simple elements with just some textContent
Expand Down Expand Up @@ -67,6 +68,24 @@ protected function validateContent(/** @scrutinizer ignore-unused */ string $con
}


/**
* Convert XML into a class instance
*
* @param \DOMElement $xml The XML element we should load
* @return self
*
* @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
* If the qualified name of the supplied element is wrong
*/
public static function fromXML(DOMElement $xml): object
{
Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);

return new self($xml->textContent);
}


/**
* Convert this element to XML.
*
Expand All @@ -80,4 +99,7 @@ public function toXML(DOMElement $parent = null): DOMElement

return $e;
}


abstract public static function getLocalName(): string;
}

0 comments on commit 1db4c4f

Please sign in to comment.