-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMap.php
More file actions
43 lines (32 loc) · 1.01 KB
/
Copy pathMap.php
File metadata and controls
43 lines (32 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php
namespace Camillebaronnet\ETL\Transformer;
use Camillebaronnet\ETL\TransformInterface;
use RuntimeException;
class Map implements TransformInterface
{
public $fields = [];
public $keepUnmapped = false;
/**
* Use the value as renamed key if key is an integer.
* @var bool
*/
public $softKey = true;
public function __invoke(iterable $data): iterable
{
$remappedData = [];
foreach ($this->fields as $newKey => $originalKey) {
$newKey = $this->softKey && is_int($newKey) ? $originalKey : $newKey;
$remappedData[$newKey] = $data[$originalKey] ?? null;
}
if (!$this->keepUnmapped) {
return $remappedData;
}
if(!is_array($data)){
throw new RuntimeException('Input data must be an array.');
}
foreach (array_diff(array_keys($data), $this->fields) as $lostField) {
$remappedData[$lostField] = $data[$lostField];
}
return $remappedData;
}
}