Skip to content

Commit 9b3ef52

Browse files
committed
Import generic helper-class for list-of-anyuri from saml2
1 parent 23c1fd6 commit 9b3ef52

File tree

2 files changed

+128
-0
lines changed

2 files changed

+128
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XMLSchema\Type\Helper;
6+
7+
use SimpleSAML\XML\Assert\Assert;
8+
use SimpleSAML\XML\Constants as C;
9+
use SimpleSAML\XMLSchema\Exception\SchemaViolationException;
10+
use SimpleSAML\XMLSchema\Type\AnyURIValue;
11+
use SimpleSAML\XMLSchema\Type\Interface\ListTypeInterface;
12+
13+
use function array_map;
14+
use function preg_split;
15+
use function str_replace;
16+
use function trim;
17+
18+
/**
19+
* @package simplesaml/xml-common
20+
*/
21+
class AnyURIListValue extends AnyURIValue implements ListTypeInterface
22+
{
23+
/**
24+
* Validate the value.
25+
*
26+
* @param string $value
27+
* @throws \SimpleSAML\XMLSchema\Exception\SchemaViolationException on failure
28+
* @return void
29+
*/
30+
protected function validateValue(string $value): void
31+
{
32+
$sanitized = $this->sanitizeValue($value);
33+
$uris = preg_split('/[\s]+/', $sanitized, C::UNBOUNDED_LIMIT);
34+
Assert::allValidAnyURI($uris, SchemaViolationException::class);
35+
}
36+
37+
38+
/**
39+
* Convert an array of xs:anyURI items into an AnyURIListValue
40+
*
41+
* @param string[] $uris
42+
* @return static
43+
*/
44+
public static function fromArray(array $uris): static
45+
{
46+
$str = '';
47+
foreach ($uris as $uri) {
48+
$str .= str_replace(' ', '+', $uri) . ' ';
49+
}
50+
51+
return new static(trim($str));
52+
}
53+
54+
55+
/**
56+
* Convert this AnyURIList into an array of xs:anyURI items
57+
*
58+
* @return array<\SimpleSAML\XMLSchema\Type\AnyURIValue>
59+
*/
60+
public function toArray(): array
61+
{
62+
$uris = preg_split('/[\s]+/', $this->getValue(), C::UNBOUNDED_LIMIT);
63+
$uris = str_replace('+', ' ', $uris);
64+
65+
return array_map([AnyURIValue::class, 'fromString'], $uris);
66+
}
67+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\Test\XMLSchema\Type\Helper;
6+
7+
use PHPUnit\Framework\Attributes\CoversClass;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use PHPUnit\Framework\Attributes\Group;
10+
use PHPUnit\Framework\TestCase;
11+
use SimpleSAML\XMLSchema\Exception\SchemaViolationException;
12+
use SimpleSAML\XMLSchema\Type\Helper\AnyURIListValue;
13+
14+
/**
15+
* Class \SimpleSAML\Test\XMLSchema\Type\Helper\AnyURIListTest
16+
*
17+
* @package simplesamlphp/xml-common
18+
*/
19+
#[Group('type')]
20+
#[CoversClass(AnyURIListValue::class)]
21+
final class AnyURIListValueTest extends TestCase
22+
{
23+
/**
24+
* @param boolean $shouldPass
25+
* @param string $anyURIList
26+
*/
27+
#[DataProvider('provideAnyURIList')]
28+
public function testAnyURIList(bool $shouldPass, string $anyURIList): void
29+
{
30+
try {
31+
AnyURIListValue::fromString($anyURIList);
32+
$this->assertTrue($shouldPass);
33+
} catch (SchemaViolationException $e) {
34+
$this->assertFalse($shouldPass);
35+
}
36+
}
37+
38+
39+
/**
40+
* Test the toArray function
41+
*/
42+
public function testToArray(): void
43+
{
44+
$anyURIList = AnyURIListValue::fromString("urn:x-simplesamlphp:namespace urn:x-ssp:ns");
45+
$this->assertEquals(['urn:x-simplesamlphp:namespace', 'urn:x-ssp:ns'], $anyURIList->toArray());
46+
}
47+
48+
49+
/**
50+
* @return array<array{0: bool, 1: string}>
51+
*/
52+
public static function provideAnyURIList(): array
53+
{
54+
return [
55+
'single' => [true, "urn:x-simplesamlphp:namespace"],
56+
'multiple' => [true, 'urn:x-simplesamlphp:namespace urn:x-ssp:ns'],
57+
'normalization' => [true, "urn:x-simplesamlphp:namespace \n urn:x-ssp:ns"],
58+
'empty' => [true, ''],
59+
];
60+
}
61+
}

0 commit comments

Comments
 (0)