1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* TechDivision\Import\Converter\Callbacks\TaxClassValidatorCallback |
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\Product\Callbacks; |
22
|
|
|
|
23
|
|
|
use TechDivision\Import\Callbacks\AbstractValidatorCallback; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Tax class validator callback implementation. |
27
|
|
|
* |
28
|
|
|
* @author Tim Wagner <[email protected]> |
29
|
|
|
* @copyright 2019 TechDivision GmbH <[email protected]> |
30
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
31
|
|
|
* @link https://github.com/techdivision/import-product |
32
|
|
|
* @link http://www.techdivision.com |
33
|
|
|
*/ |
34
|
|
|
class TaxClassValidatorCallback extends AbstractValidatorCallback |
35
|
|
|
{ |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Will be invoked by a observer it has been registered for. |
39
|
|
|
* |
40
|
|
|
* @param string|null $attributeCode The code of the attribute that has to be validated |
41
|
|
|
* @param string|null $attributeValue The attribute value to be validated |
42
|
|
|
* |
43
|
|
|
* @return mixed The modified value |
44
|
|
|
*/ |
45
|
|
|
public function handle($attributeCode = null, $attributeValue = null) |
46
|
|
|
{ |
47
|
|
|
|
48
|
|
|
// the validations for the attribute with the given code |
49
|
|
|
$validations = $this->getValidations(); |
50
|
|
|
|
51
|
|
|
// if the passed value is in the array, return immediately |
52
|
|
|
if (in_array($attributeValue, $validations) || $this->isNullable($attributeValue)) { |
53
|
|
|
return; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
// throw an exception if the value is NOT in the array |
57
|
|
|
throw new \InvalidArgumentException( |
58
|
|
|
sprintf( |
59
|
|
|
'Found invalid value "%s" for column "%s" (must be one of: "%s")', |
60
|
|
|
$attributeValue, |
61
|
|
|
$attributeCode, |
62
|
|
|
implode(', ', $validations) |
63
|
|
|
) |
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Query whether or not the passed value IS empty and empty values are allowed. |
69
|
|
|
* |
70
|
|
|
* @param string $attributeValue The attribute value to query for |
71
|
|
|
* |
72
|
|
|
* @return boolean TRUE if empty values are allowed and the passed value IS empty |
73
|
|
|
*/ |
74
|
|
|
protected function isNullable($attributeValue) |
75
|
|
|
{ |
76
|
|
|
return $this->isMainRow() && ($attributeValue === '' || $attributeValue === null); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|