Skip to content

Commit

Permalink
Add toArray for list-types
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdijen committed Jan 18, 2025
1 parent 219e793 commit fcf9a35
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 5 deletions.
15 changes: 14 additions & 1 deletion src/Type/EntitiesValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
}
}
15 changes: 14 additions & 1 deletion src/Type/IDRefsValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
}
}
20 changes: 20 additions & 0 deletions src/Type/ListTypeInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\XML\Type;

/**
* interface class to be implemented by all the classes that represent a list type
*
* @package simplesamlphp/xml-common
*/
interface ListTypeInterface extends ValueTypeInterface
{
/**
* Convert this list type to an array of individual items
*
* @return array<\SimpleSAML\XML\Type\ValueTypeInterface>
*/
public function toArray(): array;
}
18 changes: 17 additions & 1 deletion src/Type/NMTokensValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
}
}
10 changes: 10 additions & 0 deletions tests/Type/EntitiesValueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
10 changes: 10 additions & 0 deletions tests/Type/IDRefsValueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
2 changes: 1 addition & 1 deletion tests/Type/NMTokenValueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 11 additions & 1 deletion tests/Type/NMTokensValueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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());
}
}

0 comments on commit fcf9a35

Please sign in to comment.