File tree Expand file tree Collapse file tree 3 files changed +99
-19
lines changed Expand file tree Collapse file tree 3 files changed +99
-19
lines changed Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace Camillebaronnet \ETL \Tests \Transformer ;
4
+
5
+ use Camillebaronnet \ETL \Transformer \Map ;
6
+ use PHPUnit \Framework \TestCase ;
7
+
8
+ final class MapTest extends TestCase
9
+ {
10
+ /**
11
+ * @var Map
12
+ */
13
+ private $ map ;
14
+
15
+ /**
16
+ * @var array
17
+ */
18
+ private $ data = [
19
+ 'name ' => 'Bar ' ,
20
+ 'address_street ' => '1 road ' ,
21
+ 'address_zip ' => 'xxx ' ,
22
+ ];
23
+
24
+
25
+ protected function setUp ()
26
+ {
27
+ $ this ->map = new Map ();
28
+ }
29
+
30
+ public function testCanRenameSomeKeysAndRemoveUnrenamed ()
31
+ {
32
+ $ this ->map ->fields = [
33
+ 'name ' => 'TheName '
34
+ ];
35
+ $ result = $ this ->map ->__invoke ($ this ->data );
36
+
37
+ $ this ->assertEquals ($ result , [
38
+ 'TheName ' => 'Bar '
39
+ ]);
40
+ }
41
+
42
+ public function testCanRenameSomeKeysAndKeepUnrenamed ()
43
+ {
44
+ $ this ->map ->fields = [
45
+ 'name ' => 'TheName '
46
+ ];
47
+ $ this ->map ->keepUnmapped = true ;
48
+ $ result = $ this ->map ->__invoke ($ this ->data );
49
+
50
+ $ this ->assertEquals ($ result , [
51
+ 'TheName ' => 'Bar ' ,
52
+ 'address_street ' => '1 road ' ,
53
+ 'address_zip ' => 'xxx ' ,
54
+ ]);
55
+ }
56
+
57
+ public function testCanMapSomeKeysWithoutRenamesItAndRemoveOther ()
58
+ {
59
+ $ this ->map ->fields = ['name ' , 'address_street ' ];
60
+ $ result = $ this ->map ->__invoke ($ this ->data );
61
+
62
+ $ this ->assertEquals ($ result , [
63
+ 'name ' => 'Bar ' ,
64
+ 'address_street ' => '1 road ' ,
65
+ ]);
66
+ }
67
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace Camillebaronnet \ETL \Transformer ;
4
+
5
+ class Map implements TransformInterface
6
+ {
7
+ public $ fields = [];
8
+
9
+ public $ keepUnmapped = false ;
10
+
11
+ public function __invoke (array $ data ): array
12
+ {
13
+ foreach ($ data as $ key => $ value ) {
14
+ $ newName = $ this ->fields [$ key ] ?? null ;
15
+
16
+ if (!$ newName && in_array ($ key , $ this ->fields , true )){
17
+ $ newName = $ key ;
18
+ }
19
+
20
+ if ($ newName ) {
21
+ if ($ newName !== $ key ) {
22
+ $ data [$ newName ] = $ value ;
23
+ unset($ data [$ key ]);
24
+ }
25
+ } elseif (!$ this ->keepUnmapped ) {
26
+ unset($ data [$ key ]);
27
+ }
28
+ }
29
+
30
+ return $ data ;
31
+ }
32
+ }
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments