|
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
|
|
|
public function __construct(TimeProviderInterface $timeProvider, TranslatorInterface $translator, array $defaults) |
|
42
|
|
|
{ |
|
43
|
|
|
$this->timeProvider = $timeProvider; |
|
44
|
|
|
$this->translator = $translator; |
|
45
|
|
|
$this->defaults = $defaults; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function buildForm(FormBuilderInterface $builder, array $options): void |
|
49
|
|
|
{ |
|
50
|
|
|
if (!$options['antispam_time']) { |
|
51
|
|
|
return; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$providerOptions = [ |
|
55
|
|
|
'min' => $options['antispam_time_min'], |
|
56
|
|
|
'max' => $options['antispam_time_max'], |
|
57
|
|
|
]; |
|
58
|
|
|
|
|
59
|
|
|
$builder |
|
60
|
|
|
->addEventSubscriber(new AntiSpamTimeListener($this->timeProvider, $this->translator, $providerOptions)) |
|
61
|
|
|
; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function finishView(FormView $view, FormInterface $form, array $options): void |
|
65
|
|
|
{ |
|
66
|
|
|
if ($view->parent || !$options['antispam_time'] || !$options['compound']) { |
|
67
|
|
|
return; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$this->timeProvider->createFormProtection($form->getName()); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function configureOptions(OptionsResolver $resolver): void |
|
74
|
|
|
{ |
|
75
|
|
|
$resolver |
|
76
|
|
|
->setDefaults([ |
|
77
|
|
|
'antispam_time' => $this->defaults['global'], |
|
78
|
|
|
'antispam_time_min' => $this->defaults['min'], |
|
79
|
|
|
'antispam_time_max' => $this->defaults['max'], |
|
80
|
|
|
]) |
|
81
|
|
|
->setAllowedTypes('antispam_time', 'bool') |
|
82
|
|
|
->setAllowedTypes('antispam_time_min', 'int') |
|
83
|
|
|
->setAllowedTypes('antispam_time_max', 'int') |
|
84
|
|
|
; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function getExtendedType() |
|
88
|
|
|
{ |
|
89
|
|
|
foreach (static::getExtendedTypes() as $extendedType) { |
|
90
|
|
|
return $extendedType; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public static function getExtendedTypes(): iterable |
|
95
|
|
|
{ |
|
96
|
|
|
return [ |
|
97
|
|
|
FormType::class, |
|
98
|
|
|
]; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|