1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Tax\Model\Calculation\Rule;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Tax\Model\ClassModel as TaxClassModel;
use Magento\Tax\Model\ClassModelRegistry;
use Zend_Validate_Exception;
class Validator extends \Magento\Framework\Validator\AbstractValidator
{
/**
* @var ClassModelRegistry
*/
protected $classModelRegistry;
/**
* @param ClassModelRegistry $classModelRegistry
*/
public function __construct(ClassModelRegistry $classModelRegistry)
{
$this->classModelRegistry = $classModelRegistry;
}
/**
* Validate rule model
*
* @param \Magento\Tax\Model\Calculation\Rule $value
* @return boolean
* @throws Zend_Validate_Exception If validation of $value is impossible
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function isValid($value)
{
$messages = [];
// Position is required and must be 0 or greater
if (!\Zend_Validate::is(trim($value->getPosition()), 'NotEmpty')) {
$this->addErrorMessage(
$messages,
'"%fieldName" is required. Enter and try again.',
['fieldName' => 'position']
);
}
if (!\Zend_Validate::is(trim($value->getPosition()), 'GreaterThan', [-1])) {
$this->addErrorMessage(
$messages,
'The %fieldName value of "%value" must be greater than or equal to %minValue.',
['fieldName' => 'position', 'value' => $value->getPosition(), 'minValue' => 0]
);
}
// Priority is required and must be 0 or greater
if (!\Zend_Validate::is(trim($value->getPriority()), 'NotEmpty')) {
$this->addErrorMessage(
$messages,
'"%fieldName" is required. Enter and try again.',
['fieldName' => 'priority']
);
}
if (!\Zend_Validate::is(trim($value->getPriority()), 'GreaterThan', [-1])) {
$this->addErrorMessage(
$messages,
'The %fieldName value of "%value" must be greater than or equal to %minValue.',
['fieldName' => 'priority', 'value' => $value->getPriority(), 'minValue' => 0]
);
}
// Code is required
if (!\Zend_Validate::is(trim($value->getCode()), 'NotEmpty')) {
$this->addErrorMessage(
$messages,
'"%fieldName" is required. Enter and try again.',
['fieldName' => 'code']
);
}
// customer tax class ids is required
if (($value->getCustomerTaxClassIds() === null) || !$value->getCustomerTaxClassIds()) {
$this->addErrorMessage(
$messages,
'"%fieldName" is required. Enter and try again.',
['fieldName' => 'customer_tax_class_ids']
);
} else { // see if the customer tax class ids exist
$customerTaxClassIds = $value->getCustomerTaxClassIds();
foreach ($customerTaxClassIds as $customerTaxClassId) {
try {
$taxClass = $this->classModelRegistry->retrieve($customerTaxClassId);
if ($taxClass === null || !($taxClass->getClassType() == TaxClassModel::TAX_CLASS_TYPE_CUSTOMER)) {
$this->addErrorMessage(
$messages,
'No such entity with %fieldName = %fieldValue',
[
'fieldName' => 'customer_tax_class_ids',
'value' => $customerTaxClassId,
]
);
}
} catch (NoSuchEntityException $e) {
$this->addErrorMessage(
$messages,
$e->getRawMessage(),
$e->getParameters()
);
}
}
}
// product tax class ids is required
if (($value->getProductTaxClassIds() === null) || !$value->getProductTaxClassIds()) {
$this->addErrorMessage(
$messages,
'"%fieldName" is required. Enter and try again.',
['fieldName' => 'product_tax_class_ids']
);
} else { // see if the product tax class ids exist
$productTaxClassIds = $value->getProductTaxClassIds();
foreach ($productTaxClassIds as $productTaxClassId) {
try {
$taxClass = $this->classModelRegistry->retrieve($productTaxClassId);
if ($taxClass === null || !($taxClass->getClassType() == TaxClassModel::TAX_CLASS_TYPE_PRODUCT)) {
$this->addErrorMessage(
$messages,
'No such entity with %fieldName = %fieldValue',
[
'fieldName' => 'product_tax_class_ids',
'value' => $productTaxClassId,
]
);
}
} catch (NoSuchEntityException $e) {
$this->addErrorMessage(
$messages,
$e->getRawMessage(),
$e->getParameters()
);
}
}
}
// tax rate ids is required
if (($value->getTaxRateIds() === null) || !$value->getTaxRateIds()) {
$this->addErrorMessage(
$messages,
'"%fieldName" is required. Enter and try again.',
['fieldName' => 'tax_rate_ids']
);
}
$this->_addMessages($messages);
return empty($messages);
}
/**
* Format error message
*
* @param string[] $messages
* @param string $message
* @param array $params
* @return void
*/
protected function addErrorMessage(&$messages, $message, $params)
{
$messages[$params['fieldName']] = __($message, $params);
}
}