Skip to content

Commit b5541c7

Browse files
committed
Properly compare AnyURI-values by specification
1 parent 07ec972 commit b5541c7

3 files changed

Lines changed: 31 additions & 0 deletions

File tree

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"ext-pcre": "*",
4343
"ext-spl": "*",
4444

45+
"guzzlehttp/psr7": "~2.8",
4546
"psr/clock": "~1.0",
4647
"simplesamlphp/assert": "~1.9",
4748
"simplesamlphp/composer-xmlprovider-installer": "~1.2"

src/XMLSchema/Type/AnyURIValue.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44

55
namespace SimpleSAML\XMLSchema\Type;
66

7+
use GuzzleHttp\Psr7\Uri;
8+
use GuzzleHttp\Psr7\UriNormalizer;
79
use SimpleSAML\XML\Assert\Assert;
810
use SimpleSAML\XMLSchema\Exception\SchemaViolationException;
911
use SimpleSAML\XMLSchema\Type\Interface\AbstractAnySimpleType;
12+
use SimpleSAML\XMLSchema\Type\Interface\ValueTypeInterface;
1013

1114
/**
1215
* @package simplesaml/xml-common
@@ -37,4 +40,23 @@ protected function validateValue(string $value): void
3740
{
3841
Assert::validAnyURI($value, SchemaViolationException::class);
3942
}
43+
44+
45+
/**
46+
* Compare the value to another one
47+
*
48+
* @param \SimpleSAML\XMLSchema\Type\Interface\ValueTypeInterface|string $other
49+
* @return bool
50+
*/
51+
public function equals(ValueTypeInterface|string $other): bool
52+
{
53+
if (is_string($other)) {
54+
$other = static::fromString($other);
55+
}
56+
57+
$selfUri = new Uri($this->getValue());
58+
$otherUri = new Uri($other->getValue());
59+
60+
return UriNormalizer::isEquivalent($selfUri, $otherUri);
61+
}
4062
}

tests/XMLSchema/Type/AnyURIValueTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,19 @@ public function testEquals(): void
4848
$this->assertTrue(AnyURIValue::fromString('hello')->equals(AnyURIValue::fromString('hello')));
4949
$this->assertTrue(AnyURIValue::fromString('hello')->equals(StringValue::fromString('hello')));
5050
$this->assertTrue(AnyURIValue::fromString('hello')->equals('hello'));
51+
$this->assertTrue(
52+
AnyURIValue::fromString('https://simplesamlphp.org/index.html')
53+
->equals('https://simplesamlphp.org:443/index.html'),
54+
);
5155

5256
// Assert that two different values are not equal
5357
$this->assertFalse(AnyURIValue::fromString('hello')->equals(AnyURIValue::fromString('world')));
5458
$this->assertFalse(AnyURIValue::fromString('hello')->equals(StringValue::fromString('world')));
5559
$this->assertFalse(AnyURIValue::fromString('hello')->equals('world'));
60+
$this->assertFalse(
61+
AnyURIValue::fromString('https://simplesamlphp.org:8443/index.html')
62+
->equals('https://simplesamlphp.org:443/index.html'),
63+
);
5664
}
5765

5866

0 commit comments

Comments
 (0)