Skip to content

Commit fe6b511

Browse files
committed
ACPT-1429: Stabilize Import Product
1 parent c953b4d commit fe6b511

File tree

3 files changed

+79
-55
lines changed

3 files changed

+79
-55
lines changed

app/code/Magento/CatalogImportExport/Model/Import/Product/Validator.php

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -254,35 +254,14 @@ public function isAttributeValid($attrCode, array $attrParams, array $rowData)
254254
*/
255255
private function validateByAttributeType(string $attrCode, array $attrParams, array $rowData): bool
256256
{
257-
switch ($attrParams['type']) {
258-
case 'varchar':
259-
case 'text':
260-
$valid = $this->textValidation($attrCode, $attrParams['type']);
261-
break;
262-
case 'decimal':
263-
case 'int':
264-
$valid = $this->numericValidation($attrCode, $attrParams['type']);
265-
break;
266-
case 'select':
267-
case 'boolean':
268-
$valid = $this->validateOption($attrCode, $attrParams['options'], $rowData[$attrCode]);
269-
break;
270-
case 'multiselect':
271-
$valid = $this->validateMultiselect($attrCode, $attrParams['options'], $rowData[$attrCode]);
272-
break;
273-
case 'datetime':
274-
$val = trim($rowData[$attrCode]);
275-
$valid = strtotime($val) !== false;
276-
if (!$valid) {
277-
$this->_addMessages([RowValidatorInterface::ERROR_INVALID_ATTRIBUTE_TYPE]);
278-
}
279-
break;
280-
default:
281-
$valid = true;
282-
break;
283-
}
284-
285-
return $valid;
257+
return match ($attrParams['type']) {
258+
'varchar', 'text' => $this->textValidation($attrCode, $attrParams['type']),
259+
'decimal', 'int' => $this->numericValidation($attrCode, $attrParams['type']),
260+
'select', 'boolean' => $this->validateOption($attrCode, $attrParams['options'], $rowData[$attrCode]),
261+
'multiselect' => $this->validateMultiselect($attrCode, $attrParams['options'], $rowData[$attrCode]),
262+
'datetime' => $this->validateDateTime($rowData[$attrCode]),
263+
default => true,
264+
};
286265
}
287266

288267
/**
@@ -314,6 +293,22 @@ private function validateMultiselect(string $attrCode, array $options, array|str
314293
return $valid;
315294
}
316295

296+
/**
297+
* Validate datetime attribute.
298+
*
299+
* @param string $rowData
300+
* @return bool
301+
*/
302+
private function validateDateTime(string $rowData): bool
303+
{
304+
$val = trim($rowData);
305+
$valid = strtotime($val) !== false;
306+
if (!$valid) {
307+
$this->_addMessages([RowValidatorInterface::ERROR_INVALID_ATTRIBUTE_TYPE]);
308+
}
309+
return $valid;
310+
}
311+
317312
/**
318313
* Set invalid attribute
319314
*

app/code/Magento/CatalogImportExport/Model/Import/Product/Validator/Media.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
use Magento\CatalogImportExport\Model\Import\Product\RowValidatorInterface;
99
use Magento\Framework\App\ObjectManager;
10+
use Magento\Framework\Filesystem\Io\File;
1011
use Magento\Framework\Url\Validator;
1112

1213
class Media extends AbstractImportValidator implements RowValidatorInterface
@@ -29,11 +30,20 @@ class Media extends AbstractImportValidator implements RowValidatorInterface
2930
private $validator;
3031

3132
/**
32-
* @param Validator $validator The url validator
33+
* @var File
3334
*/
34-
public function __construct(Validator $validator = null)
35-
{
35+
private File $file;
36+
37+
/**
38+
* @param Validator|null $validator The url validator
39+
* @param File|null $file
40+
*/
41+
public function __construct(
42+
Validator $validator = null,
43+
File $file = null
44+
) {
3645
$this->validator = $validator ?: ObjectManager::getInstance()->get(Validator::class);
46+
$this->file = $file ?: ObjectManager::getInstance()->get(File::class);
3747
}
3848

3949
/**
@@ -79,7 +89,7 @@ protected function checkPath($string)
7989
*/
8090
protected function checkFileExists($path)
8191
{
82-
return file_exists($path);
92+
return $this->file->fileExists($path);
8393
}
8494

8595
/**

lib/internal/Magento/Framework/Webapi/ServiceInputProcessor.php

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
use Magento\Framework\Api\SimpleDataObjectConverter;
1414
use Magento\Framework\App\ObjectManager;
1515
use Magento\Framework\Exception\InputException;
16+
use Magento\Framework\Exception\InvalidArgumentException;
17+
use Magento\Framework\Exception\LocalizedException;
1618
use Magento\Framework\Exception\SerializationException;
1719
use Magento\Framework\ObjectManager\ConfigInterface;
1820
use Magento\Framework\ObjectManagerInterface;
@@ -37,7 +39,7 @@ class ServiceInputProcessor implements ServicePayloadConverterInterface
3739
public const EXTENSION_ATTRIBUTES_TYPE = \Magento\Framework\Api\ExtensionAttributesInterface::class;
3840

3941
/**
40-
* @var \Magento\Framework\Reflection\TypeProcessor
42+
* @var TypeProcessor
4143
*/
4244
protected $typeProcessor;
4345

@@ -220,7 +222,7 @@ public function process($serviceClassName, $serviceMethodName, array $inputArray
220222
* @param array $data
221223
* @return array
222224
* @throws \ReflectionException
223-
* @throws \Magento\Framework\Exception\LocalizedException
225+
* @throws LocalizedException
224226
*/
225227
private function getConstructorData(string $className, array $data): array
226228
{
@@ -498,34 +500,51 @@ protected function _createDataObjectForTypeAndArrayValue($type, $customAttribute
498500
* @param mixed $data
499501
* @param string $type Convert given value to the this type
500502
* @return mixed
501-
* @throws \Magento\Framework\Exception\LocalizedException
503+
* @throws LocalizedException
502504
*/
503505
public function convertValue($data, $type)
504506
{
505-
$isArrayType = $this->typeProcessor->isArrayType($type);
506-
if ($isArrayType && isset($data['item'])) {
507+
if ($this->typeProcessor->isArrayType($type) && isset($data['item'])) {
507508
$data = $this->_removeSoapItemNode($data);
508509
}
510+
509511
if ($this->typeProcessor->isTypeSimple($type) || $this->typeProcessor->isTypeAny($type)) {
510-
$result = $this->typeProcessor->processSimpleAndAnyType($data, $type);
511-
} elseif ($type == 'UnstructuredArray') {
512-
$result = $data;
513-
} else {
514-
/** Complex type or array of complex types */
515-
if ($isArrayType) {
516-
// Initializing the result for array type else it will return null for empty array
517-
$result = is_array($data) ? [] : null;
518-
$itemType = $this->typeProcessor->getArrayItemType($type);
519-
if (is_array($data)) {
520-
$this->serviceInputValidator->validateComplexArrayType($itemType, $data);
521-
foreach ($data as $key => $item) {
522-
$result[$key] = $this->_createFromArray($itemType, $item);
523-
}
524-
}
525-
} else {
526-
$result = $this->_createFromArray($type, $data);
512+
return $this->typeProcessor->processSimpleAndAnyType($data, $type);
513+
}
514+
515+
if ($type == TypeProcessor::UNSTRUCTURED_ARRAY) {
516+
return $data;
517+
}
518+
519+
return $this->processComplexTypes($data, $type);
520+
}
521+
522+
/**
523+
* Process complex types or array of complex types.
524+
*
525+
* @param mixed $data
526+
* @param string $type
527+
* @return array|object|SearchCriteriaInterface
528+
* @throws SerializationException
529+
* @throws InvalidArgumentException
530+
*/
531+
private function processComplexTypes($data, $type) {
532+
$isArrayType = $this->typeProcessor->isArrayType($type);
533+
534+
if (!$isArrayType) {
535+
return $this->_createFromArray($type, $data);
536+
}
537+
538+
$result = is_array($data) ? [] : null;
539+
$itemType = $this->typeProcessor->getArrayItemType($type);
540+
541+
if (is_array($data)) {
542+
$this->serviceInputValidator->validateComplexArrayType($itemType, $data);
543+
foreach ($data as $key => $item) {
544+
$result[$key] = $this->_createFromArray($itemType, $item);
527545
}
528546
}
547+
529548
return $result;
530549
}
531550

0 commit comments

Comments
 (0)