-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathImportResource.php
226 lines (177 loc) · 5.32 KB
/
ImportResource.php
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<?php
namespace LePhare\Import;
use Behat\Transliterator\Transliterator;
use Doctrine\DBAL\Schema\Table;
use LePhare\Import\Exception\ImportException;
use LePhare\Import\LoadStrategy\LoadStrategyInterface;
use Symfony\Component\Finder\Finder;
class ImportResource
{
private bool $readOnly = false;
protected string $name;
protected array $config;
protected ?LoadStrategyInterface $loadStrategy = null;
public function __construct(string $name, array $config)
{
$this->name = $name;
$this->config = $config;
}
public function getName(): string
{
return $this->name;
}
public function getConfig(): array
{
return $this->config;
}
/**
* @throws ImportException if the resource is marked as read-only
*/
public function setConfig(array $config)
{
if ($this->readOnly) {
throw new ImportException('Resource is read-only');
}
$this->config = $config;
return $this;
}
public function setReadOnly(): self
{
$this->readOnly = true;
return $this;
}
public function setLoadStrategy(LoadStrategyInterface $loadStrategy)
{
$this->loadStrategy = $loadStrategy;
return $this;
}
public function isLoadable(): bool
{
return isset($this->config['load']);
}
public function getTablename(): string
{
return $this->config['tablename'];
}
public function getTable(): Table
{
$table = new Table($this->getTablename());
$table->addColumn('file_line_no', 'string', ['notnull' => false]);
foreach ($this->config['load']['fields'] as $field => $definition) {
$table->addColumn(Transliterator::urlize($field, '_'), $definition['type'], $definition['options']);
}
foreach ($this->config['load']['extra_fields'] as $field => $definition) {
$table->addColumn(Transliterator::urlize($field, '_'), $definition['type'], $definition['options']);
}
foreach ($this->config['load']['indexes'] as $index) {
$table->addIndex($index);
}
return $table;
}
public function getCsvFields(): array
{
return $this->config['load']['fields'];
}
public function getAddLineNumber(): bool
{
return $this->config['load']['add_file_line_number'];
}
public function getFieldDelimiter(): string
{
return $this->config['load']['format_options']['field_delimiter'];
}
public function getLineDelimiter(): string
{
return $this->config['load']['format_options']['line_delimiter'];
}
public function getQuoteCharacter(): string
{
return $this->config['load']['format_options']['quote_character'];
}
public function getEscapeCharacter(): string
{
return $this->config['load']['format_options']['escape_character'];
}
public function getNullString(): string
{
return $this->config['load']['format_options']['null_string'];
}
public function isLoopable(): bool
{
return $this->config['load']['loop'];
}
public function isCopyable(): bool
{
return isset($this->config['copy']);
}
public function getCopyStrategy(): string
{
return $this->config['copy']['strategy'];
}
public function getMapping(): array
{
return $this->config['copy']['mapping'];
}
public function isUpdateableField($field): bool
{
return !isset($this->config['copy']['strategy_options'])
|| !in_array($field, $this->config['copy']['strategy_options']['non_updateable_fields'])
;
}
public function getCopyCondition(): ?string
{
return $this->config['copy']['strategy_options']['copy_condition'] ?: null;
}
public function getJoins(): ?string
{
return $this->config['copy']['strategy_options']['joins'] ?: null;
}
public function getConflictTarget(): ?string
{
return $this->config['copy']['strategy_options']['conflict_target']['sql'] ?: null;
}
public function isDistinct(): bool
{
return $this->config['copy']['strategy_options']['distinct'];
}
public function getTargetTablename(): string
{
return $this->config['copy']['target'];
}
public function getLoadMatchingFiles(string $dir): array
{
$pattern = $this->config['load']['pattern'];
$finder = new Finder();
$finder = $finder
->in($dir)
->depth('== 0')
->name('#'.$pattern.'#')
->sortByName()
;
$files = iterator_to_array($finder->files());
if (null !== $this->loadStrategy) {
$this->loadStrategy->sort($files);
}
return $files;
}
public function getSourceFiles(string $dir): array
{
$files = $this->getLoadMatchingFiles($dir);
if (!$this->config['load']['loop']) {
$files = array_slice($files, 0, 1);
}
return $files;
}
public function isArchivable(): bool
{
return isset($this->config['load']);
}
public function isQuarantinable(): bool
{
return isset($this->config['load']);
}
public function getFormat(): string
{
return $this->config['load']['format'];
}
}