¨4.0.1¨
This commit is contained in:
@@ -18,6 +18,7 @@ class ProductImport implements OnEachRow, WithChunkReading, WithHeadingRow
|
||||
return 200;
|
||||
}
|
||||
|
||||
|
||||
public function onRow(Row $row)
|
||||
{
|
||||
$data = $this->normalize($row->toArray());
|
||||
@@ -26,11 +27,12 @@ class ProductImport implements OnEachRow, WithChunkReading, WithHeadingRow
|
||||
|
||||
try {
|
||||
Product::create($data);
|
||||
} catch (QueryException | ValidationException $e) {
|
||||
} catch (QueryException|ValidationException $e) {
|
||||
session()->push('importer_errors', $row->getIndex());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private function normalize(array $data)
|
||||
{
|
||||
return array_filter([
|
||||
@@ -65,6 +67,7 @@ class ProductImport implements OnEachRow, WithChunkReading, WithHeadingRow
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
private function explode($values)
|
||||
{
|
||||
if (trim($values) == '') {
|
||||
@@ -74,6 +77,7 @@ class ProductImport implements OnEachRow, WithChunkReading, WithHeadingRow
|
||||
return array_map('trim', explode(',', $values));
|
||||
}
|
||||
|
||||
|
||||
private function normalizeFiles(array $data)
|
||||
{
|
||||
return [
|
||||
@@ -82,6 +86,7 @@ class ProductImport implements OnEachRow, WithChunkReading, WithHeadingRow
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
private function normalizeMetaData($data)
|
||||
{
|
||||
return [
|
||||
@@ -90,6 +95,7 @@ class ProductImport implements OnEachRow, WithChunkReading, WithHeadingRow
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
private function normalizeAttributes(array $data)
|
||||
{
|
||||
$attributes = [];
|
||||
@@ -104,15 +110,17 @@ class ProductImport implements OnEachRow, WithChunkReading, WithHeadingRow
|
||||
return $attributes;
|
||||
}
|
||||
|
||||
|
||||
private function findAttributes(array $data)
|
||||
{
|
||||
return collect($data)->filter(function ($value, $column) {
|
||||
preg_match('/^attribute_\d$/', $column, $matches);
|
||||
|
||||
return ! empty($matches);
|
||||
return !empty($matches);
|
||||
})->filter();
|
||||
}
|
||||
|
||||
|
||||
private function findAttributeValues(array $data, $attributeNumber)
|
||||
{
|
||||
return collect($data)->filter(function ($value, $column) use ($attributeNumber) {
|
||||
@@ -122,6 +130,7 @@ class ProductImport implements OnEachRow, WithChunkReading, WithHeadingRow
|
||||
})->flatten()->toArray();
|
||||
}
|
||||
|
||||
|
||||
private function normalizeOptions(array $data)
|
||||
{
|
||||
$options = [];
|
||||
@@ -144,23 +153,25 @@ class ProductImport implements OnEachRow, WithChunkReading, WithHeadingRow
|
||||
return $options;
|
||||
}
|
||||
|
||||
|
||||
private function findOptionPrefixes(array $data)
|
||||
{
|
||||
return collect($data)->filter(function ($value, $column) {
|
||||
preg_match('/^option_\d_name$/', $column, $matches);
|
||||
|
||||
return ! empty($matches);
|
||||
return !empty($matches);
|
||||
})->keys()->map(function ($column) {
|
||||
return str_replace('_name', '', $column);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
private function findOptionAttributes(array $data, $optionPrefix)
|
||||
{
|
||||
return collect($data)->filter(function ($value, $column) use ($optionPrefix) {
|
||||
preg_match("/{$optionPrefix}_.*/", $column, $matches);
|
||||
|
||||
return ! empty($matches);
|
||||
return !empty($matches);
|
||||
})->mapWithKeys(function ($value, $column) use ($optionPrefix) {
|
||||
$column = str_replace("{$optionPrefix}_", '', $column);
|
||||
|
||||
@@ -168,6 +179,7 @@ class ProductImport implements OnEachRow, WithChunkReading, WithHeadingRow
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
private function findOptionValues(Collection $option)
|
||||
{
|
||||
$values = [];
|
||||
@@ -189,12 +201,13 @@ class ProductImport implements OnEachRow, WithChunkReading, WithHeadingRow
|
||||
return $values;
|
||||
}
|
||||
|
||||
|
||||
private function findOptionValuePrefixes(Collection $option)
|
||||
{
|
||||
return $option->filter(function ($value, $column) {
|
||||
preg_match('/value_\d_.+/', $column, $matches);
|
||||
|
||||
return ! empty($matches);
|
||||
return !empty($matches);
|
||||
})->keys()->map(function ($column) {
|
||||
preg_match('/value_\d/', $column, $matches);
|
||||
|
||||
@@ -202,12 +215,13 @@ class ProductImport implements OnEachRow, WithChunkReading, WithHeadingRow
|
||||
})->unique();
|
||||
}
|
||||
|
||||
|
||||
private function findOptionValueAttributes(Collection $option, $valuePrefix)
|
||||
{
|
||||
return $option->filter(function ($value, $column) use ($valuePrefix) {
|
||||
preg_match("/{$valuePrefix}_.*/", $column, $matches);
|
||||
|
||||
return ! empty($matches);
|
||||
return !empty($matches);
|
||||
})->mapWithKeys(function ($value, $column) use ($valuePrefix) {
|
||||
$column = str_replace("{$valuePrefix}_", '', $column);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user