1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
require_once __DIR__ . '/vendor/autoload.php'; |
4
|
|
|
|
5
|
|
|
use oliverde8\AssociativeArraySimplified\AssociativeArray; |
6
|
|
|
use Oliverde8\Component\PhpEtl\ChainOperation\Grouping\SimpleGroupingOperation; |
7
|
|
|
use Oliverde8\Component\PhpEtl\ChainOperation\Loader\FileWriterOperation; |
8
|
|
|
use Oliverde8\Component\PhpEtl\ChainOperation\Transformer\CallbackTransformerOperation; |
9
|
|
|
use Oliverde8\Component\PhpEtl\ChainProcessor; |
10
|
|
|
use Oliverde8\Component\PhpEtl\Item\DataItem; |
11
|
|
|
use Oliverde8\Component\PhpEtl\Item\DataItemInterface; |
12
|
|
|
use Oliverde8\Component\PhpEtl\Loader\File\Csv; |
13
|
|
|
use Oliverde8\Component\PhpEtl\Model\File\Csv\Writer; |
14
|
|
|
|
15
|
|
|
$inputIterator = new Csv(__DIR__ . '/exemples/I-Service.csv'); |
16
|
|
|
|
17
|
|
|
$localizableAttributes = ['name_src', 'editor_name_src', 'valid_from', 'valid_to']; |
18
|
|
|
|
19
|
|
|
$operations = []; |
20
|
|
|
|
21
|
|
|
// Cleanup the data to use akeneo attribute codes. |
22
|
|
|
$operations[] = new CallbackTransformerOperation(function (DataItemInterface $item, &$context) { |
23
|
|
|
$data = $item->getData(); |
24
|
|
|
$newData = []; |
25
|
|
|
$newData['sku'] = implode( |
26
|
|
|
'_', |
27
|
|
|
[ |
28
|
|
|
'SRV', |
29
|
|
|
AssociativeArray::getFromKey($data, 'APS_ID') |
30
|
|
|
] |
31
|
|
|
); |
32
|
|
|
$newData['locale'] = implode( |
33
|
|
|
'_', |
34
|
|
|
[ |
35
|
|
|
strtolower(AssociativeArray::getFromKey($data, 'LANGUAGE')), |
|
|
|
|
36
|
|
|
strtoupper(AssociativeArray::getFromKey($data, 'COUNTRY')), |
|
|
|
|
37
|
|
|
] |
38
|
|
|
); |
39
|
|
|
$newData['categories'] = 'main_service,web_service'; |
40
|
|
|
$newData['family'] = 'service'; |
41
|
|
|
$newData['name_src'] = AssociativeArray::getFromKey($data, 'SERVICE_NAME'); |
42
|
|
|
$newData['editor_name_src'] = AssociativeArray::getFromKey($data, 'PROVIDER'); |
43
|
|
|
$newData['valid_from'] = AssociativeArray::getFromKey($data, 'BEGIN_DATE'); |
44
|
|
|
$newData['valid_to'] = AssociativeArray::getFromKey($data, 'END_DATE'); |
45
|
|
|
|
46
|
|
|
return new DataItem($newData); |
47
|
|
|
}); |
48
|
|
|
|
49
|
|
|
// Group products by sku. |
50
|
|
|
$operations[] = new SimpleGroupingOperation(['sku']); |
51
|
|
|
|
52
|
|
|
// Finalize transformation by having proper attribute codes taking locales into account. |
53
|
|
|
$operations[] = new CallbackTransformerOperation( |
54
|
|
|
function (DataItemInterface $item, &$context) use ($localizableAttributes) { |
55
|
|
|
|
56
|
|
|
$data = []; |
57
|
|
|
foreach ($item->getData() as $productDetails) { |
58
|
|
|
$locale = $productDetails['locale']; |
59
|
|
|
unset($productDetails['locale']); |
60
|
|
|
|
61
|
|
|
foreach ($productDetails as $attributeCode => $value) { |
62
|
|
|
if (in_array($attributeCode, $localizableAttributes)) { |
63
|
|
|
$data[$attributeCode . '-' . $locale] = $value; |
64
|
|
|
} else { |
65
|
|
|
$data[$attributeCode] = $value; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return new DataItem($data); |
71
|
|
|
} |
72
|
|
|
); |
73
|
|
|
|
74
|
|
|
// Write into files. |
75
|
|
|
$operations[] = new FileWriterOperation(new Writer(__DIR__ . '/exemples/output.csv')); |
76
|
|
|
|
77
|
|
|
$chainProcessor = new ChainProcessor($operations); |
78
|
|
|
$chainProcessor->process($inputIterator, []); |