|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* TechDivision\Import\Adapter\CsvExportAdapter |
|
5
|
|
|
* |
|
6
|
|
|
* NOTICE OF LICENSE |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
|
9
|
|
|
* that is available through the world-wide-web at this URL: |
|
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
|
11
|
|
|
* |
|
12
|
|
|
* PHP version 5 |
|
13
|
|
|
* |
|
14
|
|
|
* @author Tim Wagner <[email protected]> |
|
15
|
|
|
* @copyright 2016 TechDivision GmbH <[email protected]> |
|
16
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
17
|
|
|
* @link https://github.com/techdivision/import |
|
18
|
|
|
* @link http://www.techdivision.com |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
namespace TechDivision\Import\Adapter; |
|
22
|
|
|
|
|
23
|
|
|
use Goodby\CSV\Export\Protocol\ExporterInterface; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* CSV export adapter implementation. |
|
27
|
|
|
* |
|
28
|
|
|
* @author Tim Wagner <[email protected]> |
|
29
|
|
|
* @copyright 2016 TechDivision GmbH <[email protected]> |
|
30
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
31
|
|
|
* @link https://github.com/techdivision/import |
|
32
|
|
|
* @link http://www.techdivision.com |
|
33
|
|
|
*/ |
|
34
|
|
|
class CsvExportAdapter implements ExportAdapterInterface |
|
35
|
|
|
{ |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* The exporter instance. |
|
39
|
|
|
* |
|
40
|
|
|
* @var \Goodby\CSV\Export\Protocol\ExporterInterface |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $exporter; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* The array with the names of the exported files. |
|
46
|
|
|
* |
|
47
|
|
|
* @var array |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $exportedFilenames = array(); |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Initialize the adapter with the configuration. |
|
53
|
|
|
* |
|
54
|
|
|
* @param \Goodby\CSV\Export\Protocol\ExporterInterface $exporter The exporter instance |
|
55
|
|
|
*/ |
|
56
|
|
|
public function __construct(ExporterInterface $exporter) |
|
57
|
|
|
{ |
|
58
|
|
|
$this->exporter = $exporter; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Imports the content of the CSV file with the passed filename. |
|
63
|
|
|
* |
|
64
|
|
|
* @param array $artefacts The artefacts to be exported |
|
65
|
|
|
* @param string $targetDir The target dir to export the artefacts to |
|
66
|
|
|
* @param integer $timestamp The timestamp part of the original import file |
|
67
|
|
|
* @param string $counter The counter part of the origin import file |
|
68
|
|
|
* |
|
69
|
|
|
* @return void |
|
70
|
|
|
*/ |
|
71
|
|
|
public function export(array $artefacts, $targetDir, $timestamp, $counter) |
|
72
|
|
|
{ |
|
73
|
|
|
|
|
74
|
|
|
// reset the array with the exported filename |
|
75
|
|
|
$this->exportedFilenames = array(); |
|
76
|
|
|
|
|
77
|
|
|
// iterate over the artefacts and export them |
|
78
|
|
|
foreach ($artefacts as $artefactType => $artefacts) { |
|
79
|
|
|
// initialize the bunch and the exporter |
|
80
|
|
|
$bunch = array(); |
|
81
|
|
|
|
|
82
|
|
|
// iterate over the artefact types artefacts |
|
83
|
|
|
foreach ($artefacts as $entityArtefacts) { |
|
84
|
|
|
// set the bunch header and append the artefact data |
|
85
|
|
|
if (sizeof($bunch) === 0) { |
|
86
|
|
|
$first = reset($entityArtefacts); |
|
87
|
|
|
$second = reset($first); |
|
88
|
|
|
$bunch[] = array_keys($second); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
// export the artefacts |
|
92
|
|
|
foreach ($entityArtefacts as $entityArtefact) { |
|
93
|
|
|
$bunch = array_merge($bunch, $entityArtefact); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
// prepare the name of the export file |
|
98
|
|
|
$filename = sprintf( |
|
99
|
|
|
'%s/%s_%s_%s.csv', |
|
100
|
|
|
$targetDir, |
|
101
|
|
|
$artefactType, |
|
102
|
|
|
$timestamp, |
|
103
|
|
|
$counter |
|
104
|
|
|
); |
|
105
|
|
|
|
|
106
|
|
|
// export the artefact (bunch) |
|
107
|
|
|
$this->exporter->export($filename, $bunch); |
|
108
|
|
|
|
|
109
|
|
|
// add the filename to the array with the exported filenames |
|
110
|
|
|
$this->exportedFilenames[] = $filename; |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Return's the array with the names of the exported files. |
|
116
|
|
|
* |
|
117
|
|
|
* @return array The array with the exported filenames |
|
118
|
|
|
*/ |
|
119
|
|
|
public function getExportedFilenames() |
|
120
|
|
|
{ |
|
121
|
|
|
return $this->exportedFilenames; |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|