Skip to content

Commit 8de6aa8

Browse files
Improve DateTime transformer, Add microtime support on Sleep transformer
1 parent e3765b7 commit 8de6aa8

File tree

3 files changed

+52
-10
lines changed

3 files changed

+52
-10
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace Camillebaronnet\ETL\Tests\Transformer;
4+
5+
use Camillebaronnet\ETL\Transformer\DateTime;
6+
use PHPUnit\Framework\TestCase;
7+
8+
final class DateTimeTest extends TestCase
9+
{
10+
/**
11+
* @var DateTime
12+
*/
13+
private $dateTime;
14+
15+
/**
16+
* @var array
17+
*/
18+
private $data = [
19+
'name' => 'Bar',
20+
'created_at' => '20/06/1990 12h15',
21+
'updated_at' => '21/06/2010 6h02',
22+
];
23+
24+
25+
protected function setUp()
26+
{
27+
$this->dateTime = new DateTime();
28+
}
29+
30+
public function testCanReformatTheDatesBySpecifyingInputFormatAndOutputFormat()
31+
{
32+
$this->dateTime->from = 'd/m/Y H\hi';
33+
$this->dateTime->to = 'Y-m-d H:i:s';
34+
$this->dateTime->fields = ['created_at', 'updated_at'];
35+
36+
$result = $this->dateTime->__invoke($this->data);
37+
38+
$this->assertEquals($result, [
39+
'name' => 'Bar',
40+
'created_at' => '1990-06-20 12:15:00',
41+
'updated_at' => '2010-06-21 06:02:00',
42+
]);
43+
}
44+
}

src/Transformer/DateTime.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,20 @@
55
class DateTime implements TransformInterface
66
{
77
public $fields = [];
8-
public $format = 'Y-m-d H:i:s';
8+
public $from = 'Y-m-d H:i:s';
9+
public $to;
910

1011
public function __invoke(array $data): array
1112
{
1213
foreach($this->fields as $fieldName){
1314
$data[$fieldName] = \DateTime::createFromFormat(
14-
$this->format,
15+
$this->from,
1516
$data[$fieldName]
1617
);
18+
19+
if($this->to){
20+
$data[$fieldName] = $data[$fieldName]->format($this->to);
21+
}
1722
}
1823

1924
return $data;

src/Transformer/Sleep.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,12 @@ class Sleep implements TransformInterface
66
{
77
public $seconds = 1;
88

9-
/**
10-
* The saved context, hydrated by the __invoke.
11-
*
12-
* @var array
13-
*/
14-
protected $context = [];
15-
169
/**
1710
* The class entry point.
1811
*/
1912
public function __invoke(array $data): array
2013
{
21-
sleep($this->seconds);
14+
usleep($this->seconds * 1000000);
2215

2316
return $data;
2417
}

0 commit comments

Comments
 (0)