diff --git a/docker-compose.yml b/docker-compose.yml index 03d83cb..aa16942 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -2,7 +2,7 @@ services: ulid-php-lib: build: context: ./ - dockerfile: ./ops/Dockerfile + dockerfile: ./contrib/Dockerfile container_name: ulid-php-lib volumes: - ./:/var/www/html 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..9e6ad7b 100755 --- a/tests/UlidTest.php +++ b/tests/UlidTest.php @@ -230,6 +230,19 @@ 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 { //