/
TaxCreationForm.php
149 lines (130 loc) · 4.82 KB
/
TaxCreationForm.php
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
<?php
/*
* This file is part of the Thelia package.
* http://www.thelia.net
*
* (c) OpenStudio <info@thelia.net>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Thelia\Form;
use Symfony\Component\DependencyInjection\Attribute\TaggedIterator;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Validator\Constraints;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use Thelia\Core\Translation\Translator;
use Thelia\Model\Tax;
use Thelia\TaxEngine\TaxTypeRequirementDefinition;
use Thelia\Type\TypeInterface;
/**
* Class TaxCreationForm.
*
* @author Etienne Roudeix <eroudeix@openstudio.fr>
*/
class TaxCreationForm extends BaseForm
{
use StandardDescriptionFieldsTrait;
private iterable $taxTypeIterator;
public function __construct(
#[TaggedIterator('thelia.taxType')] iterable $taxTypeIterator
) {
$this->taxTypeIterator = $taxTypeIterator;
}
protected static $typeList = [];
protected function buildForm(): void
{
$typeList = [];
$requirementList = [];
foreach ($this->taxTypeIterator as $taxType) {
$typeList[$taxType->getTitle()] = Tax::escapeTypeName($taxType::class);
$requirementList[$taxType::class] = $taxType->getRequirementsDefinition();
}
$this->formBuilder
->add(
'locale',
HiddenType::class,
[
'constraints' => [new NotBlank()],
]
)
->add(
'type',
ChoiceType::class,
[
'choices' => $typeList,
'required' => true,
'constraints' => [
new Constraints\NotBlank(),
],
'label' => Translator::getInstance()->trans('Type'),
'label_attr' => ['for' => 'type_field'],
]
)
;
foreach ($requirementList as $name => $requirements) {
/** @var TaxTypeRequirementDefinition $requirement */
foreach ($requirements as $requirement) {
if (!isset(self::$typeList[$requirement->getName()])) {
self::$typeList[$requirement->getName()] = $requirement->getType();
}
$options = array_merge([
'constraints' => [
new Constraints\Callback([$this, 'checkRequirementField']),
],
'attr' => [
'tag' => 'requirements',
'tax_type' => Tax::escapeTypeName($name),
],
'label_attr' => [
'type' => $requirement->getName(),
],
'label' => Translator::getInstance()->trans($requirement->getTitle()),
],
$requirement->getType()->getFormOptions()
);
$this->formBuilder
// Replace the '\' in the class name by hyphens
// See TaxController::getRequirements if some changes are made about this.
->add(
Tax::escapeTypeName($name).':'.$requirement->getName(),
$requirement->getType()->getFormType(),
$options
);
}
}
$this->addStandardDescFields(['postscriptum', 'chapo', 'locale']);
}
public function checkRequirementField($value, ExecutionContextInterface $context): void
{
$data = $context->getRoot()->getData();
$type = $data['type'];
if (false !== strpos($context->getPropertyPath(), $type)) {
// extract requirement type
if (preg_match('@\:(.+)\]@', $context->getPropertyPath(), $matches)) {
$requirementType = $matches[1];
if (isset(self::$typeList[$requirementType])) {
/** @var TypeInterface $typeClass */
$typeClass = self::$typeList[$requirementType];
$typeClass->verifyForm($value, $context);
return;
}
}
$context->addViolation(
Translator::getInstance()->trans(
'Impossible to check value `%value` for `%type` type',
[
'%value' => $value,
'%type' => $type,
]
)
);
}
}
public static function getName()
{
return 'thelia_tax_creation';
}
}