forked from boekkooi/JqueryValidationBundle
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDateTimeToStringTransformerPass.php
136 lines (122 loc) · 4.38 KB
/
DateTimeToStringTransformerPass.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
<?php
namespace Boekkooi\Bundle\JqueryValidationBundle\Form\Rule\Processor;
use Boekkooi\Bundle\JqueryValidationBundle\Form\FormRuleContextBuilder;
use Boekkooi\Bundle\JqueryValidationBundle\Form\FormRuleProcessorContext;
use Boekkooi\Bundle\JqueryValidationBundle\Form\Rule\Mapping\MaxRule;
use Boekkooi\Bundle\JqueryValidationBundle\Form\Rule\Mapping\MinRule;
use Boekkooi\Bundle\JqueryValidationBundle\Form\RuleCollection;
use Boekkooi\Bundle\JqueryValidationBundle\Form\Rule\TransformerRule;
use Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeToRfc3339Transformer;
use Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeToStringTransformer;
use Symfony\Component\Form\Extension\Core\Type\BirthdayType;
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\TimeType;
use Symfony\Component\Form\FormConfigInterface;
use Symfony\Component\Form\FormView;
/**
* @author Warnar Boekkooi <[email protected]>
*/
class DateTimeToStringTransformerPass extends ViewTransformerProcessor
{
/**
* @var bool
*/
private $useTime;
public function __construct($useTimeRule = false)
{
$this->useTime = $useTimeRule;
}
public function process(FormRuleProcessorContext $context, FormRuleContextBuilder $formRuleContext)
{
$form = $context->getForm();
$formConfig = $form->getConfig();
if ($formConfig->getCompound()) {
return;
}
if (
$this->findTransformer($formConfig, DateTimeToStringTransformer::class) === null &&
$this->findTransformer($formConfig, DateTimeToRfc3339Transformer::class) === null
) {
return;
}
$formView = $context->getView();
$formTypeClass = get_class($formConfig->getType()->getInnerType());
switch ($formTypeClass) {
case TimeType::class:
$this->processTime($formView, $formConfig, $formRuleContext);
return;
case BirthdayType::class:
case DateType::class:
$this->processDate($formView, $formConfig, $formRuleContext);
return;
case DateTimeType::class:
$this->processDateTime($formView, $formConfig, $formRuleContext);
return;
}
}
private function processTime(FormView $view, FormConfigInterface $config, FormRuleContextBuilder $context)
{
$rules = new RuleCollection();
if ($config->getOption('with_minutes')) {
// Only add time rule if additional rules are enabled
if ($this->useTime) {
$rules->set(
'time',
new TransformerRule(
'time',
true,
$this->getFormRuleMessage($config)
)
);
}
} else {
$rules->set(
MinRule::RULE_NAME,
new TransformerRule(
MinRule::RULE_NAME,
0,
$this->getFormRuleMessage($config)
)
);
$rules->set(
MaxRule::RULE_NAME,
new TransformerRule(
MaxRule::RULE_NAME,
23,
$this->getFormRuleMessage($config)
)
);
}
$context->add($view, $rules);
}
private function processDate(FormView $view, FormConfigInterface $config, FormRuleContextBuilder $context)
{
$rules = new RuleCollection();
$rules->set(
'dateISO',
new TransformerRule(
'dateISO',
true,
$this->getFormRuleMessage($config)
)
);
$context->add($view, $rules);
}
private function processDateTime(FormView $view, FormConfigInterface $config, FormRuleContextBuilder $context)
{
if ($config->getOption('format') !== DateTimeType::HTML5_FORMAT) {
return;
}
$rules = new RuleCollection();
$rules->set(
'date',
new TransformerRule(
'date',
true,
$this->getFormRuleMessage($config)
)
);
$context->add($view, $rules);
}
}