1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* (c) Christian Gripp <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Core23\AntiSpamBundle\Form\Extension; |
13
|
|
|
|
14
|
|
|
use Core23\AntiSpamBundle\Form\EventListener\AntiSpamTimeListener; |
15
|
|
|
use Core23\AntiSpamBundle\Provider\TimeProviderInterface; |
16
|
|
|
use Symfony\Component\Form\AbstractTypeExtension; |
17
|
|
|
use Symfony\Component\Form\Extension\Core\Type\FormType; |
18
|
|
|
use Symfony\Component\Form\FormBuilderInterface; |
19
|
|
|
use Symfony\Component\Form\FormInterface; |
20
|
|
|
use Symfony\Component\Form\FormView; |
21
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
22
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
23
|
|
|
|
24
|
|
|
final class TimeFormExtension extends AbstractTypeExtension |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var TimeProviderInterface |
28
|
|
|
*/ |
29
|
|
|
private $timeProvider; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var TranslatorInterface |
33
|
|
|
*/ |
34
|
|
|
private $translator; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var array<string, mixed> |
38
|
|
|
*/ |
39
|
|
|
private $defaults; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param TimeProviderInterface $timeProvider |
43
|
|
|
* @param TranslatorInterface $translator |
44
|
|
|
* @param array $defaults |
45
|
|
|
*/ |
46
|
|
|
public function __construct(TimeProviderInterface $timeProvider, TranslatorInterface $translator, array $defaults) |
47
|
|
|
{ |
48
|
|
|
$this->timeProvider = $timeProvider; |
49
|
|
|
$this->translator = $translator; |
50
|
|
|
$this->defaults = $defaults; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
|
|
public function buildForm(FormBuilderInterface $builder, array $options): void |
57
|
|
|
{ |
58
|
|
|
if (!$options['antispam_time']) { |
59
|
|
|
return; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$providerOptions = [ |
63
|
|
|
'min' => $options['antispam_time_min'], |
64
|
|
|
'max' => $options['antispam_time_max'], |
65
|
|
|
]; |
66
|
|
|
|
67
|
|
|
$builder |
68
|
|
|
->addEventSubscriber(new AntiSpamTimeListener($this->timeProvider, $this->translator, $providerOptions)) |
69
|
|
|
; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
*/ |
75
|
|
|
public function finishView(FormView $view, FormInterface $form, array $options): void |
76
|
|
|
{ |
77
|
|
|
if ($view->parent || !$options['antispam_time'] || !$options['compound']) { |
78
|
|
|
return; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$this->timeProvider->createFormProtection($form->getName()); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritdoc} |
86
|
|
|
*/ |
87
|
|
|
public function configureOptions(OptionsResolver $resolver): void |
88
|
|
|
{ |
89
|
|
|
$resolver |
90
|
|
|
->setDefaults([ |
91
|
|
|
'antispam_time' => $this->defaults['global'], |
92
|
|
|
'antispam_time_min' => $this->defaults['min'], |
93
|
|
|
'antispam_time_max' => $this->defaults['max'], |
94
|
|
|
]) |
95
|
|
|
->setAllowedTypes('antispam_time', 'bool') |
96
|
|
|
->setAllowedTypes('antispam_time_min', 'int') |
97
|
|
|
->setAllowedTypes('antispam_time_max', 'int') |
98
|
|
|
; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* {@inheritdoc} |
103
|
|
|
*/ |
104
|
|
|
public function getExtendedType() |
105
|
|
|
{ |
106
|
|
|
foreach (static::getExtendedTypes() as $extendedType) { |
107
|
|
|
return $extendedType; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* {@inheritdoc} |
113
|
|
|
*/ |
114
|
|
|
public static function getExtendedTypes(): iterable |
115
|
|
|
{ |
116
|
|
|
return [ |
117
|
|
|
FormType::class, |
118
|
|
|
]; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|