|
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\Condition\FieldDependency; |
|
7
|
|
|
use Boekkooi\Bundle\JqueryValidationBundle\Form\Rule\Mapping\MaxRule; |
|
8
|
|
|
use Boekkooi\Bundle\JqueryValidationBundle\Form\Rule\Mapping\MinRule; |
|
9
|
|
|
use Boekkooi\Bundle\JqueryValidationBundle\Form\Rule\Mapping\NumberRule; |
|
10
|
|
|
use Boekkooi\Bundle\JqueryValidationBundle\Form\Rule\Mapping\RequiredRule; |
|
11
|
|
|
use Boekkooi\Bundle\JqueryValidationBundle\Form\RuleCollection; |
|
12
|
|
|
use Boekkooi\Bundle\JqueryValidationBundle\Form\RuleMessage; |
|
13
|
|
|
use Boekkooi\Bundle\JqueryValidationBundle\Form\Rule\TransformerRule; |
|
14
|
|
|
use Boekkooi\Bundle\JqueryValidationBundle\Form\Util\FormHelper; |
|
15
|
|
|
use Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeToArrayTransformer; |
|
16
|
|
|
use Symfony\Component\Form\FormView; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @author Warnar Boekkooi <[email protected]> |
|
20
|
|
|
*/ |
|
21
|
|
|
class DateTimeToArrayTransformerPass extends ViewTransformerProcessor |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var bool |
|
25
|
|
|
*/ |
|
26
|
|
|
private $useGroupRule; |
|
27
|
|
|
|
|
28
|
|
|
public function __construct($useGroupRule = false) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->useGroupRule = $useGroupRule; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function process(FormRuleProcessorContext $context, FormRuleContextBuilder $formRuleContext) |
|
34
|
|
|
{ |
|
35
|
|
|
$form = $context->getForm(); |
|
36
|
|
|
$formConfig = $form->getConfig(); |
|
37
|
|
|
if (!$formConfig->getCompound()) { |
|
38
|
|
|
return; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** @var DateTimeToArrayTransformer $transformer */ |
|
42
|
|
|
$transformer = $this->findTransformer($formConfig, DateTimeToArrayTransformer::class); |
|
43
|
|
|
if ($transformer === null) { |
|
44
|
|
|
return; |
|
45
|
|
|
} |
|
46
|
|
|
$view = $context->getView(); |
|
47
|
|
|
$fields = $this->getTransformerFields($transformer); |
|
48
|
|
|
$invalidMessage = $this->getFormRuleMessage($formConfig); |
|
49
|
|
|
|
|
50
|
|
|
$views = array(); |
|
51
|
|
|
$conditions = array(); |
|
52
|
|
|
foreach ($fields as $fieldName) { |
|
53
|
|
|
$childView = $view->children[$fieldName]; |
|
54
|
|
|
|
|
55
|
|
|
// Get child rules collection |
|
56
|
|
|
$childRules = $formRuleContext->get($childView); |
|
57
|
|
|
if ($childRules === null) { |
|
58
|
|
|
$formRuleContext->add($childView, new RuleCollection()); |
|
59
|
|
|
$childRules = $formRuleContext->get($childView); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
// Register rules |
|
63
|
|
|
$this->addNumberCheck( |
|
64
|
|
|
$childView, |
|
65
|
|
|
$childRules, |
|
66
|
|
|
$invalidMessage, |
|
67
|
|
|
$conditions |
|
68
|
|
|
); |
|
69
|
|
|
|
|
70
|
|
|
$views[] = FormHelper::getFormName($childView); |
|
71
|
|
|
$conditions[] = new FieldDependency($childView); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
if ($this->useGroupRule && count($views) > 1) { |
|
75
|
|
|
$rules = $formRuleContext->get(array_shift($views)); |
|
76
|
|
|
$rules->set( |
|
77
|
|
|
CompoundCopyToChildPass::RULE_NAME_GROUP_REQUIRED, |
|
78
|
|
|
new TransformerRule(CompoundCopyToChildPass::RULE_NAME_GROUP_REQUIRED, $views, $invalidMessage) |
|
79
|
|
|
); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
private function addNumberCheck(FormView $view, RuleCollection $rules, RuleMessage $message = null, array $conditions = array()) |
|
84
|
|
|
{ |
|
85
|
|
|
if (!$this->useGroupRule && count($conditions) > 0) { |
|
86
|
|
|
$rules->set( |
|
87
|
|
|
RequiredRule::RULE_NAME, |
|
88
|
|
|
new TransformerRule( |
|
89
|
|
|
RequiredRule::RULE_NAME, |
|
90
|
|
|
true, |
|
91
|
|
|
$message, |
|
92
|
|
|
$conditions |
|
93
|
|
|
) |
|
94
|
|
|
); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
// Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeToArrayTransformer |
|
98
|
|
|
switch ($view->vars['name']) { |
|
99
|
|
|
case 'year': |
|
100
|
|
|
$rules->set( |
|
101
|
|
|
NumberRule::RULE_NAME, |
|
102
|
|
|
new TransformerRule( |
|
103
|
|
|
NumberRule::RULE_NAME, |
|
104
|
|
|
true, |
|
105
|
|
|
$message, |
|
106
|
|
|
$conditions |
|
107
|
|
|
) |
|
108
|
|
|
); |
|
109
|
|
|
|
|
110
|
|
|
return; |
|
111
|
|
|
case 'month': |
|
112
|
|
|
$min = 1; |
|
113
|
|
|
$max = 12; |
|
114
|
|
|
break; |
|
115
|
|
|
case 'day': |
|
116
|
|
|
$min = 1; |
|
117
|
|
|
$max = 31; |
|
118
|
|
|
break; |
|
119
|
|
|
case 'hour': |
|
120
|
|
|
$min = 0; |
|
121
|
|
|
$max = 23; |
|
122
|
|
|
break; |
|
123
|
|
|
case 'minute': |
|
124
|
|
|
case 'second': |
|
125
|
|
|
$min = 0; |
|
126
|
|
|
$max = 59; |
|
127
|
|
|
break; |
|
128
|
|
|
default: |
|
129
|
|
|
return; |
|
130
|
|
|
} |
|
131
|
|
|
$rules->set( |
|
132
|
|
|
MinRule::RULE_NAME, |
|
133
|
|
|
new TransformerRule(MinRule::RULE_NAME, $min, $message, $conditions) |
|
134
|
|
|
); |
|
135
|
|
|
$rules->set( |
|
136
|
|
|
MaxRule::RULE_NAME, |
|
137
|
|
|
new TransformerRule(MaxRule::RULE_NAME, $max, $message, $conditions) |
|
138
|
|
|
); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
private function getTransformerFields(DateTimeToArrayTransformer $transformer) |
|
142
|
|
|
{ |
|
143
|
|
|
$property = new \ReflectionProperty( |
|
144
|
|
|
DateTimeToArrayTransformer::class, |
|
145
|
|
|
'fields' |
|
146
|
|
|
); |
|
147
|
|
|
$property->setAccessible(true); |
|
148
|
|
|
|
|
149
|
|
|
return $property->getValue($transformer); |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|