Skip to content
This repository was archived by the owner on Dec 28, 2020. It is now read-only.

Commit d958800

Browse files
Add support for epoch timestamps with milliseconds.
1 parent 698d660 commit d958800

File tree

4 files changed

+32
-8
lines changed

4 files changed

+32
-8
lines changed

src/Entities/AbstractEntity.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
namespace PHPTikkie\Entities;
33

4+
use DateTimeImmutable;
45
use PHPTikkie\PHPTikkie;
56

67
abstract class AbstractEntity
@@ -33,4 +34,19 @@ public function setAttributes(array $attributes)
3334
}
3435
}
3536
}
37+
38+
/**
39+
* Convert an ISO-8601 formatted string to DateTimeImmutable.
40+
*/
41+
protected function toDateTime(string $representation): DateTimeImmutable
42+
{
43+
// Due to a Tikkie bug, the API may return epoch timestamps with milliseconds instead of a ISO-8601 formatted string.
44+
// I reported this on 24-04-2019.
45+
if (is_numeric($representation)) {
46+
// Remove milliseconds and prepend with @ to mark as timestamp.
47+
$representation = '@'.substr($representation, 0, 10);
48+
}
49+
50+
return new DateTimeImmutable($representation);
51+
}
3652
}

src/Entities/Payment.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
<?php
22
namespace PHPTikkie\Entities;
33

4-
use DateTimeImmutable;
5-
64
class Payment extends AbstractEntity
75
{
86
const STATUS_NEW = 'NEW';
@@ -26,7 +24,7 @@ class Payment extends AbstractEntity
2624
public $counterPartyName;
2725

2826
/**
29-
* @var DateTimeImmutable
27+
* @var \DateTimeImmutable
3028
*/
3129
public $created;
3230

@@ -62,7 +60,7 @@ public function setAttributes(array $attributes)
6260
parent::setAttributes($attributes);
6361

6462
if (isset($attributes['created'])) {
65-
$this->created = new DateTimeImmutable($attributes['created']);
63+
$this->created = $this->toDateTime($attributes['created']);
6664
}
6765
}
6866
}

src/Entities/PaymentRequest.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
<?php
22
namespace PHPTikkie\Entities;
33

4-
use DateTimeImmutable;
5-
64
class PaymentRequest extends AbstractEntity
75
{
86
const STATUS_OPEN = 'OPEN';
@@ -42,7 +40,7 @@ class PaymentRequest extends AbstractEntity
4240
public $description;
4341

4442
/**
45-
* @var DateTimeImmutable|null
43+
* @var \DateTimeImmutable|null
4644
*/
4745
public $expired;
4846

@@ -95,7 +93,7 @@ public function setAttributes(array $attributes)
9593

9694
foreach (['created', 'expired'] as $dateAttribute) {
9795
if (isset($attributes[$dateAttribute])) {
98-
$this->{$dateAttribute} = new DateTimeImmutable($attributes[$dateAttribute]);
96+
$this->{$dateAttribute} = $this->toDateTime($attributes[$dateAttribute]);
9997
}
10098
}
10199

tests/FetchPaymentRequestTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace PHPTikkie\Tests;
44

5+
use DateTimeImmutable;
56
use PHPTikkie\Entities\Payment;
67
use PHPTikkie\Entities\PaymentRequest;
78
use PHPTikkie\Exceptions\RequestException;
@@ -52,4 +53,15 @@ public function testFetchPaymentRequestFailed()
5253

5354
$this->tikkie->paymentRequest('platformtoken1', 'usertoken1', 'paymentrequesttoken1');
5455
}
56+
57+
// Due to a Tikkie bug, the API may return epoch timestamps with milliseconds instead of a ISO-8601 formatted string.
58+
// I reported this on 24-04-2019.
59+
public function testCreatedDateCanBeTimestampWithMilliseconds()
60+
{
61+
$payment = new Payment($this->tikkie);
62+
$payment->setAttributes(['created' => '1554957274674']);
63+
64+
$this->assertInstanceOf(DateTimeImmutable::class, $payment->created);
65+
$this->assertEquals('1554957274', $payment->created->getTimestamp());
66+
}
5567
}

0 commit comments

Comments
 (0)