-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PAC-354: Add new Callback to validate store-view and website
- Loading branch information
Showing
3 changed files
with
159 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
<?php | ||
|
||
/** | ||
* TechDivision\Import\Callbacks\ArrayValidatorCallback | ||
* | ||
* PHP version 7 | ||
* | ||
* @author MET <[email protected]> | ||
* @copyright 2024 TechDivision GmbH <[email protected]> | ||
* @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 <[email protected]> | ||
* @copyright 2024 TechDivision GmbH <[email protected]> | ||
* @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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters