From fcf9a3566103fb5f46bb22cedc8f01d2d49f3052 Mon Sep 17 00:00:00 2001 From: Tim van Dijen Date: Sat, 18 Jan 2025 16:31:04 +0100 Subject: [PATCH] Add toArray for list-types --- src/Type/EntitiesValue.php | 15 ++++++++++++++- src/Type/IDRefsValue.php | 15 ++++++++++++++- src/Type/ListTypeInterface.php | 20 ++++++++++++++++++++ src/Type/NMTokensValue.php | 18 +++++++++++++++++- tests/Type/EntitiesValueTest.php | 10 ++++++++++ tests/Type/IDRefsValueTest.php | 10 ++++++++++ tests/Type/NMTokenValueTest.php | 2 +- tests/Type/NMTokensValueTest.php | 12 +++++++++++- 8 files changed, 97 insertions(+), 5 deletions(-) create mode 100644 src/Type/ListTypeInterface.php diff --git a/src/Type/EntitiesValue.php b/src/Type/EntitiesValue.php index 05c00146..49f8bcc5 100644 --- a/src/Type/EntitiesValue.php +++ b/src/Type/EntitiesValue.php @@ -5,12 +5,13 @@ namespace SimpleSAML\XML\Type; use SimpleSAML\XML\Assert\Assert; +use SimpleSAML\XML\Constants as C; use SimpleSAML\XML\Exception\SchemaViolationException; /** * @package simplesaml/xml-common */ -class EntitiesValue extends TokenValue +class EntitiesValue extends TokenValue implements ListTypeInterface { /** * Validate the value. @@ -24,4 +25,16 @@ protected function validateValue(string $value): void // Note: value must already be sanitized before validating Assert::validEntities($this->sanitizeValue($value), SchemaViolationException::class); } + + + /** + * Convert this xs:ENTITIES to an array of xs:ENTITY items + * + * @return array<\SimpleSAML\XML\Type\EntityValue> + */ + public function toArray(): array + { + $tokens = explode(' ', $this->getValue(), C::UNBOUNDED_LIMIT); + return array_map([EntityValue::class, 'fromString'], $tokens); + } } diff --git a/src/Type/IDRefsValue.php b/src/Type/IDRefsValue.php index b4891817..b2fe3ad6 100644 --- a/src/Type/IDRefsValue.php +++ b/src/Type/IDRefsValue.php @@ -5,12 +5,13 @@ namespace SimpleSAML\XML\Type; use SimpleSAML\XML\Assert\Assert; +use SimpleSAML\XML\Constants as C; use SimpleSAML\XML\Exception\SchemaViolationException; /** * @package simplesaml/xml-common */ -class IDRefsValue extends TokenValue +class IDRefsValue extends TokenValue implements ListTypeInterface { /** * Validate the value. @@ -24,4 +25,16 @@ protected function validateValue(string $value): void // Note: value must already be sanitized before validating Assert::validIDRefs($this->sanitizeValue($value), SchemaViolationException::class); } + + + /** + * Convert this xs:IDREFS to an array of xs:IDREF items + * + * @return array<\SimpleSAML\XML\Type\IDRefValue> + */ + public function toArray(): array + { + $tokens = explode(' ', $this->getValue(), C::UNBOUNDED_LIMIT); + return array_map([IDRefValue::class, 'fromString'], $tokens); + } } diff --git a/src/Type/ListTypeInterface.php b/src/Type/ListTypeInterface.php new file mode 100644 index 00000000..545d41c0 --- /dev/null +++ b/src/Type/ListTypeInterface.php @@ -0,0 +1,20 @@ + + */ + public function toArray(): array; +} diff --git a/src/Type/NMTokensValue.php b/src/Type/NMTokensValue.php index 71c8394e..7d407721 100644 --- a/src/Type/NMTokensValue.php +++ b/src/Type/NMTokensValue.php @@ -5,12 +5,16 @@ namespace SimpleSAML\XML\Type; use SimpleSAML\XML\Assert\Assert; +use SimpleSAML\XML\Constants as C; use SimpleSAML\XML\Exception\SchemaViolationException; +use function array_map; +use function explode; + /** * @package simplesaml/xml-common */ -class NMTokensValue extends TokenValue +class NMTokensValue extends TokenValue implements ListTypeInterface { /** * Validate the value. @@ -24,4 +28,16 @@ protected function validateValue(string $value): void // Note: value must already be sanitized before validating Assert::validNMTokens($this->sanitizeValue($value), SchemaViolationException::class); } + + + /** + * Convert this xs:NMTokens to an array of xs:NMToken items + * + * @return array<\SimpleSAML\XML\Type\NMTokenValue> + */ + public function toArray(): array + { + $tokens = explode(' ', $this->getValue(), C::UNBOUNDED_LIMIT); + return array_map([NMTokenValue::class, 'fromString'], $tokens); + } } diff --git a/tests/Type/EntitiesValueTest.php b/tests/Type/EntitiesValueTest.php index a50ab145..5c01f47a 100644 --- a/tests/Type/EntitiesValueTest.php +++ b/tests/Type/EntitiesValueTest.php @@ -53,4 +53,14 @@ public static function provideEntities(): array 'normalization' => [true, ' foobar '], ]; } + + + /** + * Test the toArray function + */ + public function testToArray(): void + { + $entities = EntitiesValue::fromString("foo \nbar baz"); + $this->assertEquals(['foo', 'bar', 'baz'], $entities->toArray()); + } } diff --git a/tests/Type/IDRefsValueTest.php b/tests/Type/IDRefsValueTest.php index 447c2c24..c87d4852 100644 --- a/tests/Type/IDRefsValueTest.php +++ b/tests/Type/IDRefsValueTest.php @@ -54,4 +54,14 @@ public static function provideIDRefs(): array 'normalization' => [true, ' foobar '], ]; } + + + /** + * Test the toArray function + */ + public function testToArray(): void + { + $idrefs = IDRefsValue::fromString("foo \nbar baz"); + $this->assertEquals(['foo', 'bar', 'baz'], $idrefs->toArray()); + } } diff --git a/tests/Type/NMTokenValueTest.php b/tests/Type/NMTokenValueTest.php index eee8f588..ceeebc13 100644 --- a/tests/Type/NMTokenValueTest.php +++ b/tests/Type/NMTokenValueTest.php @@ -23,7 +23,7 @@ final class NMTokenValueTest extends TestCase * @param string $nmtoken */ #[DataProvider('provideNMToken')] - public function testName(bool $shouldPass, string $nmtoken): void + public function testNMToken(bool $shouldPass, string $nmtoken): void { try { NMTokenValue::fromString($nmtoken); diff --git a/tests/Type/NMTokensValueTest.php b/tests/Type/NMTokensValueTest.php index d933c9e0..20c23de1 100644 --- a/tests/Type/NMTokensValueTest.php +++ b/tests/Type/NMTokensValueTest.php @@ -23,7 +23,7 @@ final class NMTokensValueTest extends TestCase * @param string $nmtokens */ #[DataProvider('provideNMTokens')] - public function testName(bool $shouldPass, string $nmtokens): void + public function testNMtokens(bool $shouldPass, string $nmtokens): void { try { NMTokensValue::fromString($nmtokens); @@ -50,4 +50,14 @@ public static function provideNMTokens(): array 'normalization' => [true, ' foobar nmtoken '], ]; } + + + /** + * Test the toArray function + */ + public function testToArray(): void + { + $nmtokens = NMTokensValue::fromString("foo \nbar baz"); + $this->assertEquals(['foo', 'bar', 'baz'], $nmtokens->toArray()); + } }