Skip to content

Commit

Permalink
Restructure getAttribute/getOptionalAttribute
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdijen committed Jan 15, 2025
1 parent c2b9b8e commit 81153a2
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 290 deletions.
120 changes: 22 additions & 98 deletions src/AbstractElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use SimpleSAML\XML\Exception\MissingAttributeException;
use SimpleSAML\XML\Exception\SchemaViolationException;
use SimpleSAML\XML\SerializableElementTrait;
use SimpleSAML\XML\Type\{StringValue, ValueTypeInterface};

use function array_slice;
use function defined;
Expand Down Expand Up @@ -61,22 +62,29 @@ public function instantiateParentElement(?DOMElement $parent = null): DOMElement
*
* @param \DOMElement $xml The element where we should search for the attribute.
* @param string $name The name of the attribute.
* @return string
* @param string $type The type of the attribute value.
* @return \SimpleSAML\XML\Type\ValueTypeInterface
*
* @throws \SimpleSAML\XML\Exception\MissingAttributeException if the attribute is missing from the element
*/
public static function getAttribute(DOMElement $xml, string $name): string
{
public static function getAttribute(
DOMElement $xml,
string $name,
string $type = StringValue::class,
): ValueTypeInterface {
Assert::isAOf($type, ValueTypeInterface::class);

$prefix = static::getNamespacePrefix();
$localName = static::getLocalName();
$qName = $prefix ? ($prefix . ':' . $localName) : $localName;
Assert::true(
$xml->hasAttribute($name) && func_num_args() === 2,
$xml->hasAttribute($name),
sprintf('Missing \'%s\' attribute on %s.', $name, $qName),
MissingAttributeException::class,
);

return $xml->getAttribute($name);
$value = $xml->getAttribute($name);
return $type::fromString($value);
}


Expand All @@ -85,105 +93,21 @@ public static function getAttribute(DOMElement $xml, string $name): string
*
* @param \DOMElement $xml The element where we should search for the attribute.
* @param string $name The name of the attribute.
* @param string $type The type of the attribute value.
* @param string|null $default The default to return in case the attribute does not exist and it is optional.
* @return ($default is string ? string : string|null)
*/
public static function getOptionalAttribute(DOMElement $xml, string $name, ?string $default = null): ?string
{
if (!$xml->hasAttribute($name)) {
return $default;
}

return self::getAttribute($xml, $name);
}


/**
* @param \DOMElement $xml The element where we should search for the attribute.
* @param string $name The name of the attribute.
* @return bool
*
* @throws \SimpleSAML\XML\Exception\MissingAttributeException if the attribute is missing from the element
* @throws \SimpleSAML\Assert\AssertionFailedException if the attribute is not a boolean
*/
public static function getBooleanAttribute(DOMElement $xml, string $name): bool
{
$value = self::getAttribute($xml, $name);

$prefix = static::getNamespacePrefix();
$localName = static::getLocalName();
$qName = $prefix ? ($prefix . ':' . $localName) : $localName;
Assert::oneOf(
$value,
['0', '1', 'false', 'true'],
sprintf('The \'%s\' attribute of %s must be a boolean.', $name, $qName),
);

return in_array($value, ['1', 'true'], true);
}


/**
* @param \DOMElement $xml The element where we should search for the attribute.
* @param string $name The name of the attribute.
* @param bool|null $default The default to return in case the attribute does not exist and it is optional.
* @return ($default is bool ? bool : bool|null)
*
* @throws \SimpleSAML\Assert\AssertionFailedException if the attribute is not a boolean
* @return ($default is \SimpleSAML\XML\Type\ValueTypeInterface ? \SimpleSAML\XML\Type\ValueTypeInterface : \SimpleSAML\XML\Type\ValueTypeInterface|null)
*/
public static function getOptionalBooleanAttribute(DOMElement $xml, string $name, ?bool $default = null): ?bool
{
if (!$xml->hasAttribute($name)) {
return $default;
}

return self::getBooleanAttribute($xml, $name);
}


/**
* Get the integer value of an attribute from a given element.
*
* @param \DOMElement $xml The element where we should search for the attribute.
* @param string $name The name of the attribute.
* @return int
*
* @throws \SimpleSAML\XML\Exception\MissingAttributeException if the attribute is missing from the element
* @throws \SimpleSAML\Assert\AssertionFailedException if the attribute is not an integer
*/
public static function getIntegerAttribute(DOMElement $xml, string $name): int
{
$value = self::getAttribute($xml, $name);

$prefix = static::getNamespacePrefix();
$localName = static::getLocalName();
$qName = $prefix ? ($prefix . ':' . $localName) : $localName;
Assert::numeric(
$value,
sprintf('The \'%s\' attribute of %s must be numerical.', $name, $qName),
);

return intval($value);
}


/**
* Get the integer value of an attribute from a given element.
*
* @param \DOMElement $xml The element where we should search for the attribute.
* @param string $name The name of the attribute.
* @param int|null $default The default to return in case the attribute does not exist and it is optional.
* @return ($default is int ? int : int|null)
*
* @throws \SimpleSAML\Assert\AssertionFailedException if the attribute is not an integer
*/
public static function getOptionalIntegerAttribute(DOMElement $xml, string $name, ?int $default = null): ?int
{
public static function getOptionalAttribute(
DOMElement $xml,
string $name,
string $type = StringValue::class,
?ValueTypeInterface $default = null,
): ?ValueTypeInterface {
if (!$xml->hasAttribute($name)) {
return $default;
}

return self::getIntegerAttribute($xml, $name);
return self::getAttribute($xml, $name, $type);
}


Expand Down
116 changes: 25 additions & 91 deletions src/Chunk.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use SimpleSAML\XML\Exception\MissingAttributeException;
use SimpleSAML\XML\Exception\SchemaViolationException;
use SimpleSAML\XML\SerializableElementTrait;
use SimpleSAML\XML\Type\{StringValue, ValueTypeInterface};

use function in_array;
use function intval;
Expand Down Expand Up @@ -158,120 +159,53 @@ public function getQualifiedName(): string


/**
* Get the value of an attribute from a given element.
*
* @param \DOMElement $xml The element where we should search for the attribute.
* @param string $name The name of the attribute.
* @return string
* @param string $type The type of the attribute value.
* @return \SimpleSAML\XML\Type\ValueTypeInterface
*
* @throws \SimpleSAML\XML\Exception\MissingAttributeException if the attribute is missing from the element
*/
public static function getAttribute(DOMElement $xml, string $name): string
{
public static function getAttribute(
DOMElement $xml,
string $name,
string $type = StringValue::class,
): ValueTypeInterface {
Assert::isAOf($type, ValueTypeInterface::class);

Assert::true(
$xml->hasAttribute($name),
'Missing \'' . $name . '\' attribute on ' . $xml->prefix . ':' . $xml->localName . '.',
MissingAttributeException::class,
);

return $xml->getAttribute($name);
$value = $xml->getAttribute($name);
return $type::fromString($value);
}


/**
* @param \DOMElement $xml The element where we should search for the attribute.
* @param string $name The name of the attribute.
* @param string|null $default The default to return in case the attribute does not exist and it is optional.
* @return ($default is string ? string : null)
*/
public static function getOptionalAttribute(DOMElement $xml, string $name, ?string $default = null): ?string
{
if (!$xml->hasAttribute($name)) {
return $default;
}

return $xml->getAttribute($name);
}


/**
* @param \DOMElement $xml The element where we should search for the attribute.
* @param string $name The name of the attribute.
* @return bool
* Get the value of an attribute from a given element.
*
* @throws \SimpleSAML\XML\Exception\MissingAttributeException if the attribute is missing from the element
* @throws \SimpleSAML\Assert\AssertionFailedException if the attribute is not a boolean
*/
public static function getBooleanAttribute(DOMElement $xml, string $name): bool
{
$value = self::getAttribute($xml, $name);

Assert::oneOf(
$value,
['0', '1', 'false', 'true'],
'The \'' . $name . '\' attribute of ' . $xml->prefix . ':' . $xml->localName . ' must be boolean.',
);

return in_array($value, ['1', 'true'], true);
}


/**
* @param \DOMElement $xml The element where we should search for the attribute.
* @param string $name The name of the attribute.
* @param bool|null $default The default to return in case the attribute does not exist and it is optional.
* @return ($default is bool ? bool : null)
*
* @throws \SimpleSAML\Assert\AssertionFailedException if the attribute is not a boolean
*/
public static function getOptionalBooleanAttribute(DOMElement $xml, string $name, ?bool $default = null): ?bool
{
if (!$xml->hasAttribute($name)) {
return $default;
}

return self::getBooleanAttribute($xml, $name);
}


/**
* Get the integer value of an attribute from a given element.
*
* @param \DOMElement $xml The element where we should search for the attribute.
* @param string $name The name of the attribute.
* @return int
*
* @throws \SimpleSAML\XML\Exception\MissingAttributeException if the attribute is missing from the element
* @throws \SimpleSAML\Assert\AssertionFailedException if the attribute is not an integer
*/
public static function getIntegerAttribute(DOMElement $xml, string $name): int
{
$value = self::getAttribute($xml, $name);

Assert::numeric(
$value,
'The \'' . $name . '\' attribute of ' . $xml->prefix . ':' . $xml->localName . ' must be numerical.',
);

return intval($value);
}


/**
* Get the integer value of an attribute from a given element.
*
* @param \DOMElement $xml The element where we should search for the attribute.
* @param string $name The name of the attribute.
* @param int|null $default The default to return in case the attribute does not exist and it is optional.
* @return ($default is int ? int : null)
*
* @throws \SimpleSAML\Assert\AssertionFailedException if the attribute is not an integer
* @param string $type The type of the attribute value.
* @param string|null $default The default to return in case the attribute does not exist and it is optional.
* @return ($default is \SimpleSAML\XML\Type\ValueTypeInterface ? \SimpleSAML\XML\Type\ValueTypeInterface : \SimpleSAML\XML\Type\ValueTypeInterface|null)
*/
public static function getOptionalIntegerAttribute(DOMElement $xml, string $name, ?int $default = null): ?int
{
public static function getOptionalAttribute(
DOMElement $xml,
string $name,
string $type = StringValue::class,
?ValueTypeInterface $default = null,
): ?ValueTypeInterface {
if (!$xml->hasAttribute($name)) {
return $default;
}

return self::getIntegerAttribute($xml, $name);
return self::getAttribute($xml, $name, $type);
}


Expand Down
55 changes: 7 additions & 48 deletions src/ElementInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace SimpleSAML\XML;

use DOMElement;
use SimpleSAML\XML\Type\{StringValue, ValueTypeInterface};

/**
* interface class to be implemented by all the classes that represent an XML element
Expand All @@ -26,64 +27,22 @@ public function getQualifiedName(): string;
*
* @param \DOMElement $xml The element where we should search for the attribute.
* @param string $name The name of the attribute.
* @return string
* @param string $type The type of the attribute value
* @return \SimpleSAML\XML\ValueTypeInterface
*
* @throws \SimpleSAML\XML\Exception\MissingAttributeException if the attribute is missing from the element
*/
public static function getAttribute(DOMElement $xml, string $name): string;
public static function getAttribute(DOMElement $xml, string $name, string $type = StringValue::class): ValueTypeInterface;


/**
* Get the value of an attribute from a given element.
*
* @param \DOMElement $xml The element where we should search for the attribute.
* @param string $name The name of the attribute.
* @param string $type The type of the attribute value
* @param string|null $default The default to return in case the attribute does not exist and it is optional.
* @return string|null
*/
public static function getOptionalAttribute(DOMElement $xml, string $name, ?string $default = null): ?string;


/**
* @param \DOMElement $xml The element where we should search for the attribute.
* @param string $name The name of the attribute.
* @return bool
*
* @throws \SimpleSAML\XML\Exception\MissingAttributeException if the attribute is missing from the element
* @throws \SimpleSAML\Assert\AssertionFailedException if the attribute is not a boolean
*/
public static function getBooleanAttribute(DOMElement $xml, string $name): bool;


/**
* @param \DOMElement $xml The element where we should search for the attribute.
* @param string $name The name of the attribute.
* @param bool|null $default The default to return in case the attribute does not exist and it is optional.
* @return bool|null
*
* @throws \SimpleSAML\Assert\AssertionFailedException if the attribute is not a boolean
*/
public static function getOptionalBooleanAttribute(DOMElement $xml, string $name, ?bool $default = null): ?bool;


/**
* @param \DOMElement $xml The element where we should search for the attribute.
* @param string $name The name of the attribute.
* @return int
*
* @throws \SimpleSAML\XML\Exception\MissingAttributeException if the attribute is missing from the element
* @throws \SimpleSAML\Assert\AssertionFailedException if the attribute is not an integer
*/
public static function getIntegerAttribute(DOMElement $xml, string $name): int;


/**
* @param \DOMElement $xml The element where we should search for the attribute.
* @param string $name The name of the attribute.
* @param int|null $default The default to return in case the attribute does not exist and it is optional.
* @return int|null
*
* @throws \SimpleSAML\Assert\AssertionFailedException if the attribute is not an integer
* @return \SimpleSAML\XML\Type\ValueInterfaceType|null
*/
public static function getOptionalIntegerAttribute(DOMElement $xml, string $name, ?int $default = null): ?int;
public static function getOptionalAttribute(DOMElement $xml, string $name, string $type = StringValue::class, ?ValueTypeInterface $default = null): ?ValueTypeInterface;
}
Loading

0 comments on commit 81153a2

Please sign in to comment.