forked from boekkooi/JqueryValidationBundle
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDateTimeToArrayTransformerPass.php
151 lines (136 loc) · 5.01 KB
/
DateTimeToArrayTransformerPass.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?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\Condition\FieldDependency;
use Boekkooi\Bundle\JqueryValidationBundle\Form\Rule\Mapping\MaxRule;
use Boekkooi\Bundle\JqueryValidationBundle\Form\Rule\Mapping\MinRule;
use Boekkooi\Bundle\JqueryValidationBundle\Form\Rule\Mapping\NumberRule;
use Boekkooi\Bundle\JqueryValidationBundle\Form\Rule\Mapping\RequiredRule;
use Boekkooi\Bundle\JqueryValidationBundle\Form\RuleCollection;
use Boekkooi\Bundle\JqueryValidationBundle\Form\RuleMessage;
use Boekkooi\Bundle\JqueryValidationBundle\Form\Rule\TransformerRule;
use Boekkooi\Bundle\JqueryValidationBundle\Form\Util\FormHelper;
use Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeToArrayTransformer;
use Symfony\Component\Form\FormView;
/**
* @author Warnar Boekkooi <[email protected]>
*/
class DateTimeToArrayTransformerPass extends ViewTransformerProcessor
{
/**
* @var bool
*/
private $useGroupRule;
public function __construct($useGroupRule = false)
{
$this->useGroupRule = $useGroupRule;
}
public function process(FormRuleProcessorContext $context, FormRuleContextBuilder $formRuleContext)
{
$form = $context->getForm();
$formConfig = $form->getConfig();
if (!$formConfig->getCompound()) {
return;
}
/** @var DateTimeToArrayTransformer $transformer */
$transformer = $this->findTransformer($formConfig, DateTimeToArrayTransformer::class);
if ($transformer === null) {
return;
}
$view = $context->getView();
$fields = $this->getTransformerFields($transformer);
$invalidMessage = $this->getFormRuleMessage($formConfig);
$views = array();
$conditions = array();
foreach ($fields as $fieldName) {
$childView = $view->children[$fieldName];
// Get child rules collection
$childRules = $formRuleContext->get($childView);
if ($childRules === null) {
$formRuleContext->add($childView, new RuleCollection());
$childRules = $formRuleContext->get($childView);
}
// Register rules
$this->addNumberCheck(
$childView,
$childRules,
$invalidMessage,
$conditions
);
$views[] = FormHelper::getFormName($childView);
$conditions[] = new FieldDependency($childView);
}
if ($this->useGroupRule && count($views) > 1) {
$rules = $formRuleContext->get(array_shift($views));
$rules->set(
CompoundCopyToChildPass::RULE_NAME_GROUP_REQUIRED,
new TransformerRule(CompoundCopyToChildPass::RULE_NAME_GROUP_REQUIRED, $views, $invalidMessage)
);
}
}
private function addNumberCheck(FormView $view, RuleCollection $rules, RuleMessage $message = null, array $conditions = array())
{
if (!$this->useGroupRule && count($conditions) > 0) {
$rules->set(
RequiredRule::RULE_NAME,
new TransformerRule(
RequiredRule::RULE_NAME,
true,
$message,
$conditions
)
);
}
// Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeToArrayTransformer
switch ($view->vars['name']) {
case 'year':
$rules->set(
NumberRule::RULE_NAME,
new TransformerRule(
NumberRule::RULE_NAME,
true,
$message,
$conditions
)
);
return;
case 'month':
$min = 1;
$max = 12;
break;
case 'day':
$min = 1;
$max = 31;
break;
case 'hour':
$min = 0;
$max = 23;
break;
case 'minute':
case 'second':
$min = 0;
$max = 59;
break;
default:
return;
}
$rules->set(
MinRule::RULE_NAME,
new TransformerRule(MinRule::RULE_NAME, $min, $message, $conditions)
);
$rules->set(
MaxRule::RULE_NAME,
new TransformerRule(MaxRule::RULE_NAME, $max, $message, $conditions)
);
}
private function getTransformerFields(DateTimeToArrayTransformer $transformer)
{
$property = new \ReflectionProperty(
DateTimeToArrayTransformer::class,
'fields'
);
$property->setAccessible(true);
return $property->getValue($transformer);
}
}