Skip to content

Commit 6c148dd

Browse files
authored
Merge pull request #50 from mirko-bukilic/release-3.x
Added TimestampValueMilliseconds for handling milliseconds
2 parents d93511e + 428a412 commit 6c148dd

4 files changed

Lines changed: 203 additions & 0 deletions

File tree

src/TimestampValue.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,17 @@ public function getMilliseconds()
6464
return (int) ($this->value * 1000);
6565
}
6666

67+
/**
68+
* @param TimestampValueMilliSeconds $timestampValue
69+
* @return TimestampValue
70+
* @throws InvalidTimestampValueException
71+
* @throws MissingTimestampValueException
72+
*/
73+
public static function fromTimestampValueMillis(TimestampValueMilliSeconds $timestampValue)
74+
{
75+
return new self($timestampValue->getValue() / 1000);
76+
}
77+
6778
/**
6879
* @param $timestamp
6980
* @return bool

src/TimestampValueMilliSeconds.php

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
3+
namespace G4\ValueObject;
4+
5+
use G4\ValueObject\Exception\MissingTimestampValueException;
6+
use G4\ValueObject\Exception\InvalidTimestampValueException;
7+
8+
class TimestampValueMilliSeconds implements StringInterface, NumberInterface
9+
{
10+
const MILLISECONDS_MIN_LENGTH = 10;
11+
const MILLISECONDS_MAX_LENGTH = 13;
12+
13+
/**
14+
* @var int
15+
*/
16+
private $value;
17+
18+
/**
19+
* @param $value
20+
* @throws MissingTimestampValueException
21+
* @throws InvalidTimestampValueException
22+
*/
23+
public function __construct($value)
24+
{
25+
if (empty($value)) {
26+
throw new MissingTimestampValueException();
27+
}
28+
29+
if (!self::isValid($value)) {
30+
throw new InvalidTimestampValueException($value);
31+
}
32+
33+
$this->value = (int) $value;
34+
}
35+
36+
public function __toString()
37+
{
38+
return (string) $this->value;
39+
}
40+
41+
/**
42+
* @return int
43+
*/
44+
public function getValue()
45+
{
46+
return $this->value;
47+
}
48+
49+
/**
50+
* @return int
51+
*/
52+
public function getSeconds()
53+
{
54+
return (int) ($this->value / 1000);
55+
}
56+
57+
/**
58+
* @return string
59+
*/
60+
public function getFormatted()
61+
{
62+
$seconds = (int) ($this->value / 1000);
63+
$milliseconds = $this->value % 1000;
64+
$millisecondsPadded = str_pad($milliseconds, 3, '0', STR_PAD_LEFT);
65+
return date('Y-m-d H:i:s', $seconds) . '.' . $millisecondsPadded;
66+
}
67+
68+
/**
69+
* @param TimestampValue $timestampValue
70+
* @return self
71+
* @throws InvalidTimestampValueException
72+
* @throws MissingTimestampValueException
73+
*/
74+
public static function fromTimestampValue(TimestampValue $timestampValue)
75+
{
76+
return new self($timestampValue->getValue() * 1000);
77+
}
78+
79+
/**
80+
* @return self
81+
* @throws InvalidTimestampValueException
82+
* @throws MissingTimestampValueException
83+
*/
84+
public static function now()
85+
{
86+
return new self((int) (microtime(true) * 1000));
87+
}
88+
89+
/**
90+
* @param $timestamp
91+
* @return bool
92+
*/
93+
public static function isValid($timestamp)
94+
{
95+
96+
if (strlen((string) $timestamp) < self::MILLISECONDS_MIN_LENGTH
97+
|| strlen((string) $timestamp) > self::MILLISECONDS_MAX_LENGTH
98+
) {
99+
return false;
100+
}
101+
102+
$check = (is_int($timestamp) || is_float($timestamp))
103+
? $timestamp
104+
: (string) (int) $timestamp;
105+
106+
return $check === $timestamp;
107+
}
108+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
namespace unit\src;
4+
5+
use G4\ValueObject\Exception\MissingTimestampValueException;
6+
use G4\ValueObject\Exception\InvalidTimestampValueException;
7+
use G4\ValueObject\TimestampValue;
8+
use G4\ValueObject\TimestampValueMilliSeconds;
9+
use PHPUnit\Framework\TestCase;
10+
11+
class TimestampValueMillisecondsTest extends TestCase
12+
{
13+
public function testMissingValueException()
14+
{
15+
$this->expectException(MissingTimestampValueException::class);
16+
new TimestampValueMilliSeconds('');
17+
}
18+
19+
public function testInvalidValueFloatException()
20+
{
21+
$this->expectException(InvalidTimestampValueException::class);
22+
new TimestampValueMilliSeconds('1.0');
23+
}
24+
25+
public function testInvalidValueHexaNumberException()
26+
{
27+
$this->expectException(InvalidTimestampValueException::class);
28+
new TimestampValueMilliSeconds('0xFF');
29+
}
30+
31+
public function testValidValues()
32+
{
33+
$now = (int) (microtime(true) * 1000);
34+
$timestampInt = new TimestampValueMilliSeconds($now);
35+
$this->assertEquals($now, $timestampInt->getValue());
36+
37+
$timestampString = new TimestampValueMilliSeconds('1748436857881');
38+
$this->assertEquals('1748436857881', (string) $timestampString);
39+
}
40+
41+
public function testGetFormatted()
42+
{
43+
$timestamp = new TimestampValueMilliSeconds('1748436857881');
44+
$this->assertEquals('2025-05-28 14:54:17.881', $timestamp->getFormatted());
45+
}
46+
47+
public function testGetSeconds()
48+
{
49+
$timestamp = new TimestampValueMilliSeconds(1748436857881);
50+
$this->assertEquals(1748436857, $timestamp->getSeconds());
51+
}
52+
53+
public function testNow()
54+
{
55+
$time = time();
56+
$timestamp = TimestampValueMilliSeconds::now();
57+
$this->assertEquals($time, $timestamp->getSeconds());
58+
}
59+
60+
public function testInvalidValueLengthLower()
61+
{
62+
$this->expectException(InvalidTimestampValueException::class);
63+
new TimestampValueMilliSeconds(123456789);
64+
}
65+
66+
public function testInvalidValueLengthHigher()
67+
{
68+
$this->expectException(InvalidTimestampValueException::class);
69+
new TimestampValueMilliSeconds(12345678912345);
70+
}
71+
72+
public function testFromTimestampValue()
73+
{
74+
$timestamp = TimestampValueMilliSeconds::fromTimestampValue(new TimestampValue(1748436857));
75+
$this->assertEquals(1748436857000, $timestamp->getValue());
76+
}
77+
}

tests/unit/src/TimestampValueTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use G4\ValueObject\TimestampValue;
44
use G4\ValueObject\Exception\MissingTimestampValueException;
55
use G4\ValueObject\Exception\InvalidTimestampValueException;
6+
use G4\ValueObject\TimestampValueMilliSeconds;
67

78
class TimestampValueTest extends \PHPUnit_Framework_TestCase
89
{
@@ -47,4 +48,10 @@ public function testGetMilliseconds()
4748
$timestampString = new TimestampValue('1523441442');
4849
$this->assertEquals('1523441442000', $timestampString->getMilliseconds());
4950
}
51+
52+
public function testFromTimestampValueMillis()
53+
{
54+
$timestamp = TimestampValue::fromTimestampValueMillis(new TimestampValueMilliSeconds(1748436857000));
55+
$this->assertEquals(1748436857, $timestamp->getValue());
56+
}
5057
}

0 commit comments

Comments
 (0)