Skip to content

Commit 20a8252

Browse files
committed
Merge branch 'master' of github.com:not-empty/ulid-php-lib
2 parents ee0f4f7 + 0211032 commit 20a8252

4 files changed

Lines changed: 39 additions & 8 deletions

File tree

docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ services:
22
ulid-php-lib:
33
build:
44
context: ./
5-
dockerfile: ./ops/Dockerfile
5+
dockerfile: ./contrib/Dockerfile
66
container_name: ulid-php-lib
77
volumes:
88
- ./:/var/www/html

src/Ulid.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,17 @@ class Ulid
1616
* @param string $ulid
1717
* @return bool
1818
*/
19-
public function isValidFormat(
20-
string $ulid
21-
): bool {
19+
public function isValidFormat(string $ulid): bool
20+
{
2221
$validator = UlidConstants::TIME_LENGTH + UlidConstants::RANDOM_LENGTH;
22+
2323
if (strlen($ulid) !== $validator) {
2424
return false;
2525
}
26+
27+
if (!preg_match(UlidConstants::REGEX, $ulid)) {
28+
return false;
29+
}
2630
return true;
2731
}
2832

@@ -155,9 +159,9 @@ public function generate(
155159
$encodingChars = UlidConstants::CHARS;
156160

157161
for ($i = 9; $i >= 0; $i--) {
158-
$mod = $time % UlidConstants::LENGHT;
162+
$mod = $time % UlidConstants::LENGTH;
159163
$timeChars = $encodingChars[$mod] . $timeChars;
160-
$time = ($time - $mod) / UlidConstants::LENGHT;
164+
$time = ($time - $mod) / UlidConstants::LENGTH;
161165
}
162166

163167
$this->hasIncrementLastRandChars(
@@ -195,7 +199,7 @@ public function decodeTime(
195199
if ($encodingIndex === false) {
196200
throw new Exception('Invalid ULID character: ' . $char);
197201
}
198-
$exponential = pow(UlidConstants::LENGHT, $index);
202+
$exponential = pow(UlidConstants::LENGTH, $index);
199203
$carry += ($encodingIndex * $exponential);
200204
}
201205

src/UlidConstants.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
class UlidConstants
66
{
77
const CHARS = '0123456789ABCDEFGHJKMNPQRSTVWXYZ';
8-
const LENGHT = 32;
8+
const LENGTH = 32;
99
const TIME_MAX = 281474976710655;
1010
const TIME_LENGTH = 10;
1111
const RANDOM_LENGTH = 16;
12+
const REGEX = '/^[0-9A-HJKMNP-TV-Z]{26}$/ ';
1213
}

tests/UlidTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,32 @@ public function testDecodeTimeInvalidTime()
230230
$ulid->decodeTime('QS234SAD23RADSWRA3FADQ3RS2');
231231
}
232232

233+
/**
234+
* @covers Ulid\Ulid::isValidFormat
235+
*/
236+
public function testIsInvalidCharactersFormat()
237+
{
238+
$ulid = new Ulid();
239+
240+
$invalidUlid = '%%%%%%%%%%%%%%%%%%%%%%%%%%';
241+
242+
$result = $ulid->isValidFormat($invalidUlid);
243+
244+
$this->assertFalse($result);
245+
}
246+
247+
/**
248+
* @covers Ulid\Ulid::isValidFormat
249+
*/
250+
public function testIsValidFormatDoesNotValidateCharacters()
251+
{
252+
$ulid = new Ulid();
253+
254+
$result = $ulid->isValidFormat('01E475VQGHNW990PVHXFDT4C6W');
255+
256+
$this->assertTrue($result);
257+
}
258+
233259
protected function tearDown(): void
234260
{
235261
//

0 commit comments

Comments
 (0)