Skip to content

Commit

Permalink
Add wsse:EmbeddedType
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdijen committed Jul 9, 2023
1 parent c0f6b43 commit bba5aaf
Showing 1 changed file with 116 additions and 0 deletions.
116 changes: 116 additions & 0 deletions src/XML/wsse/AbstractEmbeddedType.php
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;
}
}

0 comments on commit bba5aaf

Please sign in to comment.