Skip to content

Commit 42da0d3

Browse files
authored
Merge pull request #81 from utopia-php/support-arrays-on-csv
Add: support for array attributes
2 parents ea1c585 + d5bc659 commit 42da0d3

1 file changed

Lines changed: 53 additions & 17 deletions

File tree

src/Migration/Sources/CSV.php

Lines changed: 53 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -135,31 +135,40 @@ private function exportDocuments(int $batchSize): void
135135
}
136136
}
137137

138+
$arrayKeys = [];
138139
$attributeTypes = [];
139140
$manyToManyKeys = [];
140141

141142
foreach ($attributes as $attribute) {
142143
$key = $attribute['key'];
144+
$type = $attribute['type'];
145+
$isArray = $attribute['array'] ?? false;
146+
$relationSide = $attribute['side'] ?? '';
147+
$relationType = $attribute['relationType'] ?? '';
143148

144149
if (
145-
$attribute['type'] === Attribute::TYPE_RELATIONSHIP &&
146-
($attribute['side'] ?? '') === UtopiaDatabase::RELATION_SIDE_CHILD
150+
$type === Attribute::TYPE_RELATIONSHIP &&
151+
$relationSide === UtopiaDatabase::RELATION_SIDE_CHILD
147152
) {
148153
continue;
149154
}
150155

151-
$attributeTypes[$key] = $attribute['type'];
156+
$attributeTypes[$key] = $type;
152157

153158
if (
154-
$attribute['type'] === Attribute::TYPE_RELATIONSHIP &&
155-
($attribute['relationType'] ?? '') === 'manyToMany' &&
156-
($attribute['side'] ?? '') === 'parent'
159+
$type === Attribute::TYPE_RELATIONSHIP &&
160+
$relationType === 'manyToMany' &&
161+
$relationSide === 'parent'
157162
) {
158163
$manyToManyKeys[] = $key;
159164
}
165+
166+
if ($isArray && $type !== Attribute::TYPE_RELATIONSHIP) {
167+
$arrayKeys[] = $key;
168+
}
160169
}
161170

162-
$this->withCSVStream(function ($stream) use ($attributeTypes, $manyToManyKeys, $collection, $batchSize) {
171+
$this->withCSVStream(function ($stream) use ($attributeTypes, $manyToManyKeys, $arrayKeys, $collection, $batchSize) {
163172
$headers = fgetcsv($stream);
164173
if (! is_array($headers) || count($headers) === 0) {
165174
return;
@@ -185,24 +194,51 @@ private function exportDocuments(int $batchSize): void
185194
$parsedValue = trim($value);
186195
$type = $attributeTypes[$key] ?? null;
187196

188-
if (! isset($type) || $parsedValue === '') {
197+
if (! isset($type)) {
189198
continue;
190199
}
191200

192201
if (in_array($key, $manyToManyKeys, true)) {
193-
$parsedData[$key] = str_contains($parsedValue, ',')
194-
? array_map('trim', explode(',', $parsedValue))
195-
: [$parsedValue];
202+
$parsedData[$key] = $parsedValue === ''
203+
? []
204+
: array_values(
205+
array_filter(
206+
array_map(
207+
'trim',
208+
explode(',', $parsedValue)
209+
)
210+
)
211+
);
212+
continue;
213+
}
196214

215+
if (in_array($key, $arrayKeys, true)) {
216+
if ($parsedValue === '') {
217+
$parsedData[$key] = [];
218+
} else {
219+
$arrayValues = str_getcsv($parsedValue);
220+
$arrayValues = array_map('trim', $arrayValues);
221+
222+
$parsedData[$key] = array_map(function ($item) use ($type) {
223+
return match ($type) {
224+
Attribute::TYPE_INTEGER => is_numeric($item) ? (int) $item : null,
225+
Attribute::TYPE_FLOAT => is_numeric($item) ? (float) $item : null,
226+
Attribute::TYPE_BOOLEAN => filter_var($item, FILTER_VALIDATE_BOOLEAN),
227+
default => $item,
228+
};
229+
}, $arrayValues);
230+
}
197231
continue;
198232
}
199233

200-
$parsedData[$key] = match ($type) {
201-
Attribute::TYPE_INTEGER => is_numeric($parsedValue) ? (int) $parsedValue : null,
202-
Attribute::TYPE_FLOAT => is_numeric($parsedValue) ? (float) $parsedValue : null,
203-
Attribute::TYPE_BOOLEAN => filter_var($parsedValue, FILTER_VALIDATE_BOOLEAN),
204-
default => $parsedValue,
205-
};
234+
if ($parsedValue !== '') {
235+
$parsedData[$key] = match ($type) {
236+
Attribute::TYPE_INTEGER => is_numeric($parsedValue) ? (int) $parsedValue : null,
237+
Attribute::TYPE_FLOAT => is_numeric($parsedValue) ? (float) $parsedValue : null,
238+
Attribute::TYPE_BOOLEAN => filter_var($parsedValue, FILTER_VALIDATE_BOOLEAN),
239+
default => $parsedValue,
240+
};
241+
}
206242
}
207243

208244
$documentId = $parsedData['$id'] ?? 'unique()';

0 commit comments

Comments
 (0)