diff --git a/CHANGELOG.md b/CHANGELOG.md index 8fc8677..7cc3e53 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,15 @@ +# Version 17.8.2 + +## Bugfixes + +* None + +## Features + +* Add #PAC-354 new feature: + * Validation whether the product of the website is assigned to the current store view line + * `TechDivision\Import\Callbacks\StoreWebsiteValidatorCallback` + # Version 17.8.1 ## Bugfixes diff --git a/src/Callbacks/StoreWebsiteValidatorCallback.php b/src/Callbacks/StoreWebsiteValidatorCallback.php new file mode 100644 index 0000000..d4144d3 --- /dev/null +++ b/src/Callbacks/StoreWebsiteValidatorCallback.php @@ -0,0 +1,139 @@ + + * @copyright 2024 TechDivision GmbH + * @license https://opensource.org/licenses/MIT + * @link https://github.com/techdivision/import + * @link http://www.techdivision.com + */ + +namespace TechDivision\Import\Callbacks; + +use TechDivision\Import\Loaders\LoaderInterface; +use TechDivision\Import\Services\ImportProcessorInterface; +use TechDivision\Import\Utils\MemberNames; + +/** + * storeview validator callback implementation. + * + * @author MET + * @copyright 2024 TechDivision GmbH + * @license https://opensource.org/licenses/MIT + * @link https://github.com/techdivision/import + * @link http://www.techdivision.com + */ +class StoreWebsiteValidatorCallback extends ArrayValidatorCallback +{ + + /** + * The flag to query whether or not the value can be empty. + * + * @var boolean + */ + protected $nullable = false; + + /** + * The flag to query whether or not the value has to be validated on the main row only. + * + * @var boolean + */ + protected $mainRowOnly = false; + + /** + * The flag to query whether or not the value has to be ignored global strict mode configuration. + * + * @var boolean + */ + protected $ignoreStrictMode = false; + + /** + * The store websites. + * + * @var array + */ + protected $storeWebsites = array(); + + /** + * Initializes the callback with the loader instance. + * + * @param \TechDivision\Import\Loaders\LoaderInterface $storeLoader The loader instance to load the validations with + * @param ImportProcessorInterface $importProcessor The loader instance to load the validations with + * @param boolean $nullable The flag to decide whether or not the value can be empty + * @param boolean $mainRowOnly The flag to decide whether or not the value has to be validated on the main row only + * @param boolean $ignoreStrictMode The flag to query whether or not the value has to be ignored global strict mode configuration. + */ + public function __construct(LoaderInterface $storeLoader, ImportProcessorInterface $importProcessor, $nullable = false, $mainRowOnly = false, $ignoreStrictMode = true) + { + + // pass the loader to the parent instance + parent::__construct($storeLoader); + + // initialize the flags with the passed values + $this->nullable = $nullable; + $this->mainRowOnly = $mainRowOnly; + $this->ignoreStrictMode = $ignoreStrictMode; + + // load the store websites + $storeWebsites = $importProcessor->getStoreWebsites(); + + // initialize the array with the store websites + foreach ($storeWebsites as $storeWebsite) { + $this->storeWebsites[$storeWebsite[MemberNames::CODE]] = $storeWebsite[MemberNames::WEBSITE_ID]; + } + } + + /** + * Will be invoked by a observer it has been registered for. + * + * @param string|null $attributeCode The code of the attribute that has to be validated + * @param string|null $attributeValue The attribute value to be validated + * + * @return mixed The modified value + */ + public function handle($attributeCode = null, $attributeValue = null) + { + + // the validations for the attribute with the given code + $validations = $this->getValidations($attributeCode); + + // query whether or not the passed value IS empty and empty + // values are allowed + if ($this->isNullable($attributeValue)) { + return; + } + + $website = $this->load(); + $productWebsite = $this->getSubject()->getValue('product_websites'); + $message = sprintf( + 'The store "%s" does not belong to the website "%s" . Please check your data.', + $attributeValue, + $productWebsite + ); + + if ($validations[$attributeValue][MemberNames::WEBSITE_ID] === $website[$productWebsite]) { + return; + } else { + if ($this->hasHandleStrictMode($attributeCode, $message)) { + return; + } + } + + // throw an exception if the store not in Website + throw new \InvalidArgumentException($message); + } + + /** + * Loads and returns data. + * + * @return \ArrayAccess The array with the data + */ + public function load() + { + return $this->storeWebsites; + } +} diff --git a/symfony/Resources/config/services.xml b/symfony/Resources/config/services.xml index 7d08756..1dfd50a 100644 --- a/symfony/Resources/config/services.xml +++ b/symfony/Resources/config/services.xml @@ -885,6 +885,14 @@ %array_validator.nullable.true% + + + + %array_validator.nullable.true% + %array_validator.nullable.false% + %array_validator.nullable.false% + +