From b40ab8213d437954941e8c85880d6e19802871fe Mon Sep 17 00:00:00 2001 From: Franziska Dyckhoff Date: Tue, 4 Feb 2020 19:57:28 +0100 Subject: [PATCH] Export and import column definition of attribute options Add decorators for Akeneo services to enhance the standard CSV and XLSX batch jobs --- .../FlatToStandard/AttributeOption.php | 75 +++++++++++++++ .../StandardToFlat/AttributeOption.php | 29 ++++++ src/Resources/config/array_converters.xml | 12 +++ src/Resources/config/services.xml | 8 ++ src/Resources/config/updaters.xml | 6 ++ src/Updater/AttributeOptionUpdater.php | 92 +++++++++++++++++++ 6 files changed, 222 insertions(+) create mode 100644 src/ArrayConverter/FlatToStandard/AttributeOption.php create mode 100644 src/ArrayConverter/StandardToFlat/AttributeOption.php create mode 100644 src/Updater/AttributeOptionUpdater.php diff --git a/src/ArrayConverter/FlatToStandard/AttributeOption.php b/src/ArrayConverter/FlatToStandard/AttributeOption.php new file mode 100644 index 0000000..9812a25 --- /dev/null +++ b/src/ArrayConverter/FlatToStandard/AttributeOption.php @@ -0,0 +1,75 @@ +baseArrayConverter = $baseArrayConverter; + } + + public function convert(array $item, array $options = []) + { + $convertedItems = []; + $baseItem = []; + + foreach ($item as $field => $data) { + if (preg_match('/^(constraints|type|type_config\-\w+)$/', $field)) { + $convertedItems = $this->convertItem($field, $data, $convertedItems); + } else { + $baseItem[$field] = $data; + } + } + + return array_merge( + $this->baseArrayConverter->convert($baseItem, $options), + $convertedItems + ); + } + + protected function convertItem($field, $data, array $convertedItem) + { + if (!array_key_exists('type_config', $convertedItem)) { + $convertedItem['type_config'] = []; + } + + switch ($field) { + case 'constraints': + $constraints = explode(',', $data); + $convertedItem['constraints'] = array_fill_keys($constraints, []); + break; + + case 'type_config-is_decimal': + if (!empty($data)) { + $convertedItem['type_config']['is_decimal'] = (bool) $data; + } + break; + + case 'type_config-options_url': + if (!empty($data)) { + $convertedItem['type_config']['options_url'] = $data; + } + break; + + case 'type_config-options': + if (!empty($data)) { + $convertedItem['type_config']['options'] = json_decode($data, true); + } + break; + + default: + $convertedItem[$field] = $data; + break; + } + + return $convertedItem; + } +} diff --git a/src/ArrayConverter/StandardToFlat/AttributeOption.php b/src/ArrayConverter/StandardToFlat/AttributeOption.php new file mode 100644 index 0000000..9cc3d7d --- /dev/null +++ b/src/ArrayConverter/StandardToFlat/AttributeOption.php @@ -0,0 +1,29 @@ + $value) { + $convertedItem['type_config-'.$key] = is_array($value) ? json_encode($value) : (string) $value; + } + break; + + default: + $convertedItem = parent::convertProperty($property, $data, $convertedItem, $options); + break; + } + + return $convertedItem; + } +} diff --git a/src/Resources/config/array_converters.xml b/src/Resources/config/array_converters.xml index 033e8f1..8602e60 100644 --- a/src/Resources/config/array_converters.xml +++ b/src/Resources/config/array_converters.xml @@ -18,5 +18,17 @@ flagbit_catalog_table + + + + + + + diff --git a/src/Resources/config/services.xml b/src/Resources/config/services.xml index 8584cfd..c2f15b3 100644 --- a/src/Resources/config/services.xml +++ b/src/Resources/config/services.xml @@ -27,6 +27,14 @@ + + + + + diff --git a/src/Resources/config/updaters.xml b/src/Resources/config/updaters.xml index 76c08c0..22a0b04 100644 --- a/src/Resources/config/updaters.xml +++ b/src/Resources/config/updaters.xml @@ -11,5 +11,11 @@ + + + + diff --git a/src/Updater/AttributeOptionUpdater.php b/src/Updater/AttributeOptionUpdater.php new file mode 100644 index 0000000..6d30d94 --- /dev/null +++ b/src/Updater/AttributeOptionUpdater.php @@ -0,0 +1,92 @@ + $value) { + $this->validateTypeConfig($key, $value); + } + break; + + case 'constraints': + if (!is_array($data)) { + throw InvalidPropertyTypeException::arrayExpected($field, static::class, $data); + } + break; + + default: + break; + } + } + + protected function validateTypeConfig($key, $value) + { + switch ($key) { + case 'is_decimal': + if (!is_bool($value)) { + InvalidPropertyTypeException::booleanExpected('type_config-is_decimal', static::class, $value); + } + break; + + case 'options': + if (!is_array($value)) { + throw InvalidPropertyTypeException::arrayExpected('type_config-options', static::class, $value); + } + break; + + case 'options_url': + if (!is_string($value)) { + InvalidPropertyTypeException::stringExpected('type_config-options_url', static::class, $value); + } + break; + + default: + break; + } + } + + protected function setData(AttributeOptionInterface $attributeOption, $field, $data) + { + parent::setData($attributeOption, $field, $data); + + if ($attributeOption instanceof AttributeOption && $attributeOption->isTableAttribute()) { + if ('type' === $field) { + $attributeOption->setType($data); + } + + if ('type_config' === $field) { + $attributeOption->setTypeConfig($data); + } + + if ('constraints' === $field) { + $attributeOption->setConstraints($data); + } + } + } +}