1 | <?php |
||
2 | |||
3 | /** |
||
4 | * Copyright © 2016-present Spryker Systems GmbH. All rights reserved. |
||
5 | * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file. |
||
6 | */ |
||
7 | |||
8 | namespace Spryker\Zed\Discount\Communication\Form; |
||
9 | |||
10 | use Generated\Shared\Transfer\DiscountVoucherTransfer; |
||
11 | use Spryker\Zed\Discount\Communication\Form\Constraint\Sequentially; |
||
12 | use Spryker\Zed\Gui\Communication\Form\Type\FormattedNumberType; |
||
13 | use Spryker\Zed\Kernel\Communication\Form\AbstractType; |
||
14 | use Symfony\Component\Form\Extension\Core\Type\ChoiceType; |
||
15 | use Symfony\Component\Form\Extension\Core\Type\HiddenType; |
||
16 | use Symfony\Component\Form\Extension\Core\Type\SubmitType; |
||
17 | use Symfony\Component\Form\Extension\Core\Type\TextType; |
||
18 | use Symfony\Component\Form\FormBuilderInterface; |
||
19 | use Symfony\Component\OptionsResolver\OptionsResolver; |
||
20 | use Symfony\Component\Validator\Constraints\Range; |
||
21 | use Symfony\Component\Validator\Constraints\Regex; |
||
22 | |||
23 | /** |
||
24 | * @method \Spryker\Zed\Discount\Business\DiscountFacadeInterface getFacade() |
||
25 | * @method \Spryker\Zed\Discount\Communication\DiscountCommunicationFactory getFactory() |
||
26 | * @method \Spryker\Zed\Discount\Persistence\DiscountQueryContainerInterface getQueryContainer() |
||
27 | * @method \Spryker\Zed\Discount\DiscountConfig getConfig() |
||
28 | * @method \Spryker\Zed\Discount\Persistence\DiscountRepositoryInterface getRepository() |
||
29 | */ |
||
30 | class VoucherForm extends AbstractType |
||
31 | { |
||
32 | /** |
||
33 | * @var string |
||
34 | */ |
||
35 | public const OPTION_LOCALE = 'locale'; |
||
36 | |||
37 | /** |
||
38 | * @var string |
||
39 | */ |
||
40 | public const FIELD_QUANTITY = 'quantity'; |
||
41 | |||
42 | /** |
||
43 | * @var string |
||
44 | */ |
||
45 | public const FIELD_CUSTOM_CODE = 'custom_code'; |
||
46 | |||
47 | /** |
||
48 | * @var string |
||
49 | */ |
||
50 | public const FIELD_RANDOM_GENERATED_CODE_LENGTH = 'random_generated_code_length'; |
||
51 | |||
52 | /** |
||
53 | * @var string |
||
54 | */ |
||
55 | public const FIELD_MAX_NUMBER_OF_USES = 'max_number_of_uses'; |
||
56 | |||
57 | /** |
||
58 | * @var string |
||
59 | */ |
||
60 | public const FIELD_ID_DISCOUNT = 'id_discount'; |
||
61 | |||
62 | /** |
||
63 | * @var string |
||
64 | */ |
||
65 | protected const FIELD_QUANTITY_ERROR_MESSAGE = 'Invalid entry. Please enter an integer %min%-%max%'; |
||
66 | |||
67 | /** |
||
68 | * @var string |
||
69 | */ |
||
70 | protected const FIELD_QUANTITY_HELP_MESSAGE = 'Enter an integer %min%-%max%.'; |
||
71 | |||
72 | /** |
||
73 | * @var string |
||
74 | */ |
||
75 | protected const PARAM_QUANTITY_MESSAGE_MIN = '%min%'; |
||
76 | |||
77 | /** |
||
78 | * @var string |
||
79 | */ |
||
80 | protected const PARAM_QUANTITY_MESSAGE_MAX = '%max%'; |
||
81 | |||
82 | /** |
||
83 | * @param \Symfony\Component\Form\FormBuilderInterface $builder |
||
84 | * @param array<string, string> $options |
||
85 | * |
||
86 | * @return void |
||
87 | */ |
||
88 | public function buildForm(FormBuilderInterface $builder, array $options): void |
||
89 | { |
||
90 | $this->addQuantityField($builder, $options) |
||
91 | ->addCustomCodeField($builder) |
||
92 | ->addRandomGeneratedCodeLength($builder) |
||
93 | ->addMaxNumberOfUsesField($builder) |
||
94 | ->addIdDiscount($builder) |
||
95 | ->addSubmitButton($builder); |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * @param \Symfony\Component\OptionsResolver\OptionsResolver $resolver |
||
100 | * |
||
101 | * @return void |
||
102 | */ |
||
103 | public function configureOptions(OptionsResolver $resolver): void |
||
104 | { |
||
105 | parent::configureOptions($resolver); |
||
106 | |||
107 | $resolver->setDefaults([ |
||
108 | 'data_class' => DiscountVoucherTransfer::class, |
||
109 | static::OPTION_LOCALE => null, |
||
110 | ]); |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * @param \Symfony\Component\Form\FormBuilderInterface $builder |
||
115 | * @param array<string, string> $options |
||
116 | * |
||
117 | * @return $this |
||
118 | */ |
||
119 | protected function addQuantityField(FormBuilderInterface $builder, array $options) |
||
120 | { |
||
121 | $minValue = $this->getConfig()->getVoucherCodesQuantityMinValue(); |
||
122 | $maxValue = $this->getConfig()->getVoucherCodesQuantityMaxValue(); |
||
123 | |||
124 | $quantityHelpMessage = $this->getFactory() |
||
125 | ->getTranslatorFacade() |
||
126 | ->trans(static::FIELD_QUANTITY_HELP_MESSAGE, [ |
||
127 | static::PARAM_QUANTITY_MESSAGE_MIN => $minValue, |
||
128 | static::PARAM_QUANTITY_MESSAGE_MAX => $maxValue, |
||
129 | ]); |
||
130 | |||
131 | $builder->add(static::FIELD_QUANTITY, FormattedNumberType::class, [ |
||
132 | 'label' => 'Quantity', |
||
133 | 'locale' => $options[static::OPTION_LOCALE], |
||
134 | 'help' => $quantityHelpMessage, |
||
135 | 'attr' => [ |
||
136 | 'min' => $minValue, |
||
137 | 'max' => $maxValue, |
||
138 | ], |
||
139 | 'constraints' => $this->getQuantityFiledConstraints($minValue, $maxValue), |
||
140 | ]); |
||
141 | |||
142 | return $this; |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * @param \Symfony\Component\Form\FormBuilderInterface $builder |
||
147 | * |
||
148 | * @return $this |
||
149 | */ |
||
150 | protected function addCustomCodeField(FormBuilderInterface $builder) |
||
151 | { |
||
152 | $builder->add( |
||
153 | static::FIELD_CUSTOM_CODE, |
||
154 | TextType::class, |
||
155 | [ |
||
156 | 'required' => false, |
||
157 | ], |
||
158 | ); |
||
159 | |||
160 | return $this; |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * @param \Symfony\Component\Form\FormBuilderInterface $builder |
||
165 | * |
||
166 | * @return $this |
||
167 | */ |
||
168 | protected function addRandomGeneratedCodeLength(FormBuilderInterface $builder) |
||
169 | { |
||
170 | $builder->add( |
||
171 | static::FIELD_RANDOM_GENERATED_CODE_LENGTH, |
||
172 | ChoiceType::class, |
||
173 | [ |
||
174 | 'label' => 'Add Random Generated Code Length', |
||
175 | 'placeholder' => 'No additional random characters', |
||
176 | 'required' => false, |
||
177 | 'choices' => array_flip($this->createCodeLengthRangeList()), |
||
178 | ], |
||
179 | ); |
||
180 | |||
181 | return $this; |
||
182 | } |
||
183 | |||
184 | /** |
||
185 | * @param \Symfony\Component\Form\FormBuilderInterface $builder |
||
186 | * |
||
187 | * @return $this |
||
188 | */ |
||
189 | protected function addMaxNumberOfUsesField(FormBuilderInterface $builder) |
||
190 | { |
||
191 | $builder->add( |
||
192 | static::FIELD_MAX_NUMBER_OF_USES, |
||
193 | TextType::class, |
||
194 | [ |
||
195 | 'label' => 'Max number of uses (0 = Infinite usage)', |
||
196 | ], |
||
197 | ); |
||
198 | |||
199 | return $this; |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * @param \Symfony\Component\Form\FormBuilderInterface $builder |
||
204 | * |
||
205 | * @return $this |
||
206 | */ |
||
207 | protected function addIdDiscount(FormBuilderInterface $builder) |
||
208 | { |
||
209 | $builder->add(static::FIELD_ID_DISCOUNT, HiddenType::class); |
||
210 | |||
211 | return $this; |
||
212 | } |
||
213 | |||
214 | /** |
||
215 | * @param \Symfony\Component\Form\FormBuilderInterface $builder |
||
216 | * |
||
217 | * @return $this |
||
218 | */ |
||
219 | protected function addSubmitButton(FormBuilderInterface $builder) |
||
220 | { |
||
221 | $builder->add('generate', SubmitType::class, [ |
||
222 | 'attr' => [ |
||
223 | 'class' => 'btn-create', |
||
224 | ], |
||
225 | ]); |
||
226 | |||
227 | return $this; |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * @return array<int> |
||
232 | */ |
||
233 | protected function createCodeLengthRangeList() |
||
234 | { |
||
235 | $range = range(3, 10); |
||
236 | |||
237 | return array_combine(array_values($range), $range); |
||
238 | } |
||
239 | |||
240 | /** |
||
241 | * @param int $minValue |
||
242 | * @param int $maxValue |
||
243 | * |
||
244 | * @return list<\Symfony\Component\Validator\Constraint> |
||
245 | */ |
||
246 | protected function getQuantityFiledConstraints(int $minValue, int $maxValue): array |
||
247 | { |
||
248 | $quantityRangeErrorMessage = $this->getFactory() |
||
249 | ->getTranslatorFacade() |
||
250 | ->trans(static::FIELD_QUANTITY_ERROR_MESSAGE, [ |
||
251 | static::PARAM_QUANTITY_MESSAGE_MIN => $minValue, |
||
252 | static::PARAM_QUANTITY_MESSAGE_MAX => $maxValue, |
||
253 | ]); |
||
254 | |||
255 | $constraints = [ |
||
256 | 'constraints' => [ |
||
257 | new Range([ |
||
258 | 'min' => $minValue, |
||
259 | 'max' => $maxValue, |
||
260 | 'notInRangeMessage' => $quantityRangeErrorMessage, |
||
261 | 'invalidMessage' => $quantityRangeErrorMessage, |
||
262 | ]), |
||
263 | new Regex([ |
||
264 | 'pattern' => '/^\d+$/', |
||
265 | 'message' => $quantityRangeErrorMessage, |
||
266 | ]), |
||
267 | ], |
||
268 | ]; |
||
269 | |||
270 | return [ |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
271 | new Sequentially($constraints), |
||
272 | ]; |
||
273 | } |
||
274 | |||
275 | /** |
||
276 | * @return string |
||
277 | */ |
||
278 | public function getBlockPrefix() |
||
279 | { |
||
280 | return 'discount_voucher'; |
||
281 | } |
||
282 | } |
||
283 |