File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 55class 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}
Original file line number Diff line number Diff 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 //
You can’t perform that action at this time.
0 commit comments