This repository was archived by the owner on Dec 28, 2020. It is now read-only.
File tree Expand file tree Collapse file tree 4 files changed +32
-8
lines changed Expand file tree Collapse file tree 4 files changed +32
-8
lines changed Original file line number Diff line number Diff line change 1
1
<?php
2
2
namespace PHPTikkie \Entities ;
3
3
4
+ use DateTimeImmutable ;
4
5
use PHPTikkie \PHPTikkie ;
5
6
6
7
abstract class AbstractEntity
@@ -33,4 +34,19 @@ public function setAttributes(array $attributes)
33
34
}
34
35
}
35
36
}
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
+ }
36
52
}
Original file line number Diff line number Diff line change 1
1
<?php
2
2
namespace PHPTikkie \Entities ;
3
3
4
- use DateTimeImmutable ;
5
-
6
4
class Payment extends AbstractEntity
7
5
{
8
6
const STATUS_NEW = 'NEW ' ;
@@ -26,7 +24,7 @@ class Payment extends AbstractEntity
26
24
public $ counterPartyName ;
27
25
28
26
/**
29
- * @var DateTimeImmutable
27
+ * @var \ DateTimeImmutable
30
28
*/
31
29
public $ created ;
32
30
@@ -62,7 +60,7 @@ public function setAttributes(array $attributes)
62
60
parent ::setAttributes ($ attributes );
63
61
64
62
if (isset ($ attributes ['created ' ])) {
65
- $ this ->created = new DateTimeImmutable ($ attributes ['created ' ]);
63
+ $ this ->created = $ this -> toDateTime ($ attributes ['created ' ]);
66
64
}
67
65
}
68
66
}
Original file line number Diff line number Diff line change 1
1
<?php
2
2
namespace PHPTikkie \Entities ;
3
3
4
- use DateTimeImmutable ;
5
-
6
4
class PaymentRequest extends AbstractEntity
7
5
{
8
6
const STATUS_OPEN = 'OPEN ' ;
@@ -42,7 +40,7 @@ class PaymentRequest extends AbstractEntity
42
40
public $ description ;
43
41
44
42
/**
45
- * @var DateTimeImmutable|null
43
+ * @var \ DateTimeImmutable|null
46
44
*/
47
45
public $ expired ;
48
46
@@ -95,7 +93,7 @@ public function setAttributes(array $attributes)
95
93
96
94
foreach (['created ' , 'expired ' ] as $ dateAttribute ) {
97
95
if (isset ($ attributes [$ dateAttribute ])) {
98
- $ this ->{$ dateAttribute } = new DateTimeImmutable ($ attributes [$ dateAttribute ]);
96
+ $ this ->{$ dateAttribute } = $ this -> toDateTime ($ attributes [$ dateAttribute ]);
99
97
}
100
98
}
101
99
Original file line number Diff line number Diff line change 2
2
3
3
namespace PHPTikkie \Tests ;
4
4
5
+ use DateTimeImmutable ;
5
6
use PHPTikkie \Entities \Payment ;
6
7
use PHPTikkie \Entities \PaymentRequest ;
7
8
use PHPTikkie \Exceptions \RequestException ;
@@ -52,4 +53,15 @@ public function testFetchPaymentRequestFailed()
52
53
53
54
$ this ->tikkie ->paymentRequest ('platformtoken1 ' , 'usertoken1 ' , 'paymentrequesttoken1 ' );
54
55
}
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
+ }
55
67
}
You can’t perform that action at this time.
0 commit comments