-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWriter.php
More file actions
170 lines (141 loc) · 5.67 KB
/
Writer.php
File metadata and controls
170 lines (141 loc) · 5.67 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
declare(strict_types=1);
namespace Draeli\Mysql;
use Sonata\Exporter\Writer\CsvWriter as BaseWriter;
use Sonata\Exporter\Writer\TypedWriterInterface;
use Draeli\Mysql\Components\AbstractConfigurationImportField;
use Draeli\Mysql\Components\ConfigurationImport;
use Draeli\Mysql\Components\ConfigurationImportField;
use Draeli\Mysql\Components\ConfigurationImportFieldCalculated;
use Draeli\Mysql\Utils;
/**
* Import writer
* @package Draeli\Mysql
*/
class Writer implements TypedWriterInterface
{
/** @var ConfigurationImport */
private $configurationImport;
/** @var BaseWriter */
private $typedWriter;
/** @var int */
private $lineCreated = 0;
/** @var string[] */
private $fieldsOrder;
/** @var AbstractConfigurationImportField[] */
private $fieldsTarget;
/** @var callable|null */
private $callbackLineCleaning;
/** @var callable|null */
private $callbackLineValidation;
/**
* @param ConfigurationImport $configurationImport
* @param string $filename
*/
public function __construct(
ConfigurationImport $configurationImport
, string $filename
)
{
// clone to avoid object change after export preparation
$this->configurationImport = clone $configurationImport;
$delimiter = $this->configurationImport->getFormattingDelimiter();
$enclosure = $this->configurationImport->getFormattingEnclosure();
$escape = $this->configurationImport->getFormattingEscapeChar();
$withBom = false;
$terminate = "\n";
$this->typedWriter = new BaseWriter($filename, $delimiter, $enclosure, $escape, false, $withBom, $terminate);
// /!\ To avoid weird behaviour, calculated field must appear in last position
// Contain columns name to use for first line of file witch will be create in write method
$this->fieldsOrder = $this->configurationImport->getOrderTargetFields();
$this->fieldsTarget = $configurationImport->getFieldsFromTargetName($this->fieldsOrder);
// Prepare callback type conversion
$this->fieldsConversionType = [];
foreach ($this->fieldsOrder as $fieldNameTarget){
$field = $this->fieldsTarget[$fieldNameTarget];
$this->fieldsConversionType[$fieldNameTarget] = static function($data)use($field){
return Utils::getDataConverted($data, $field);
};
}
$this->callbackLineCleaning = $configurationImport->getCallbackLineCleaning();
$this->callbackLineValidation = $configurationImport->getCallbackLineValidation();
}
/**
* @param array $data
*/
public function write(array $data): void
{
$result = [];
// manage field result by order
foreach($this->fieldsOrder as $fieldName){
$field = $this->fieldsTarget[$fieldName];
if( $field instanceof ConfigurationImportField ){
$sourceName = $field->getSourceName();
$value = $data[$sourceName];
$callbackCleaning = $field->getCallbackCleaning();
if( null !== $callbackCleaning ){
// to clean/manage value before his conversion
$value = $callbackCleaning($value);
}
}
elseif( $field instanceof ConfigurationImportFieldCalculated ){
$value = $field->getCallbackCalcul()($data);
}
else{
throw new \UnexpectedValueException('Unsupported field type.');
}
// ensure value is converted according with field definition
$value = $this->fieldsConversionType[$fieldName]($value);
$result[$fieldName] = $value;
}
$callbackLineCleaning = $this->callbackLineCleaning;
if( null !== $callbackLineCleaning ){
// to adjust some fields for special case where dependency between fields is required
$datasToChange = $callbackLineCleaning($result);
if( !\is_array($datasToChange) && null !== $datasToChange ){
throw new \InvalidArgumentException('Callback for "getCallbackLineCleaning" must return array or null.');
}
if( \is_array($datasToChange) ){
$unknownColumns = array_diff_key($datasToChange, $result);
if( \count($unknownColumns) ){
throw new \LogicException('Columns "' . implode(';', $unknownColumns) . '" are unknown.');
}
$result = array_merge($result, $datasToChange);
}
}
$callbackLineValidation = $this->callbackLineValidation;
// add line only if there is no callback or callback return true
if( (null === $callbackLineValidation) || $callbackLineValidation($result) ){
++$this->lineCreated;
foreach($result as &$v){
if( null === $v ){
// https://dev.mysql.com/doc/refman/8.0/en/problems-with-null.html => "To load a NULL value into a column, use \N in the data file"
$v = '\N';
}
}
unset($v);
$this->typedWriter->write($result);
}
}
public function getDefaultMimeType(): string
{
return $this->typedWriter->getDefaultMimeType();
}
public function getFormat(): string
{
return $this->typedWriter->getFormat();
}
public function getLinesCreated(): int
{
return $this->lineCreated;
}
public function open()
{
$this->typedWriter->open();
$this->typedWriter->write($this->fieldsOrder);
}
public function close()
{
$this->typedWriter->close();
}
}