diff --git a/src/SAML2/XML/mdrpi/PublicationInfo.php b/src/SAML2/XML/mdrpi/PublicationInfo.php index fd105dd94..093f42cb2 100644 --- a/src/SAML2/XML/mdrpi/PublicationInfo.php +++ b/src/SAML2/XML/mdrpi/PublicationInfo.php @@ -4,6 +4,7 @@ namespace SimpleSAML\SAML2\XML\mdrpi; +use DateTimeImmutable; use DOMElement; use SimpleSAML\Assert\Assert; use SimpleSAML\SAML2\Constants as C; @@ -12,7 +13,6 @@ use SimpleSAML\SAML2\XML\md\AbstractLocalizedURI; use SimpleSAML\XML\ArrayizableElementInterface; use SimpleSAML\XML\Exception\InvalidDOMElementException; -use SimpleSAML\XML\Utils as XMLUtils; use function array_change_key_case; use function array_keys; @@ -30,16 +30,18 @@ final class PublicationInfo extends AbstractMdrpiElement implements ArrayizableE * Create/parse a mdrpi:PublicationInfo element. * * @param string $publisher - * @param int|null $creationInstant + * @param \DateTimeImmutable|null $creationInstant * @param string|null $publicationId * @param \SimpleSAML\SAML2\XML\mdrpi\UsagePolicy[] $usagePolicy */ public function __construct( protected string $publisher, - protected ?int $creationInstant = null, + protected ?DateTimeImmutable $creationInstant = null, protected ?string $publicationId = null, protected array $usagePolicy = [], ) { + Assert::nullOrSame($creationInstant?->getTimeZone()->getName(), 'Z', ProtocolViolationException::class); + Assert::maxCount($usagePolicy, C::UNBOUNDED_LIMIT); Assert::allIsInstanceOf($usagePolicy, UsagePolicy::class); /** @@ -75,9 +77,9 @@ public function getPublisher(): string /** * Collect the value of the creationInstant-property * - * @return int|null + * @return \DateTimeImmutable|null */ - public function getCreationInstant(): ?int + public function getCreationInstant(): ?DateTimeImmutable { return $this->creationInstant; } @@ -130,7 +132,7 @@ public static function fromXML(DOMElement $xml): static $creationInstant = preg_replace('/([.][0-9]+Z)$/', 'Z', $creationInstant, 1); Assert::validDateTimeZulu($creationInstant, ProtocolViolationException::class); - $creationInstant = XMLUtils::xsDateTimeToTimestamp($creationInstant); + $creationInstant = new DateTimeImmutable($creationInstant); } $publicationId = self::getOptionalAttribute($xml, 'publicationId', null); @@ -152,7 +154,7 @@ public function toXML(DOMElement $parent = null): DOMElement $e->setAttribute('publisher', $this->getPublisher()); if ($this->getCreationInstant() !== null) { - $e->setAttribute('creationInstant', gmdate('Y-m-d\TH:i:s\Z', $this->getCreationInstant())); + $e->setAttribute('creationInstant', $this->getCreationInstant()->format(C::DATETIME_FORMAT)); } if ($this->getPublicationId() !== null) { @@ -210,7 +212,7 @@ private static function processArrayContents(array $data): array if (array_key_exists('creationinstant', $data)) { Assert::string($data['creationinstant'], ArrayValidationException::class); Assert::validDateTimeZulu($data['creationinstant'], ArrayValidationException::class); - $retval['creationInstant'] = XMLUtils::xsDateTimeToTimestamp($data['creationinstant']); + $retval['creationInstant'] = new DateTimeImmutable($data['creationinstant']); } if (array_key_exists('publicationid', $data)) { @@ -240,7 +242,7 @@ public function toArray(): array $data['publisher'] = $this->getPublisher(); if ($this->getCreationInstant() !== null) { - $data['creationInstant'] = gmdate('Y-m-d\TH:i:s\Z', $this->getCreationInstant()); + $data['creationInstant'] = $this->getCreationInstant()->format(C::DATETIME_FORMAT); } if ($this->getPublicationId() !== null) { diff --git a/src/SAML2/XML/mdrpi/RegistrationInfo.php b/src/SAML2/XML/mdrpi/RegistrationInfo.php index 284c1b63c..0f4168a63 100644 --- a/src/SAML2/XML/mdrpi/RegistrationInfo.php +++ b/src/SAML2/XML/mdrpi/RegistrationInfo.php @@ -4,6 +4,7 @@ namespace SimpleSAML\SAML2\XML\mdrpi; +use DateTimeImmutable; use DOMElement; use SimpleSAML\Assert\Assert; use SimpleSAML\SAML2\Constants as C; @@ -12,7 +13,8 @@ use SimpleSAML\SAML2\XML\md\AbstractLocalizedURI; use SimpleSAML\XML\ArrayizableElementInterface; use SimpleSAML\XML\Exception\InvalidDOMElementException; -use SimpleSAML\XML\Utils as XMLUtils; + +use function preg_replace; /** * Class for handling the mdrpi:RegistrationInfo element. @@ -26,14 +28,16 @@ final class RegistrationInfo extends AbstractMdrpiElement implements Arrayizable * Create/parse a mdrpi:RegistrationInfo element. * * @param string $registrationAuthority - * @param int|null $registrationInstant - * @param \SimpleSAML\SAML2\XML\mdrpi\RegistrationPolicy[] $RegistrationPolicy + * @param \DateTimeImmutable|null $registrationInstant + * @param \SimpleSAML\SAML2\XML\mdrpi\RegistrationPolicy[] $registrationPolicy */ public function __construct( protected string $registrationAuthority, - protected ?int $registrationInstant = null, + protected ?DateTimeImmutable $registrationInstant = null, protected array $registrationPolicy = [], ) { + Assert::nullOrSame($registrationInstant?->getTimeZone()->getName(), 'Z', ProtocolViolationException::class); + Assert::maxCount($registrationPolicy, C::UNBOUNDED_LIMIT); Assert::allIsInstanceOf($registrationPolicy, RegistrationPolicy::class); /** @@ -69,9 +73,9 @@ public function getRegistrationAuthority(): string /** * Collect the value of the registrationInstant property * - * @return int|null + * @return \DateTimeImmutable|null */ - public function getRegistrationInstant(): ?int + public function getRegistrationInstant(): ?DateTimeImmutable { return $this->registrationInstant; } @@ -113,7 +117,7 @@ public static function fromXML(DOMElement $xml): static $registrationInstant = preg_replace('/([.][0-9]+Z)$/', 'Z', $registrationInstant, 1); Assert::validDateTimeZulu($registrationInstant, ProtocolViolationException::class); - $registrationInstant = XMLUtils::xsDateTimeToTimestamp($registrationInstant); + $registrationInstant = new DateTimeImmutable($registrationInstant); } $RegistrationPolicy = RegistrationPolicy::getChildrenOfClass($xml); @@ -133,7 +137,7 @@ public function toXML(DOMElement $parent = null): DOMElement $e->setAttribute('registrationAuthority', $this->getRegistrationAuthority()); if ($this->getRegistrationInstant() !== null) { - $e->setAttribute('registrationInstant', gmdate('Y-m-d\TH:i:s\Z', $this->getRegistrationInstant())); + $e->setAttribute('registrationInstant', $this->getRegistrationInstant()->format(C::DATETIME_FORMAT)); } foreach ($this->getRegistrationPolicy() as $rp) { @@ -186,7 +190,7 @@ private static function processArrayContents(array $data): array if (array_key_exists('registrationinstant', $data)) { Assert::string($data['registrationinstant'], ArrayValidationException::class); Assert::validDateTimeZulu($data['registrationinstant'], ArrayValidationException::class); - $retval['registrationInstant'] = XMLUtils::xsDateTimeToTimestamp($data['registrationinstant']); + $retval['registrationInstant'] = new DateTimeImmutable($data['registrationinstant']); } if (array_key_exists('registrationpolicy', $data)) { @@ -211,7 +215,7 @@ public function toArray(): array $data['registrationAuthority'] = $this->getRegistrationAuthority(); if ($this->getRegistrationInstant() !== null) { - $data['registrationInstant'] = gmdate('Y-m-d\TH:i:s\Z', $this->getRegistrationInstant()); + $data['registrationInstant'] = $this->getRegistrationInstant()->format(C::DATETIME_FORMAT); } if (!empty($this->getRegistrationPolicy())) { diff --git a/tests/SAML2/XML/mdrpi/PublicationInfoTest.php b/tests/SAML2/XML/mdrpi/PublicationInfoTest.php index 3ad5e4c9a..563919bea 100644 --- a/tests/SAML2/XML/mdrpi/PublicationInfoTest.php +++ b/tests/SAML2/XML/mdrpi/PublicationInfoTest.php @@ -4,6 +4,7 @@ namespace SimpleSAML\Test\SAML2\XML\mdrpi; +use DateTimeImmutable; use DOMDocument; use PHPUnit\Framework\TestCase; use SimpleSAML\SAML2\Exception\ProtocolViolationException; @@ -61,7 +62,7 @@ public function testMarshalling(): void { $publicationInfo = new PublicationInfo( 'SomePublisher', - 1293840000, + new DateTimeImmutable('2011-01-01T00:00:00Z'), 'SomePublicationId', [ new UsagePolicy('en', 'http://TheEnglishUsagePolicy'), diff --git a/tests/SAML2/XML/mdrpi/RegistrationInfoTest.php b/tests/SAML2/XML/mdrpi/RegistrationInfoTest.php index f0b072ae9..ffd5f0d2c 100644 --- a/tests/SAML2/XML/mdrpi/RegistrationInfoTest.php +++ b/tests/SAML2/XML/mdrpi/RegistrationInfoTest.php @@ -4,6 +4,7 @@ namespace SimpleSAML\Test\SAML2\XML\mdrpi; +use DateTimeImmutable; use DOMDocument; use PHPUnit\Framework\TestCase; use SimpleSAML\SAML2\Exception\ProtocolViolationException; @@ -63,7 +64,7 @@ public function testMarshalling(): void { $registrationInfo = new RegistrationInfo( 'https://ExampleAuthority', - 1234567890, + new DateTimeImmutable('2009-02-13T23:31:30Z'), [ new RegistrationPolicy('en', 'http://www.example.org/aai/metadata/en_registration.html'), new RegistrationPolicy('de', 'http://www.example.org/aai/metadata/de_registration.html'),