From 79e65ae658d2d20fe86932611812ddb8c7e138ae Mon Sep 17 00:00:00 2001 From: RafaJEstabile Date: Mon, 26 Jan 2026 09:28:05 -0300 Subject: [PATCH] =?UTF-8?q?Ajustes=20na=20implementa=C3=A7=C3=A3o=20do=20U?= =?UTF-8?q?LID?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Ulid.php | 12 ++++++++---- src/UlidConstants.php | 1 + tests/UlidTest.php | 12 ++++++++++++ 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/Ulid.php b/src/Ulid.php index d0d4bef..722f008 100755 --- a/src/Ulid.php +++ b/src/Ulid.php @@ -16,14 +16,18 @@ class Ulid * @param string $ulid * @return bool */ - public function isValidFormat( - string $ulid - ): bool { + public function isValidFormat(string $ulid): bool + { $validator = UlidConstants::TIME_LENGTH + UlidConstants::RANDOM_LENGTH; + if (strlen($ulid) !== $validator) { return false; } - return true; + + return preg_match( + UlidConstants::REGEX, + $ulid + ) === 1; } /** diff --git a/src/UlidConstants.php b/src/UlidConstants.php index 992a9c2..58dcc11 100755 --- a/src/UlidConstants.php +++ b/src/UlidConstants.php @@ -9,4 +9,5 @@ class UlidConstants const TIME_MAX = 281474976710655; const TIME_LENGTH = 10; const RANDOM_LENGTH = 16; + const REGEX = '/^[0-9A-HJKMNP-TV-Z]{26}$/ '; } diff --git a/tests/UlidTest.php b/tests/UlidTest.php index 5689cdf..0c69913 100755 --- a/tests/UlidTest.php +++ b/tests/UlidTest.php @@ -230,6 +230,18 @@ public function testDecodeTimeInvalidTime() $ulid->decodeTime('QS234SAD23RADSWRA3FADQ3RS2'); } + /** + * @covers Ulid\Ulid::isValidFormat + */ + public function testIsValidFormatDoesNotValidateCharacters() + { + $ulid = new Ulid(); + + $result = $ulid->isValidFormat('01E475VQGHNW990PVHXFDT4C6W'); + + $this->assertTrue($result); + } + protected function tearDown(): void { //