-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
116 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SimpleSAML\WSSecurity\XML\wsse; | ||
|
||
use DOMElement; | ||
use SimpleSAML\Assert\Assert; | ||
use SimpleSAML\WSSecurity\Constants as C; | ||
use SimpleSAML\XML\Chunk; | ||
use SimpleSAML\XML\ExtentableAttributeElement; | ||
use SimpleSAML\XML\ExtentableElementElement; | ||
use SimpleSAML\XML\Exception\InvalidDOMElementException; | ||
use SimpleSAML\XML\Exception\SchemaViolationException; | ||
|
||
/** | ||
* Class defining the EmbeddedType element | ||
* | ||
* @package tvdijen/ws-security | ||
*/ | ||
abstract class AbstractEmbeddedType extends AbstractWsseElement | ||
{ | ||
use ExtendableAttributeTrait; | ||
use ExtendableElementTrait; | ||
|
||
/** The namespace-attribute for the xs:anyAttribute element */ | ||
public const XS_ANY_ATTR_NAMESPACE = C::XS_ANY_NS_OTHER; | ||
|
||
/** The namespace-attribute for the xs:any element */ | ||
public const XS_ANY_ELT_NAMESPACE = C::XS_ANY_NS_ANY; | ||
|
||
|
||
/** | ||
* AbstractEmbeddedType constructor | ||
* | ||
* @param string $valueType | ||
* @param \SimpleSAML\XML\SerializableElementInterface[] $children | ||
* @param array $namespacedAttributes | ||
*/ | ||
public function __construct( | ||
protected string $valueType, | ||
array $children = [], | ||
array $namespacedAttributes = [] | ||
) { | ||
Assert::validURI($valueType, SchemaViolationException::class); | ||
|
||
$this->setChildren($children); | ||
$this->setAttributesNS($namespacedAttributes); | ||
} | ||
|
||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getValueType(): string | ||
{ | ||
return $this->valueType; | ||
} | ||
|
||
|
||
/** | ||
* Create an instance of this object from its XML representation. | ||
* | ||
* @param \DOMElement $xml | ||
* @return static | ||
* | ||
* @throws \SimpleSAML\XML\Exception\InvalidDOMElementException | ||
* if the qualified name of the supplied element is wrong | ||
*/ | ||
public static function fromXML(DOMElement $xml): static | ||
{ | ||
Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class); | ||
Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class); | ||
|
||
$children = []; | ||
foreach ($xml->childNodes as $child) { | ||
if (!($child instanceof DOMElement)) { | ||
continue; | ||
} | ||
|
||
$children[] = new Chunk($child); | ||
} | ||
|
||
return new static( | ||
self::getAttribute($xml, 'ValueType'), | ||
$children, | ||
self::getAttributesNSFromXML($xml), | ||
); | ||
} | ||
|
||
|
||
/** | ||
* Add this username token to an XML element. | ||
* | ||
* @param \DOMElement $parent The element we should append this username token to. | ||
* @return \DOMElement | ||
*/ | ||
public function toXML(DOMElement $parent = null): DOMElement | ||
{ | ||
$e = parent::instantiateParentElement($parent); | ||
$e->setAttribute('ValueType', $this->getValueType()); | ||
|
||
foreach ($this->getAttributesNS() as $attr) { | ||
$attr->toXML($e); | ||
} | ||
|
||
/** @psalm-var \SimpleSAML\XML\SerializableElementInterface $child */ | ||
foreach ($this->getElements() as $child) { | ||
if (!$child->isEmptyElement()) { | ||
$child->toXML($e); | ||
} | ||
} | ||
|
||
return $e; | ||
} | ||
} |