Skip to content

Commit 7b3b0b8

Browse files
committed
- improved timestamps handling
- improved toDateTime() method - added strictness - typos
1 parent 19033bb commit 7b3b0b8

File tree

1 file changed

+27
-20
lines changed

1 file changed

+27
-20
lines changed

ULID.php

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php declare(strict_types=1);
22

33
/*
44
* This file is part of the Koded package.
@@ -72,7 +72,7 @@ private function __construct(int $timestamps, int $count)
7272
}
7373

7474
/**
75-
* Creates and instance of ULID with number
75+
* Creates an instance of ULID with number
7676
* of timestamps defined by the count argument.
7777
* @param int $count
7878
* @return static
@@ -83,7 +83,7 @@ public static function generate(int $count = 1): self
8383
}
8484

8585
/**
86-
* Decode the ULID string into the instance of ULID.
86+
* Decode the ULID string into an instance of ULID.
8787
* @param string $ulid
8888
* @return static
8989
*/
@@ -104,7 +104,7 @@ public static function fromULID(string $ulid): self
104104
}
105105

106106
/**
107-
* Decode the ULID string in UUID format into the instance of ULID.
107+
* Decode the ULID string in UUID format into an instance of ULID.
108108
* @param string $ulid UUID representation of ULID value
109109
* @return static
110110
*/
@@ -118,31 +118,37 @@ public static function fromUUID(string $ulid): self
118118
}
119119

120120
/**
121-
* Decode the date time string into the instance of ULID.
122-
* @param float $timestamp UNIX timestamp with or without the milliseconds part.
121+
* Decode the date time string into an instance of ULID.
122+
* @param float $timestamp UNIX timestamp with or without the milliseconds part
123123
* @return static
124124
*/
125125
public static function fromTimestamp(float $timestamp): self
126126
{
127-
if ($timestamp >= 0 && $timestamp <= PHP_INT_MAX) {
128-
[$ts, $ms] = explode('.', $timestamp) + [1 => '000'];
129-
return new static("$ts$ms", 1);
127+
if ($timestamp <= 0 || $timestamp >= PHP_INT_MAX) {
128+
throw new InvalidArgumentException("Invalid timestamp ($timestamp)", 400);
130129
}
131-
throw new InvalidArgumentException("Invalid timestamp ($timestamp)", 400);
130+
$timestamp = (string)$timestamp;
131+
if (str_contains($timestamp, '.')) {
132+
$timestamp = (string)($timestamp * 1000);
133+
}
134+
if (strlen($timestamp) >= 13) {
135+
$timestamp = substr($timestamp, 0, 13);
136+
}
137+
return new static(intval($timestamp), 1);
132138
}
133139

134140
/**
135-
* Decode the date time string into the instance of ULID.
141+
* Decode the date time string into an instance of ULID.
136142
* @param string $datetime in format: Y-m-d H:i:s with optional 'v' (milliseconds)
137143
* @return static
138144
*/
139145
public static function fromDateTime(string $datetime): self
140146
{
141147
try {
142-
$dt = (false === str_contains($datetime, '.'))
143-
? DateTime::createFromFormat('Y-m-d H:i:s', $datetime, new DateTimeZone('UTC'))
144-
: DateTime::createFromFormat('Y-m-d H:i:s.v', $datetime, new DateTimeZone('UTC'));
145-
return new static($dt->getTimestamp() . $dt->format('v'), 1);
148+
$dt = (str_contains($datetime, '.'))
149+
? DateTime::createFromFormat('Y-m-d H:i:s.v', $datetime, new DateTimeZone('UTC'))
150+
: DateTime::createFromFormat('Y-m-d H:i:s', $datetime, new DateTimeZone('UTC'));
151+
return new static(intval($dt->getTimestamp() . $dt->format('v')), 1);
146152
} catch (Throwable) {
147153
throw new InvalidArgumentException("Invalid datetime ($datetime)", 400);
148154
}
@@ -154,7 +160,7 @@ public static function valid(string $uuid): bool
154160
}
155161

156162
/**
157-
* Creates a single, or a list. of UUID representations of ULID values.
163+
* Creates a single, or a list, of UUID values.
158164
* @return array|string
159165
*/
160166
public function toUUID(): array|string
@@ -175,7 +181,7 @@ public function toUUID(): array|string
175181
}
176182

177183
/**
178-
* Creates a single, or a list. of ULID representations.
184+
* Creates a single, or a list of ULID values.
179185
* @return array|string
180186
*/
181187
public function toULID(): array|string
@@ -191,16 +197,17 @@ public function toULID(): array|string
191197
}
192198

193199
/**
194-
* Returns a single, or a list, of DateTime instances for this ULID.
200+
* Returns a single, or a list, of DateTime instances.
195201
* @return array|DateTime
196202
*/
197203
public function toDateTime(): array|DateTime
198204
{
199205
$list = [];
200206
foreach ($this->timestamps as $timestamp) {
207+
$timestamp = (string)$timestamp;
201208
$datetime = new DateTime('@' . substr($timestamp, 0, 10), new DateTimeZone('UTC'));
202-
if (13 === strlen($timestamp)) {
203-
$ms = substr($timestamp, 10);
209+
if (strlen($timestamp) >= 13) {
210+
$ms = substr($timestamp, 10, 3);
204211
$datetime->modify("+{$ms} milliseconds");
205212
}
206213
$list[] = $datetime;

0 commit comments

Comments
 (0)