|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* TechDivision\Import\Callbacks\SelectValidatorCallback |
|
5
|
|
|
* |
|
6
|
|
|
* NOTICE OF LICENSE |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
|
9
|
|
|
* that is available through the world-wide-web at this URL: |
|
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
|
11
|
|
|
* |
|
12
|
|
|
* PHP version 5 |
|
13
|
|
|
* |
|
14
|
|
|
* @author Tim Wagner <[email protected]> |
|
15
|
|
|
* @copyright 2019 TechDivision GmbH <[email protected]> |
|
16
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
17
|
|
|
* @link https://github.com/techdivision/import-product |
|
18
|
|
|
* @link http://www.techdivision.com |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
namespace TechDivision\Import\Callbacks; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* A callback implementation that validates the option values of EAV select attributes. |
|
25
|
|
|
* |
|
26
|
|
|
* @author Tim Wagner <[email protected]> |
|
27
|
|
|
* @copyright 2019 TechDivision GmbH <[email protected]> |
|
28
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
29
|
|
|
* @link https://github.com/techdivision/import-product |
|
30
|
|
|
* @link http://www.techdivision.com |
|
31
|
|
|
*/ |
|
32
|
|
View Code Duplication |
class SelectValidatorCallback extends IndexedArrayValidatorCallback |
|
|
|
|
|
|
33
|
|
|
{ |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Will be invoked by the observer it has been registered for. |
|
37
|
|
|
* |
|
38
|
|
|
* @param string|null $attributeCode The code of the attribute that has to be validated |
|
39
|
|
|
* @param string|null $attributeValue The attribute value to be validated |
|
40
|
|
|
* |
|
41
|
|
|
* @return mixed The modified value |
|
42
|
|
|
*/ |
|
43
|
|
|
public function handle($attributeCode = null, $attributeValue = null) |
|
44
|
|
|
{ |
|
45
|
|
|
|
|
46
|
|
|
// load the subject instance |
|
47
|
|
|
$subject = $this->getSubject(); |
|
48
|
|
|
|
|
49
|
|
|
// explode the additional attributes |
|
50
|
|
|
if ($this->isNullable($optionValues = $subject->explode($attributeValue, '='))) { |
|
|
|
|
|
|
51
|
|
|
return; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$validations = $this->getValidations($attributeCode); |
|
55
|
|
|
|
|
56
|
|
|
// iterate over the attributes and append them to the row |
|
57
|
|
|
foreach ($optionValues as $optionValue) { |
|
58
|
|
|
|
|
59
|
|
|
if (in_array($optionValue, $validations)) { |
|
60
|
|
|
continue; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
// throw an exception if the value is NOT in the array |
|
64
|
|
|
throw new \InvalidArgumentException( |
|
65
|
|
|
sprintf('Found invalid option value "%s" for attribute with code "%s"', $optionValue, $attributeCode) |
|
66
|
|
|
); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.