Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/xsd types #6

Draft
wants to merge 18 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
Split element- and extension-registry
tvdijen committed Jan 28, 2025

Verified

This commit was signed with the committer’s verified signature.
leighmacdonald Leigh MacDonald
commit f74923ed4ec45662eff459a0372cef8255acc543
49 changes: 28 additions & 21 deletions src/SAML11/Compat/AbstractContainer.php
Original file line number Diff line number Diff line change
@@ -17,12 +17,16 @@
use function array_key_exists;
use function implode;
use function is_subclass_of;
use function strval;

abstract class AbstractContainer
{
/** @var array */
protected array $registry = [];

/** @var array */
protected array $extRegistry = [];

/** @var array|null */
protected ?array $blacklistedEncryptionAlgorithms = [
EncryptionAlgorithmFactory::DEFAULT_BLACKLIST,
@@ -43,6 +47,20 @@ public function getBlacklistedEncryptionAlgorithms(): ?array
}


/**
* Register a class that can handle a given element.
*
* @param string $class The class name of a class extending AbstractElement
* @psalm-param class-string $class
*/
public function registerElementHandler(string $class): void
{
Assert::subclassOf($class, AbstractElement::class);
$key = '{' . strval($class::NS) . '}' . AbstractElement::getClassName($class);
$this->registry[$key] = $class;
}


/**
* Register a class that can handle given extension points of the standard.
*
@@ -51,32 +69,27 @@ public function getBlacklistedEncryptionAlgorithms(): ?array
*/
public function registerExtensionHandler(string $class): void
{
Assert::subclassOf($class, AbstractElement::class);
if (is_subclass_of($class, ExtensionPointInterface::class, true)) {
$key = '{' . $class::getXsiTypeNamespaceURI() . '}' . $class::getXsiTypePrefix() . ':' . $class::getXsiTypeName();
} else {
$className = AbstractElement::getClassName($class);
$key = ($class::NS === null) ? $className : implode(':', [$class::NS, $className]);
}
$this->registry[$key] = $class;
Assert::subclassOf($class, ExtensionPointInterface::class);
$key = '{' . $class::getXsiTypeNamespaceURI() . '}' . $class::getXsiTypeName();
$this->extRegistry[$key] = $class;
}


/**
* Search for a class that implements an $element in the given $namespace.
* Search for a class that implements an element in the given $namespace.
*
* Such classes must have been registered previously by calling registerExtensionHandler(), and they must
* extend \SimpleSAML\XML\AbstractElement.
*
* @param \SimpleSAML\XML\Typr\QNameValue|null $qName The qualified name of the element.
* @param \SimpleSAML\XML\Type\QNameValue|null $qName The qualified name of the element.
*
* @return string|null The fully-qualified name of a class extending \SimpleSAML\XML\AbstractElement and
* implementing support for the given element, or null if no such class has been registered before.
* @psalm-return class-string|null
*/
public function getElementHandler(QNameValue $qName): ?string
{
$key = $qName->getRawValue();
$key = '{' . strval($qName->getNameSpaceURI()) . '}' . strval($qName->getLocalName());
if (array_key_exists($key, $this->registry) === true) {
Assert::implementsInterface($this->registry[$key], ElementInterface::class);
return $this->registry[$key];
@@ -99,16 +112,10 @@ public function getElementHandler(QNameValue $qName): ?string
*/
public function getExtensionHandler(QNameValue $qName): ?string
{
$prefix = $qName->getNamespacePrefix()->getValue();
$namespaceURI = $qName->getNamespaceURI()->getValue();

if ($namespaceURI !== null) {
$key = $qName->getRawValue();
if (array_key_exists($key, $this->registry) === true) {
Assert::implementsInterface($this->registry[$key], ExtensionPointInterface::class);
return $this->registry[$key];
}
return null;
$key = '{' . strval($qName->getNameSpaceURI()) . '}' . strval($qName->getLocalName());
if (array_key_exists($key, $this->extRegistry) === true) {
Assert::implementsInterface($this->extRegistry[$key], ExtensionPointInterface::class);
return $this->extRegistry[$key];
}

return null;