1
|
|
|
<?php |
2
|
|
|
namespace Boekkooi\Bundle\JqueryValidationBundle\Form\Rule\Processor; |
3
|
|
|
|
4
|
|
|
use Boekkooi\Bundle\JqueryValidationBundle\Form\FormRuleContextBuilder; |
5
|
|
|
use Boekkooi\Bundle\JqueryValidationBundle\Form\FormRuleProcessorContext; |
6
|
|
|
use Boekkooi\Bundle\JqueryValidationBundle\Form\Rule\Mapping\MaxRule; |
7
|
|
|
use Boekkooi\Bundle\JqueryValidationBundle\Form\Rule\Mapping\MinRule; |
8
|
|
|
use Boekkooi\Bundle\JqueryValidationBundle\Form\RuleCollection; |
9
|
|
|
use Boekkooi\Bundle\JqueryValidationBundle\Form\Rule\TransformerRule; |
10
|
|
|
use Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeToRfc3339Transformer; |
11
|
|
|
use Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeToStringTransformer; |
12
|
|
|
use Symfony\Component\Form\Extension\Core\Type\BirthdayType; |
13
|
|
|
use Symfony\Component\Form\Extension\Core\Type\DateTimeType; |
14
|
|
|
use Symfony\Component\Form\Extension\Core\Type\DateType; |
15
|
|
|
use Symfony\Component\Form\Extension\Core\Type\TimeType; |
16
|
|
|
use Symfony\Component\Form\FormConfigInterface; |
17
|
|
|
use Symfony\Component\Form\FormView; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @author Warnar Boekkooi <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class DateTimeToStringTransformerPass extends ViewTransformerProcessor |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var bool |
26
|
|
|
*/ |
27
|
|
|
private $useTime; |
28
|
|
|
|
29
|
|
|
public function __construct($useTimeRule = false) |
30
|
|
|
{ |
31
|
|
|
$this->useTime = $useTimeRule; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function process(FormRuleProcessorContext $context, FormRuleContextBuilder $formRuleContext) |
35
|
|
|
{ |
36
|
|
|
$form = $context->getForm(); |
37
|
|
|
$formConfig = $form->getConfig(); |
38
|
|
|
if ($formConfig->getCompound()) { |
39
|
|
|
return; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
if ( |
43
|
|
|
$this->findTransformer($formConfig, DateTimeToStringTransformer::class) === null && |
44
|
|
|
$this->findTransformer($formConfig, DateTimeToRfc3339Transformer::class) === null |
45
|
|
|
) { |
46
|
|
|
return; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$formView = $context->getView(); |
50
|
|
|
$formTypeClass = get_class($formConfig->getType()->getInnerType()); |
51
|
|
|
|
52
|
|
|
switch ($formTypeClass) { |
53
|
|
|
case TimeType::class: |
54
|
|
|
$this->processTime($formView, $formConfig, $formRuleContext); |
55
|
|
|
|
56
|
|
|
return; |
57
|
|
|
case BirthdayType::class: |
58
|
|
|
case DateType::class: |
59
|
|
|
$this->processDate($formView, $formConfig, $formRuleContext); |
60
|
|
|
|
61
|
|
|
return; |
62
|
|
|
case DateTimeType::class: |
63
|
|
|
$this->processDateTime($formView, $formConfig, $formRuleContext); |
64
|
|
|
|
65
|
|
|
return; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
private function processTime(FormView $view, FormConfigInterface $config, FormRuleContextBuilder $context) |
70
|
|
|
{ |
71
|
|
|
$rules = new RuleCollection(); |
72
|
|
|
if ($config->getOption('with_minutes')) { |
73
|
|
|
// Only add time rule if additional rules are enabled |
74
|
|
|
if ($this->useTime) { |
75
|
|
|
$rules->set( |
76
|
|
|
'time', |
77
|
|
|
new TransformerRule( |
78
|
|
|
'time', |
79
|
|
|
true, |
80
|
|
|
$this->getFormRuleMessage($config) |
81
|
|
|
) |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
} else { |
85
|
|
|
$rules->set( |
86
|
|
|
MinRule::RULE_NAME, |
87
|
|
|
new TransformerRule( |
88
|
|
|
MinRule::RULE_NAME, |
89
|
|
|
0, |
90
|
|
|
$this->getFormRuleMessage($config) |
91
|
|
|
) |
92
|
|
|
); |
93
|
|
|
$rules->set( |
94
|
|
|
MaxRule::RULE_NAME, |
95
|
|
|
new TransformerRule( |
96
|
|
|
MaxRule::RULE_NAME, |
97
|
|
|
23, |
98
|
|
|
$this->getFormRuleMessage($config) |
99
|
|
|
) |
100
|
|
|
); |
101
|
|
|
} |
102
|
|
|
$context->add($view, $rules); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
private function processDate(FormView $view, FormConfigInterface $config, FormRuleContextBuilder $context) |
106
|
|
|
{ |
107
|
|
|
$rules = new RuleCollection(); |
108
|
|
|
$rules->set( |
109
|
|
|
'dateISO', |
110
|
|
|
new TransformerRule( |
111
|
|
|
'dateISO', |
112
|
|
|
true, |
113
|
|
|
$this->getFormRuleMessage($config) |
114
|
|
|
) |
115
|
|
|
); |
116
|
|
|
$context->add($view, $rules); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
private function processDateTime(FormView $view, FormConfigInterface $config, FormRuleContextBuilder $context) |
120
|
|
|
{ |
121
|
|
|
if ($config->getOption('format') !== DateTimeType::HTML5_FORMAT) { |
122
|
|
|
return; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$rules = new RuleCollection(); |
126
|
|
|
$rules->set( |
127
|
|
|
'date', |
128
|
|
|
new TransformerRule( |
129
|
|
|
'date', |
130
|
|
|
true, |
131
|
|
|
$this->getFormRuleMessage($config) |
132
|
|
|
) |
133
|
|
|
); |
134
|
|
|
$context->add($view, $rules); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|