Skip to content

Commit fcf9a35

Browse files
committed
Add toArray for list-types
1 parent 219e793 commit fcf9a35

File tree

8 files changed

+97
-5
lines changed

8 files changed

+97
-5
lines changed

src/Type/EntitiesValue.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
namespace SimpleSAML\XML\Type;
66

77
use SimpleSAML\XML\Assert\Assert;
8+
use SimpleSAML\XML\Constants as C;
89
use SimpleSAML\XML\Exception\SchemaViolationException;
910

1011
/**
1112
* @package simplesaml/xml-common
1213
*/
13-
class EntitiesValue extends TokenValue
14+
class EntitiesValue extends TokenValue implements ListTypeInterface
1415
{
1516
/**
1617
* Validate the value.
@@ -24,4 +25,16 @@ protected function validateValue(string $value): void
2425
// Note: value must already be sanitized before validating
2526
Assert::validEntities($this->sanitizeValue($value), SchemaViolationException::class);
2627
}
28+
29+
30+
/**
31+
* Convert this xs:ENTITIES to an array of xs:ENTITY items
32+
*
33+
* @return array<\SimpleSAML\XML\Type\EntityValue>
34+
*/
35+
public function toArray(): array
36+
{
37+
$tokens = explode(' ', $this->getValue(), C::UNBOUNDED_LIMIT);
38+
return array_map([EntityValue::class, 'fromString'], $tokens);
39+
}
2740
}

src/Type/IDRefsValue.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
namespace SimpleSAML\XML\Type;
66

77
use SimpleSAML\XML\Assert\Assert;
8+
use SimpleSAML\XML\Constants as C;
89
use SimpleSAML\XML\Exception\SchemaViolationException;
910

1011
/**
1112
* @package simplesaml/xml-common
1213
*/
13-
class IDRefsValue extends TokenValue
14+
class IDRefsValue extends TokenValue implements ListTypeInterface
1415
{
1516
/**
1617
* Validate the value.
@@ -24,4 +25,16 @@ protected function validateValue(string $value): void
2425
// Note: value must already be sanitized before validating
2526
Assert::validIDRefs($this->sanitizeValue($value), SchemaViolationException::class);
2627
}
28+
29+
30+
/**
31+
* Convert this xs:IDREFS to an array of xs:IDREF items
32+
*
33+
* @return array<\SimpleSAML\XML\Type\IDRefValue>
34+
*/
35+
public function toArray(): array
36+
{
37+
$tokens = explode(' ', $this->getValue(), C::UNBOUNDED_LIMIT);
38+
return array_map([IDRefValue::class, 'fromString'], $tokens);
39+
}
2740
}

src/Type/ListTypeInterface.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XML\Type;
6+
7+
/**
8+
* interface class to be implemented by all the classes that represent a list type
9+
*
10+
* @package simplesamlphp/xml-common
11+
*/
12+
interface ListTypeInterface extends ValueTypeInterface
13+
{
14+
/**
15+
* Convert this list type to an array of individual items
16+
*
17+
* @return array<\SimpleSAML\XML\Type\ValueTypeInterface>
18+
*/
19+
public function toArray(): array;
20+
}

src/Type/NMTokensValue.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@
55
namespace SimpleSAML\XML\Type;
66

77
use SimpleSAML\XML\Assert\Assert;
8+
use SimpleSAML\XML\Constants as C;
89
use SimpleSAML\XML\Exception\SchemaViolationException;
910

11+
use function array_map;
12+
use function explode;
13+
1014
/**
1115
* @package simplesaml/xml-common
1216
*/
13-
class NMTokensValue extends TokenValue
17+
class NMTokensValue extends TokenValue implements ListTypeInterface
1418
{
1519
/**
1620
* Validate the value.
@@ -24,4 +28,16 @@ protected function validateValue(string $value): void
2428
// Note: value must already be sanitized before validating
2529
Assert::validNMTokens($this->sanitizeValue($value), SchemaViolationException::class);
2630
}
31+
32+
33+
/**
34+
* Convert this xs:NMTokens to an array of xs:NMToken items
35+
*
36+
* @return array<\SimpleSAML\XML\Type\NMTokenValue>
37+
*/
38+
public function toArray(): array
39+
{
40+
$tokens = explode(' ', $this->getValue(), C::UNBOUNDED_LIMIT);
41+
return array_map([NMTokenValue::class, 'fromString'], $tokens);
42+
}
2743
}

tests/Type/EntitiesValueTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,14 @@ public static function provideEntities(): array
5353
'normalization' => [true, ' foobar '],
5454
];
5555
}
56+
57+
58+
/**
59+
* Test the toArray function
60+
*/
61+
public function testToArray(): void
62+
{
63+
$entities = EntitiesValue::fromString("foo \nbar baz");
64+
$this->assertEquals(['foo', 'bar', 'baz'], $entities->toArray());
65+
}
5666
}

tests/Type/IDRefsValueTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,14 @@ public static function provideIDRefs(): array
5454
'normalization' => [true, ' foobar '],
5555
];
5656
}
57+
58+
59+
/**
60+
* Test the toArray function
61+
*/
62+
public function testToArray(): void
63+
{
64+
$idrefs = IDRefsValue::fromString("foo \nbar baz");
65+
$this->assertEquals(['foo', 'bar', 'baz'], $idrefs->toArray());
66+
}
5767
}

tests/Type/NMTokenValueTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ final class NMTokenValueTest extends TestCase
2323
* @param string $nmtoken
2424
*/
2525
#[DataProvider('provideNMToken')]
26-
public function testName(bool $shouldPass, string $nmtoken): void
26+
public function testNMToken(bool $shouldPass, string $nmtoken): void
2727
{
2828
try {
2929
NMTokenValue::fromString($nmtoken);

tests/Type/NMTokensValueTest.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ final class NMTokensValueTest extends TestCase
2323
* @param string $nmtokens
2424
*/
2525
#[DataProvider('provideNMTokens')]
26-
public function testName(bool $shouldPass, string $nmtokens): void
26+
public function testNMtokens(bool $shouldPass, string $nmtokens): void
2727
{
2828
try {
2929
NMTokensValue::fromString($nmtokens);
@@ -50,4 +50,14 @@ public static function provideNMTokens(): array
5050
'normalization' => [true, ' foobar nmtoken '],
5151
];
5252
}
53+
54+
55+
/**
56+
* Test the toArray function
57+
*/
58+
public function testToArray(): void
59+
{
60+
$nmtokens = NMTokensValue::fromString("foo \nbar baz");
61+
$this->assertEquals(['foo', 'bar', 'baz'], $nmtokens->toArray());
62+
}
5363
}

0 commit comments

Comments
 (0)